Submission #797271

#TimeUsernameProblemLanguageResultExecution timeMemory
797271n3rm1nJJOOII 2 (JOI20_ho_t2)C++17
100 / 100
8 ms3284 KiB
#include<bits/stdc++.h> #define endl '\n' using namespace std; const int MAXN = 2e5 + 10; void speed() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } int n, k; char s[MAXN]; void read() { cin >> n >> k; for (int i = 1; i <= n; ++ i) cin >> s[i]; } int dp0[MAXN], dp1[MAXN]; int cnt[MAXN]; void precompute() { deque < int > q; for (int i = n; i >= 1; -- i) { if(s[i] == 'J')q.push_back(i); if(q.size() > k)q.pop_front(); if(q.size() < k)dp0[i] = n+1; else dp0[i] = q.front(); } q.clear(); for (int i = 1; i <= n; ++ i) { if(s[i] == 'I')q.push_back(i); if(q.size() > k)q.pop_front(); if(q.size() < k)dp1[i] = 0; else dp1[i] = q.front(); } for (int i = 1; i <= n; ++ i) { cnt[i] = cnt[i-1]; if(s[i] == 'O')cnt[i] ++; } /* for (int i = 1; i <= n; ++ i) cout << dp0[i] << " "; cout << endl; for (int i = 1; i <= n; ++ i) cout << dp1[i] << " "; cout << endl; */ } bool check(int x) { for (int i = 0; i <= x; ++ i) /// how much we delete from the front { int lastj = dp0[i+1]; int firsti = dp1[n - (x - i)]; if(lastj > firsti)continue; if(cnt[firsti - 1] - cnt[lastj] >= k)return true; } return false; } void solve() { int left = 0, right = n - 3*k, mid, ans = -1; while(left <= right) { mid = (left + right) / 2; if(check(mid)) { ans = mid; left = mid + 1; } else right = mid - 1; } if(ans == -1)cout << -1 << endl; else cout << n - 3*k - ans << endl; } int main() { speed(); read(); precompute(); solve(); return 0; }

Compilation message (stderr)

ho_t2.cpp: In function 'void precompute()':
ho_t2.cpp:27:21: warning: comparison of integer expressions of different signedness: 'std::deque<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   27 |         if(q.size() > k)q.pop_front();
      |            ~~~~~~~~~^~~
ho_t2.cpp:29:21: warning: comparison of integer expressions of different signedness: 'std::deque<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   29 |         if(q.size() < k)dp0[i] = n+1;
      |            ~~~~~~~~~^~~
ho_t2.cpp:36:21: warning: comparison of integer expressions of different signedness: 'std::deque<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   36 |         if(q.size() > k)q.pop_front();
      |            ~~~~~~~~~^~~
ho_t2.cpp:38:21: warning: comparison of integer expressions of different signedness: 'std::deque<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   38 |         if(q.size() < k)dp1[i] = 0;
      |            ~~~~~~~~~^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...