#include <bits/stdc++.h>
#define MAX 1000005
#define MOD (ll)(1048573)
#define INF (ll)(1e12)
#define inf (int)(1e9)
#define eps (double)(1e-8)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
int tt, N, K;
string str;
int cnt[MAX], pos[MAX], can[MAX], q[MAX], k[MAX];
ll dp[MAX], sum[MAX];
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
inline int readChar();
template<class T = int> inline T readInt();
template<class T> inline void writeInt(T x, char end = 0);
inline void writeChar(int x);
inline void writeWord(const char *s);
static const int buf_size = 1 << 18;
inline int getChar(){
#ifndef LOCAL
static char buf[buf_size];
static int len = 0, pos = 0;
if(pos == len) pos = 0, len = fread(buf, 1, buf_size, stdin);
if(pos == len) return -1;
return buf[pos++];
#endif
}
inline int readChar(){
#ifndef LOCAL
int c = getChar();
while(c <= 32) c = getChar();
return c;
#else
char c; cin >> c; return c;
#endif
}
template <class T>
inline T readInt(){
#ifndef LOCAL
int s = 1, c = readChar();
T x = 0;
if(c == '-') s = -1, c = getChar();
while('0' <= c && c <= '9') x = x * 10 + c - '0', c = getChar();
return s == 1 ? x : -x;
#else
T x; cin >> x; return x;
#endif
}
static int write_pos = 0;
static char write_buf[buf_size];
inline void writeChar(int x){
if(write_pos == buf_size) fwrite(write_buf, 1, buf_size, stdout), write_pos = 0;
write_buf[write_pos++] = x;
}
template <class T>
inline void writeInt(T x, char end){
if(x < 0) writeChar('-'), x = -x;
char s[24]; int n = 0;
while(x || !n) s[n++] = '0' + x % 10, x /= 10;
while(n--) writeChar(s[n]);
if(end) writeChar(end);
}
inline void writeWord(const char *s){
while(*s) writeChar(*s++);
}
struct Flusher{
~Flusher(){ if(write_pos) fwrite(write_buf, 1, write_pos, stdout), write_pos = 0; }
}flusher;
ll cost(int i, int j) {
return max(sum[j] - sum[pos[i - 1] - 1] - (ll)(j - pos[i - 1] + 1) * (i - 1), 0ll);
}
ll func(int i, int j) {
return dp[i] + cost(i + 1, j);
}
int cross(int i, int j) {
int lo = j , hi = N + 1;
while(lo + 1 < hi) {
int mid = (lo + hi) >> 1;
if(func(i, mid) <= func(j, mid)) lo = mid;
else hi = mid;
}
return lo;
}
void monotoneQueue(ll lambda) {
int str = 0, ed = 1;
q[0] = 0; can[0] = N;
for(int i = 1; i <= N; ++i) {
while(can[str] < i) ++str;
dp[i] = func(q[str], i) + lambda; k[i] = k[q[str]] + 1;
while(str + 1 < ed && can[ed - 2] >= cross(q[ed - 1], i)) --ed;
can[ed - 1] = cross(q[ed - 1], i); q[ed] = i; can[ed] = N;
++ed;
}
return;
}
ll alienTrick() {
ll lo = -1, hi = INF;
ll ans = 0;
while(lo + 1 < hi) {
ll mid = (lo + hi) >> 1;
monotoneQueue(mid);
ans = max(ans, dp[N] - mid * K);
if(k[N] >= K) lo = mid;
else hi = mid;
}
return ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(NULL);
N = readInt();
K = readInt();
for(int i = 0; i < 2 * N; ++i) str += readChar();
for(int i = 0, tmp = 0, p = 0; i < 2 * N; ++i) {
if(str[i] == 'A') cnt[++p] = tmp;
else ++tmp;
}
for(int i = 0, j = 1; i <= N; ++i) {
while(j <= N && cnt[j] < i) ++j;
pos[i] = max(i + 1, j);
}
for(int i = 1; i <= N; ++i) sum[i] = sum[i - 1] + cnt[i];
writeInt(alienTrick(), ' ');
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
344 KB |
Output is correct |
18 |
Correct |
2 ms |
348 KB |
Output is correct |
19 |
Correct |
2 ms |
348 KB |
Output is correct |
20 |
Correct |
2 ms |
344 KB |
Output is correct |
21 |
Correct |
2 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
348 KB |
Output is correct |
24 |
Correct |
2 ms |
348 KB |
Output is correct |
25 |
Correct |
2 ms |
348 KB |
Output is correct |
26 |
Correct |
2 ms |
348 KB |
Output is correct |
27 |
Correct |
2 ms |
348 KB |
Output is correct |
28 |
Correct |
2 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
344 KB |
Output is correct |
18 |
Correct |
2 ms |
348 KB |
Output is correct |
19 |
Correct |
2 ms |
348 KB |
Output is correct |
20 |
Correct |
2 ms |
344 KB |
Output is correct |
21 |
Correct |
2 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
348 KB |
Output is correct |
24 |
Correct |
2 ms |
348 KB |
Output is correct |
25 |
Correct |
2 ms |
348 KB |
Output is correct |
26 |
Correct |
2 ms |
348 KB |
Output is correct |
27 |
Correct |
2 ms |
348 KB |
Output is correct |
28 |
Correct |
2 ms |
348 KB |
Output is correct |
29 |
Correct |
19 ms |
604 KB |
Output is correct |
30 |
Correct |
21 ms |
604 KB |
Output is correct |
31 |
Correct |
20 ms |
604 KB |
Output is correct |
32 |
Correct |
16 ms |
692 KB |
Output is correct |
33 |
Correct |
17 ms |
696 KB |
Output is correct |
34 |
Correct |
17 ms |
724 KB |
Output is correct |
35 |
Correct |
17 ms |
604 KB |
Output is correct |
36 |
Correct |
22 ms |
600 KB |
Output is correct |
37 |
Correct |
20 ms |
712 KB |
Output is correct |
38 |
Correct |
18 ms |
604 KB |
Output is correct |
39 |
Correct |
23 ms |
604 KB |
Output is correct |
40 |
Correct |
19 ms |
600 KB |
Output is correct |
41 |
Correct |
19 ms |
708 KB |
Output is correct |
42 |
Correct |
16 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
344 KB |
Output is correct |
18 |
Correct |
2 ms |
348 KB |
Output is correct |
19 |
Correct |
2 ms |
348 KB |
Output is correct |
20 |
Correct |
2 ms |
344 KB |
Output is correct |
21 |
Correct |
2 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
348 KB |
Output is correct |
24 |
Correct |
2 ms |
348 KB |
Output is correct |
25 |
Correct |
2 ms |
348 KB |
Output is correct |
26 |
Correct |
2 ms |
348 KB |
Output is correct |
27 |
Correct |
2 ms |
348 KB |
Output is correct |
28 |
Correct |
2 ms |
348 KB |
Output is correct |
29 |
Correct |
19 ms |
604 KB |
Output is correct |
30 |
Correct |
21 ms |
604 KB |
Output is correct |
31 |
Correct |
20 ms |
604 KB |
Output is correct |
32 |
Correct |
16 ms |
692 KB |
Output is correct |
33 |
Correct |
17 ms |
696 KB |
Output is correct |
34 |
Correct |
17 ms |
724 KB |
Output is correct |
35 |
Correct |
17 ms |
604 KB |
Output is correct |
36 |
Correct |
22 ms |
600 KB |
Output is correct |
37 |
Correct |
20 ms |
712 KB |
Output is correct |
38 |
Correct |
18 ms |
604 KB |
Output is correct |
39 |
Correct |
23 ms |
604 KB |
Output is correct |
40 |
Correct |
19 ms |
600 KB |
Output is correct |
41 |
Correct |
19 ms |
708 KB |
Output is correct |
42 |
Correct |
16 ms |
600 KB |
Output is correct |
43 |
Correct |
326 ms |
2692 KB |
Output is correct |
44 |
Correct |
642 ms |
4068 KB |
Output is correct |
45 |
Correct |
558 ms |
3956 KB |
Output is correct |
46 |
Correct |
437 ms |
3568 KB |
Output is correct |
47 |
Correct |
462 ms |
3872 KB |
Output is correct |
48 |
Correct |
463 ms |
4328 KB |
Output is correct |
49 |
Correct |
437 ms |
4552 KB |
Output is correct |
50 |
Correct |
640 ms |
4028 KB |
Output is correct |
51 |
Correct |
585 ms |
4072 KB |
Output is correct |
52 |
Correct |
444 ms |
4508 KB |
Output is correct |
53 |
Correct |
433 ms |
4304 KB |
Output is correct |
54 |
Correct |
558 ms |
4076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
344 KB |
Output is correct |
18 |
Correct |
2 ms |
348 KB |
Output is correct |
19 |
Correct |
2 ms |
348 KB |
Output is correct |
20 |
Correct |
2 ms |
344 KB |
Output is correct |
21 |
Correct |
2 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
348 KB |
Output is correct |
24 |
Correct |
2 ms |
348 KB |
Output is correct |
25 |
Correct |
2 ms |
348 KB |
Output is correct |
26 |
Correct |
2 ms |
348 KB |
Output is correct |
27 |
Correct |
2 ms |
348 KB |
Output is correct |
28 |
Correct |
2 ms |
348 KB |
Output is correct |
29 |
Correct |
19 ms |
604 KB |
Output is correct |
30 |
Correct |
21 ms |
604 KB |
Output is correct |
31 |
Correct |
20 ms |
604 KB |
Output is correct |
32 |
Correct |
16 ms |
692 KB |
Output is correct |
33 |
Correct |
17 ms |
696 KB |
Output is correct |
34 |
Correct |
17 ms |
724 KB |
Output is correct |
35 |
Correct |
17 ms |
604 KB |
Output is correct |
36 |
Correct |
22 ms |
600 KB |
Output is correct |
37 |
Correct |
20 ms |
712 KB |
Output is correct |
38 |
Correct |
18 ms |
604 KB |
Output is correct |
39 |
Correct |
23 ms |
604 KB |
Output is correct |
40 |
Correct |
19 ms |
600 KB |
Output is correct |
41 |
Correct |
19 ms |
708 KB |
Output is correct |
42 |
Correct |
16 ms |
600 KB |
Output is correct |
43 |
Correct |
326 ms |
2692 KB |
Output is correct |
44 |
Correct |
642 ms |
4068 KB |
Output is correct |
45 |
Correct |
558 ms |
3956 KB |
Output is correct |
46 |
Correct |
437 ms |
3568 KB |
Output is correct |
47 |
Correct |
462 ms |
3872 KB |
Output is correct |
48 |
Correct |
463 ms |
4328 KB |
Output is correct |
49 |
Correct |
437 ms |
4552 KB |
Output is correct |
50 |
Correct |
640 ms |
4028 KB |
Output is correct |
51 |
Correct |
585 ms |
4072 KB |
Output is correct |
52 |
Correct |
444 ms |
4508 KB |
Output is correct |
53 |
Correct |
433 ms |
4304 KB |
Output is correct |
54 |
Correct |
558 ms |
4076 KB |
Output is correct |
55 |
Correct |
4432 ms |
22344 KB |
Output is correct |
56 |
Correct |
6399 ms |
36308 KB |
Output is correct |
57 |
Execution timed out |
7078 ms |
34080 KB |
Time limit exceeded |
58 |
Halted |
0 ms |
0 KB |
- |