Submission #979885

# Submission time Handle Problem Language Result Execution time Memory
979885 2024-05-11T14:36:30 Z IUA_Hasin JJOOII 2 (JOI20_ho_t2) C++17
100 / 100
96 ms 21276 KB
#include <bits/stdc++.h>

#define ll                                  long long
#define ld                                  long double
#define endl                                "\n"

using namespace std;


int main(){
    #if __has_include("LOCAL.hh")
        #include "LOCAL.hh"
    #endif

    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
        using namespace std::chrono;
        cout << fixed << setprecision(9);
        auto begin = steady_clock::now();
    #else
        std::ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout.tie(NULL);
    #endif



    ll n, k;
    cin>>n>>k;
    string s;
    cin>>s;

    vector<ll> vj, vo, vi;
    for(int i=0; i<n; i++){
        if(s[i]=='J'){
            vj.push_back(i);
        } else if(s[i]=='O'){
            vo.push_back(i);
        } else if(s[i]=='I'){
            vi.push_back(i);
        }
    }

    map<ll, ll> mj, mo, mi;
    for(int i=0; i<vj.size(); i++){
        ll a = vj[i];
        if(i<=(vj.size()-k)){
            ll b = vj[i+k-1];
            mj[a] = b; 
        } else {
            mj[a] = -1;
        }
    }

    set<ll> so;
    for(int i=0; i<vo.size(); i++){
        ll a = vo[i];
        if(i<=(vo.size()-k)){
            ll b = vo[i+k-1];
            mo[a] = b;
        } else {
            mo[a] = -1;
        }
        so.insert(a);
    }

    set<ll> si;
    for(int i=0; i<vi.size(); i++){
        ll a = vi[i];
        if(i<=(vi.size()-k)){
            ll b = vi[i+k-1];
            mi[a] = b;
        } else {
            mi[a] = -1;
        }
        si.insert(a);
    }


    // for(int i=0; i<vj.size(); i++){
    //     cout << mj[vj[i]] << " ";
    // }
    // cout<<endl;
    // for(int i=0; i<vo.size(); i++){
    //     cout << mo[vo[i]] << " ";
    // }
    // cout<<endl;
    // for(int i=0; i<vi.size(); i++){
    //     cout << mi[vi[i]] << " ";
    // }
    // cout<<endl;
    // cout<<endl;

    // for(auto u : so){
    //     cout << u << " ";
    // }
    // cout<<endl;
    // for(auto u : si){
    //     cout << u << " ";
    // }
    // cout<<endl;
    // cout<<endl;

    ll status = -1;
    ll possible = 1;
    ll ans = 1e9;

    for(int i=0; i<=vj.size()-k; i++){
        if(possible==-1){
            break;
        } else {
            ll a = vj[i];
            ll b = mj[a];
            //cout << a << " " << b << endl;
            auto it = so.upper_bound(b);
            ll temp1;
            if(it==so.end()){
                possible=-1;
                break;
            } else {
                temp1 = *it;
            }
            ll c = mo[temp1];
            //cout << c << " " << temp1 << endl;
            if(c==-1){
                possible = -1;
                break;
            } else {
                auto it2 = si.upper_bound(c);
                ll temp2;
                if(it2==si.end()){
                    possible = -1;
                    break;
                } else {
                    temp2 = *it2;
                }
                ll d = mi[temp2];
                //cout << d << " " << temp2 << endl;
                if(d==-1){
                    possible = -1;
                    break;
                } else {
                    status = 1;
                    ll e = d-a+1;
                    ll tempans = e-3*k;
                    ans = min(ans, tempans);
                    //cout << e << " " << tempans << endl;
                }
            }
        }
    }

    //cout << status << endl;
    if(status==1){
        cout << ans << endl;
    } else {
        cout << -1 << endl;
    }
    






    #ifdef LOCAL
        auto end = steady_clock::now();
        cout << "\nTime : " 
             << (ld)duration_cast<nanoseconds>
                (end - begin).count()/1000000000 
             << "s" << endl;
    #endif


    return 0;
}

Compilation message

ho_t2.cpp: In function 'int main()':
ho_t2.cpp:46:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |     for(int i=0; i<vj.size(); i++){
      |                  ~^~~~~~~~~~
ho_t2.cpp:48:13: warning: comparison of integer expressions of different signedness: 'int' and 'long long unsigned int' [-Wsign-compare]
   48 |         if(i<=(vj.size()-k)){
      |            ~^~~~~~~~~~~~~~~
ho_t2.cpp:57:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |     for(int i=0; i<vo.size(); i++){
      |                  ~^~~~~~~~~~
ho_t2.cpp:59:13: warning: comparison of integer expressions of different signedness: 'int' and 'long long unsigned int' [-Wsign-compare]
   59 |         if(i<=(vo.size()-k)){
      |            ~^~~~~~~~~~~~~~~
ho_t2.cpp:69:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   69 |     for(int i=0; i<vi.size(); i++){
      |                  ~^~~~~~~~~~
ho_t2.cpp:71:13: warning: comparison of integer expressions of different signedness: 'int' and 'long long unsigned int' [-Wsign-compare]
   71 |         if(i<=(vi.size()-k)){
      |            ~^~~~~~~~~~~~~~~
ho_t2.cpp:109:19: warning: comparison of integer expressions of different signedness: 'int' and 'long long unsigned int' [-Wsign-compare]
  109 |     for(int i=0; i<=vj.size()-k; i++){
      |                  ~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 452 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 452 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 2 ms 604 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 456 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 2 ms 604 KB Output is correct
21 Correct 1 ms 772 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 756 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 1 ms 600 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1 ms 712 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 1 ms 604 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 452 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 2 ms 604 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 456 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 2 ms 604 KB Output is correct
21 Correct 1 ms 772 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 756 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 1 ms 600 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1 ms 712 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 1 ms 604 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 78 ms 19720 KB Output is correct
37 Correct 96 ms 21116 KB Output is correct
38 Correct 95 ms 21096 KB Output is correct
39 Correct 75 ms 20768 KB Output is correct
40 Correct 76 ms 21276 KB Output is correct
41 Correct 70 ms 21168 KB Output is correct
42 Correct 84 ms 21124 KB Output is correct
43 Correct 40 ms 13132 KB Output is correct
44 Correct 37 ms 16372 KB Output is correct
45 Correct 54 ms 21108 KB Output is correct
46 Correct 57 ms 20980 KB Output is correct
47 Correct 65 ms 21140 KB Output is correct
48 Correct 57 ms 21088 KB Output is correct
49 Correct 38 ms 14120 KB Output is correct
50 Correct 55 ms 21204 KB Output is correct
51 Correct 56 ms 21104 KB Output is correct
52 Correct 58 ms 20356 KB Output is correct
53 Correct 55 ms 21156 KB Output is correct
54 Correct 68 ms 21008 KB Output is correct
55 Correct 60 ms 21052 KB Output is correct
56 Correct 65 ms 21000 KB Output is correct