Submission #996740

# Submission time Handle Problem Language Result Execution time Memory
996740 2024-06-11T06:41:00 Z MrAndria JJOOII 2 (JOI20_ho_t2) C++14
100 / 100
25 ms 8252 KB
/*
¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦
¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦
¦¦¦_¯¦¦¦¦¦¦¦¦¦¦¦¦¦¦_¯¦¦¦¦¦_¯_¯_¯_¯_¯_¯¦¦¦¦¦¦¦_¯_¯_¯_¯_¯¦¦¦¦¦_¯¦¦¦¦¦¦¦¦¦¦¦¦¦¦_¯¦¦¦¦¦_¯_¯_¯_¯¦¦¦¦¦¦¦_¯_¯_¯_¯_¯_¯¦¦¦¦¦¦¦_¯_¯_¯¦¦¦¦¦_¯_¯_¯_¯_¯¦¦¦
¦¦¦_¯_¯_¯_¯_¯_¯_¯_¯_¯¦¦¦¦¦_¯¦¦¦¦¦¦¦¦_¯¦¦¦¦¦¦¦_¯¦¦¦¦¦¦_¯¦¦¦¦¦_¯_¯_¯_¯_¯¦¦¦¦¦¦_¯¦¦¦¦¦_¯¦¦¦¦_¯_¯¦¦¦¦¦_¯¦¦¦¦¦¦¦¦_¯¦¦¦¦¦¦¦¦¦_¯¦¦¦¦¦¦¦_¯¦¦¦¦¦¦_¯¦¦¦
¦¦¦_¯¦¦¦¦¦¦_¯¦¦¦¦¦¦_¯¦¦¦¦¦_¯¦¦¦¦¦¦¦¦_¯¦¦¦¦¦¦¦_¯¦¦¦¦¦¦_¯¦¦¦¦¦_¯¦¦¦¦¦¦_¯¦¦¦¦¦¦_¯¦¦¦¦¦_¯¦¦¦¦¦¦_¯¦¦¦¦¦_¯¦¦¦¦¦¦¦¦_¯¦¦¦¦¦¦¦¦¦_¯¦¦¦¦¦¦¦_¯¦¦¦¦¦¦_¯¦¦¦
¦¦¦_¯¦¦¦¦¦¦_¯¦¦¦¦¦¦_¯¦¦¦¦¦_¯¦¦¦¦¦¦¦¦_¯¦¦¦¦¦¦¦_¯¦¦¦¦¦¦_¯¦¦¦¦¦_¯¦¦¦¦¦¦_¯¦¦¦¦¦¦_¯¦¦¦¦¦_¯¦¦¦¦¦¦_¯¦¦¦¦¦_¯¦¦¦¦¦¦¦¦_¯¦¦¦¦¦¦¦¦¦_¯¦¦¦¦¦¦¦_¯¦¦¦¦¦¦_¯¦¦¦
¦¦¦_¯¦¦¦¦¦¦_¯¦¦¦¦¦¦_¯¦¦¦¦¦_¯_¯_¯_¯_¯_¯¦¦¦¦¦¦¦_¯_¯_¯_¯_¯¦¦¦¦¦_¯¦¦¦¦¦¦_¯¦¦¦¦¦¦_¯¦¦¦¦¦_¯¦¦¦¦¦¦_¯¦¦¦¦¦_¯_¯_¯_¯_¯_¯¦¦¦¦¦¦¦¦¦_¯¦¦¦¦¦¦¦_¯_¯_¯_¯_¯¦¦¦
¦¦¦_¯¦¦¦¦¦¦¦¦¦¦¦¦¦¦_¯¦¦¦¦¦_¯¦¦¦¦¦¦_¯¦¦¦¦¦¦¦¦¦_¯¦¦¦¦¦¦_¯¦¦¦¦¦_¯¦¦¦¦¦¦_¯¦¦¦¦¦¦_¯¦¦¦¦¦_¯¦¦¦¦¦¦_¯¦¦¦¦¦_¯¦¦¦¦¦¦_¯¦¦¦¦¦¦¦¦¦¦¦_¯¦¦¦¦¦¦¦_¯¦¦¦¦¦¦_¯¦¦¦
¦¦¦_¯¦¦¦¦¦¦¦¦¦¦¦¦¦¦_¯¦¦¦¦¦_¯¦¦¦¦¦¦_¯¦¦¦¦¦¦¦¦¦_¯¦¦¦¦¦¦_¯¦¦¦¦¦_¯¦¦¦¦¦¦_¯¦¦¦¦¦¦_¯¦¦¦¦¦_¯¦¦¦¦¦¦_¯¦¦¦¦¦_¯¦¦¦¦¦¦_¯¦¦¦¦¦¦¦¦¦¦¦_¯¦¦¦¦¦¦¦_¯¦¦¦¦¦¦_¯¦¦¦
¦¦¦_¯¦¦¦¦¦¦¦¦¦¦¦¦¦¦_¯¦¦¦¦¦_¯¦¦¦¦¦¦_¯¦¦¦¦¦¦¦¦¦_¯¦¦¦¦¦¦_¯¦¦¦¦¦_¯¦¦¦¦¦¦_¯_¯_¯_¯_¯¦¦¦¦¦_¯¦¦¦¦_¯_¯¦¦¦¦¦_¯¦¦¦¦¦¦_¯¦¦¦¦¦¦¦¦¦¦¦_¯¦¦¦¦¦¦¦_¯¦¦¦¦¦¦_¯¦¦¦
¦¦¦_¯¦¦¦¦¦¦¦¦¦¦¦¦¦¦_¯¦¦¦¦¦_¯¦¦¦¦¦¦_¯_¯_¯¦¦¦¦¦_¯¦¦¦¦¦¦_¯¦¦¦¦¦_¯¦¦¦¦¦¦¦¦¦¦¦¦¦¦_¯¦¦¦¦¦_¯_¯_¯_¯¦¦¦¦¦¦¦_¯¦¦¦¦¦¦_¯_¯_¯¦¦¦¦¦_¯_¯_¯¦¦¦¦¦_¯¦¦¦¦¦¦_¯¦¦¦
¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦
¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦
*/
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ff first
#define ss second
//#define int long long
int l,r,n,t,m,mid,ans,sum,sum1,sum2,sum0,k,p1[1000005],p2[1000005],p3[1000005],mi;
vector <int> v,v1;
map <int,int> mp;
string s,s1;
bool b1;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    t=1;
    // cin>>t;
    while(t--){
        cin>>n>>k;
        cin>>s;
        s=" "+s;
        mi=INT_MAX;
        for(int i=1;i<=n;i++){
            if(s[i]=='J'){
                p1[i]=p1[i-1]+1;
            }else{
                p1[i]=p1[i-1];
            }
            if(s[i]=='O'){
                p2[i]=p2[i-1]+1;
            }else{
                p2[i]=p2[i-1];
            }
            if(s[i]=='I'){
                p3[i]=p3[i-1]+1;
            }else{
                p3[i]=p3[i-1];
            }
        }
        for(int i=1;i<=n;i++){
            int mn1=p1[i-1];
            int mn2=p2[i-1];
            int mn3=p3[i-1];
            sum=0;
            l=i;
            r=n;
            while(l<=r){
                mid=(l+r)/2;
                if(p1[mid]==mn1+k){
                    r=mid-1;
                    ans=mid;
                }else{
                    if(p1[mid]<mn1+k){
                        l=mid+1;
                    }else{
                        r=mid-1;
                    }
                }
                
                // cout<<mid<<" "<<p1[mid]<<endl;
            }
            // cout<<ans<<" "<<mn1<<" "<<mn1+k<<" "<<p1[4]<<endl;
            if(p1[ans]==mn1+k){
                l=ans+1;
                mn2=p2[ans];
                r=n;
                while(l<=r){
                    mid=(l+r)/2;
                    if(p2[mid]==mn2+k){
                        r=mid-1;
                        ans=mid;
                    }else{
                        if(p2[mid]<mn2+k){
                            l=mid+1;
                        }else{
                            r=mid-1;
                        }
                    }
                    
                    // cout<<mid<<" "<<p1[mid]<<endl;
                }
                // cout<<ans<<" "<<mn2<<" "<<mn2+k<<" "<<p1[7]<<endl;
                if(p2[ans]==mn2+k){
                    
                    l=ans+1;
                    mn3=p3[ans];
                    r=n;
                    while(l<=r){
                        mid=(l+r)/2;
                        if(p3[mid]==mn3+k){
                            r=mid-1;
                            ans=mid;
                        }else{
                            if(p3[mid]<mn3+k){
                                l=mid+1;
                            }else{
                                r=mid-1;
                            }
                        }
                        
                        // cout<<mid<<" "<<p1[mid]<<endl;
                    }
                    if(p3[ans]==mn3+k){
                        sum+=(ans-(i-1)-3*k);
                        mi=min(mi,sum);
                    }else{
                        break;
                    }
                }else{
                    break;
                }
            }else{
                break;
            }

        }
    }
    if(mi==INT_MAX){
        cout<<-1<<endl;
        return 0;
    }
    cout<<mi<<endl;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 356 KB Output is correct
3 Correct 0 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 456 KB Output is correct
7 Correct 0 ms 464 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 356 KB Output is correct
3 Correct 0 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 456 KB Output is correct
7 Correct 0 ms 464 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 468 KB Output is correct
28 Correct 0 ms 344 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 356 KB Output is correct
3 Correct 0 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 456 KB Output is correct
7 Correct 0 ms 464 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 468 KB Output is correct
28 Correct 0 ms 344 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 23 ms 7984 KB Output is correct
37 Correct 24 ms 8000 KB Output is correct
38 Correct 25 ms 8224 KB Output is correct
39 Correct 19 ms 8228 KB Output is correct
40 Correct 8 ms 8252 KB Output is correct
41 Correct 15 ms 8224 KB Output is correct
42 Correct 24 ms 8228 KB Output is correct
43 Correct 1 ms 5976 KB Output is correct
44 Correct 2 ms 8016 KB Output is correct
45 Correct 2 ms 8228 KB Output is correct
46 Correct 3 ms 8228 KB Output is correct
47 Correct 3 ms 8228 KB Output is correct
48 Correct 2 ms 8224 KB Output is correct
49 Correct 2 ms 5732 KB Output is correct
50 Correct 2 ms 8228 KB Output is correct
51 Correct 2 ms 8228 KB Output is correct
52 Correct 2 ms 8232 KB Output is correct
53 Correct 2 ms 8224 KB Output is correct
54 Correct 10 ms 8252 KB Output is correct
55 Correct 5 ms 8252 KB Output is correct
56 Correct 2 ms 8228 KB Output is correct