Submission #761842

#TimeUsernameProblemLanguageResultExecution timeMemory
761842sysiaJJOOII 2 (JOI20_ho_t2)C++17
100 / 100
19 ms11692 KiB
//Sylwia Sapkowska #include <bits/stdc++.h> #pragma GCC optimize("O3", "unroll-loops") using namespace std; void __print(int x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << "'" << x << "'";} void __print(const char *x) {cerr << '"' << x << '"';} void __print(const string &x) {cerr << '"' << x << '"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifdef LOCAL #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #else #define debug(x...) #endif #define int long long typedef pair<int, int> T; const int oo = 1e18, oo2 = 1e9+7, K = 30; const int mod = 998244353; int change(char c){ if (c == 'J') return 0; if (c == 'O') return 1; return 2; } void solve(){ int n, k; cin >> n >> k; string s; cin >> s; s = "$" + s; vector nxt(n+1, vector<int>(3, -1)); for (int rep = 0; rep < 3; rep++){ int R = 1; int cnt = (change(s[1]) == rep); for (int L = 1; L <= n; L++){ while (R+1 <= n && cnt < k){ R++; cnt += (change(s[R]) == rep); } if (cnt == k){ nxt[L][rep] = R; } cnt -= (change(s[L]) == rep); } } for (int i = 1; i<=n; i++){ debug(i, nxt[i]); } int ans = oo; for (int i = 1; i<=n; i++){ if (nxt[i][0] == -1) break; int x = nxt[i][0]+1; if (x > n || nxt[x][1] == -1) continue; int y = nxt[x][1]+1; if (y > n || nxt[y][2] == -1) continue; int j = nxt[y][2]; debug(i, j); ans = min(ans, (j-i+1)); } if (ans == oo) cout << "-1\n"; else cout << ans - 3 * k << "\n"; } int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; //cin >> t; while (t--) solve(); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...