Submission #900157

#TimeUsernameProblemLanguageResultExecution timeMemory
900157andrei_iorgulescuJJOOII 2 (JOI20_ho_t2)C++14
100 / 100
10 ms5984 KiB
#include <bits/stdc++.h> using namespace std; int inf = 1e9; int n,k; char sir[200005]; int a[200005]; int next0[200005],next1[200005],next2[200005]; vector<int>pos0,pos1,pos2; int p[200005]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> k; for (int i = 1; i <= n; i++) cin >> sir[i]; for (int i = 1; i <= n; i++) { if (sir[i] == 'J') a[i] = 0; else if (sir[i] == 'O') a[i] = 1; else a[i] = 2; } next0[n] = next1[n] = next2[n] = n + 1; for (int i = n - 1; i >= 1; i--) { next0[i] = next0[i + 1]; next1[i] = next1[i + 1]; next2[i] = next2[i + 1]; if (a[i + 1] == 0) next0[i] = i + 1; if (a[i + 1] == 1) next1[i] = i + 1; if (a[i + 1] == 2) next2[i] = i + 1; } next0[n + 1] = next1[n + 1] = next2[n + 1] = n + 1; for (int i = 1; i <= n; i++) { if (a[i] == 0) pos0.push_back(i),p[i] = pos0.size() - 1; if (a[i] == 1) pos1.push_back(i),p[i] = pos1.size() - 1; if (a[i] == 2) pos2.push_back(i),p[i] = pos2.size() - 1; } int ans = 1e9; p[n + 1] = n + 1; for (int i = 1; i <= n; i++) { if (a[i] == 0) { if (p[i] + k - 1 < pos0.size()) { int x = pos0[p[i] + k - 1]; int x0 = next1[x]; if (p[x0] + k - 1 < pos1.size()) { int x1 = pos1[p[x0] + k - 1]; int x2 = next2[x1]; if (p[x2] + k - 1 < pos2.size()) { int x3 = pos2[p[x2] + k - 1]; ans = min(ans,x3 - i + 1 - 3 * k); } } } } } if (ans == inf) cout << -1; else cout << ans; return 0; }

Compilation message (stderr)

ho_t2.cpp: In function 'int main()':
ho_t2.cpp:60:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |             if (p[i] + k - 1 < pos0.size())
      |                 ~~~~~~~~~~~~~^~~~~~~~~~~~~
ho_t2.cpp:64:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   64 |                 if (p[x0] + k - 1 < pos1.size())
      |                     ~~~~~~~~~~~~~~^~~~~~~~~~~~~
ho_t2.cpp:68:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   68 |                     if (p[x2] + k - 1 < pos2.size())
      |                         ~~~~~~~~~~~~~~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...