# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
880517 |
2023-11-29T15:10:43 Z |
cpptowin |
JJOOII 2 (JOI20_ho_t2) |
C++17 |
|
1 ms |
4700 KB |
#include <bits/stdc++.h>
#define fo(i, d, c) for (int i = d; i <= c; i++)
#define fod(i, c, d) for (int i = c; i >= d; i--)
#define maxn 100010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout << "\n";
#define int long long
#define inf 1000000000
#define pii pair<int, int>
#define vii vector<pii>
#define lb(x) x & -x
#define bit(i, j) ((i >> j) & 1)
#define offbit(i, j) (i ^ (1 << j))
#define onbit(i, j) (i | (1 << j))
#define vi vector<int>
template <typename T1, typename T2>
bool minimize(T1 &a, T2 b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
bool maximize(T1 &a, T2 b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
using namespace std;
string s;
int cnt[maxn][3];
int n;
int ans = inf;
int k;
int f[maxn][3];
main()
{
#define name "TASK"
if (fopen(name ".inp", "r"))
{
freopen(name ".inp", "r", stdin);
freopen(name ".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k >> s;
s = ' ' + s;
memset(f, -1, sizeof f);
fo(i, 1, n)
{
fo(j, 0, 2) cnt[i][j] = cnt[i - 1][j];
if (s[i] == 'J')
cnt[i][0]++;
if (s[i] == 'O')
cnt[i][1]++;
if (s[i] == 'I')
cnt[i][2]++;
if (f[cnt[i][0]][0] == -1)
f[cnt[i][0]][0] = i;
if (f[cnt[i][1]][1] == -1)
f[cnt[i][1]][1] = i;
if (f[cnt[i][2]][2] == -1)
f[cnt[i][2]][2] = i;
}
// fo(i, 1, n) cout << cnt[i][0] << ' ' << cnt[i][1] << ' ' << cnt[i][2] << "\n";
fo(i, 1, n)
{
int val = cnt[i][0];
int pos = f[val + k - 1][0];
if (pos == -1)
break;
int now = 0;
now += cnt[pos][1] - cnt[i - 1][1] + cnt[pos][2] - cnt[i - 1][2];
// cout << now << ' ' << pos << "\n";
val = cnt[pos + 1][1];
int pre = pos;
pos = f[val + k - 1][1];
if (pos == -1)
break;
now += cnt[pos][2] - cnt[pre][2] + cnt[pos][0] - cnt[pre][0];
// cout << now << ' ' << pos << "\n";
pre = pos + 1;
val = cnt[pos + 1][2];
pos = f[val + k - 1][2];
if (pos == -1)
break;
now += cnt[pos][1] - cnt[pre][1] + cnt[pos][0] - cnt[pre][0];
// cout << now << ' ' << pos << "\n";
ans = min(ans, now);
// cout << now << "\n";
}
cout << (ans == inf ? -1 : ans);
}
Compilation message
ho_t2.cpp:46:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
46 | main()
| ^~~~
ho_t2.cpp: In function 'int main()':
ho_t2.cpp:51:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
51 | freopen(name ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
ho_t2.cpp:52:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
52 | freopen(name ".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |