Submission #256941

#TimeUsernameProblemLanguageResultExecution timeMemory
256941akatJJOOII 2 (JOI20_ho_t2)C++14
100 / 100
10 ms1548 KiB
#include<bits/stdc++.h> using namespace std; string s; int k; deque<int> d[3]; char c[3] = {'J', 'O', 'I'}; int last[3]; void rem(int x) { while((int)d[x].size() > k) d[x].pop_front(); } void update(int x, int y) { rem(x); for(int l = last[y] + 1; l < d[x].front(); l++) if(s[l] == c[y]) d[y].push_back(l); last[y] = d[x].front() - 1; } void add_letter(int x) { int l; for(l = 0; l < 2; l++) { if((int)d[l].size() < k) { if(s[x] == c[l]) { d[l].push_back(x); last[l] = x; } if(d[l].size() == 1) { while(l) { update(l,l-1); l--; } rem(0); } return; } } if(s[x] != c[2]) return; d[2].push_back(x); last[2] = x; update(2,1); update(1,0); rem(0); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, l, ans = -1; cin>>n>>k>>s; for(l = 0; l < n; l++) { add_letter(l); if((int)d[2].size() == k) { int cans = d[2].back() - d[0].front() + 1 - k * 3; if(ans == -1 || ans > cans) ans = cans; } } cout<<ans<<'\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...