# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
896068 |
2023-12-31T16:16:56 Z |
cadmiumsky |
Chorus (JOI23_chorus) |
C++17 |
|
1179 ms |
75892 KB |
#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;
using ll = long long;
using ld = long double;
#define int ll
#define sz(x) ((int)(x).size())
using pii = pair<int,int>;
using tii = tuple<int,int,int>;
const int nmax = 1e6 + 5;
const ll inf = 1e9 + 5;
int B[nmax];
ll sum_under[nmax], cnt_under[nmax];
ll sum_pref[nmax];
struct IWCnt {
ll val;
ll cnt;
IWCnt(): val(0), cnt(0) {;}
IWCnt(ll a, ll b): val(a), cnt(b) {;}
IWCnt operator + (const IWCnt& x) const { return IWCnt(val + x.val, cnt + x.cnt); }
IWCnt operator + (const ll& x) const { return IWCnt(val + x, cnt); }
IWCnt operator * (const ll& x) const { return IWCnt(val * x, cnt); }
};
namespace CHT {
struct Line {
ll m;
IWCnt b;
Line(ll a, IWCnt c): m(a), b(c) {;}
ll operator()(const ll& x) const { return m * x + b.val; }
};
bool bad(Line second, Line last, Line nv) {
return (nv.b.val - last.b.val) * (second.m - nv.m) < (nv.b.val - second.b.val) * (last.m - nv.m);
}
vector<Line> st;
int ptr;
void push(Line a) {
while(sz(st) > 1 && bad(rbegin(st)[1], rbegin(st)[0], a)) st.pop_back();
st.emplace_back(a);
}
pii query(int P) {
ptr = min(ptr, sz(st) - 1);
while(ptr + 1 < sz(st) && (st[ptr](P) > st[ptr + 1](P) || (st[ptr](P) == st[ptr + 1](P) && st[ptr + 1].b.cnt > st[ptr].b.cnt))) // ca gen, aia mari sunt teoretic primii intalniti, si ar trebui sa ii aflu pe ei pt a ma asigura ca-s pe panta corecta
ptr++;
return pii{st[ptr](P), st[ptr].b.cnt};
}
void clear() {
st.clear();
ptr = 0;
}
}
IWCnt dp[nmax];
int n, k;
bool check(ll lambda) {
int last_nv = n;
CHT::clear();
dp[n + 1].val = 0;
dp[n + 1].cnt = 0;
for(int i = n; i > 0; i--) {
while(last_nv >= B[i]) {
CHT::push(CHT::Line(last_nv, dp[last_nv + 1] + (-sum_under[last_nv] + cnt_under[last_nv] * last_nv + last_nv)));
last_nv--;
}
auto [C, tp] = CHT::query(-i);
dp[i].val = C + sum_pref[i - 1] + lambda;
dp[i].cnt = tp + 1;
}
//cerr << lambda << ' ' << dp[1].cnt << '\t' << dp[1].val << '\n';
return dp[1].cnt >= k;
}
signed main() {
cin.tie(0) -> sync_with_stdio(0);
cin >> n >> k;
int cnt[2] = {0, 0};
char ch;
for(int i = 0; i < 2 * n; i++) {
cin >> ch;
if(ch == 'A')
cnt[0]++;
else
B[++cnt[1]] = cnt[0];
//cerr << cnt[0] << '\n';
}
ll fixing = 0;
for(int i = 1; i <= n; i++) {
int target = max(B[i - 1], i);
fixing += max(0LL, target - B[i]);
B[i] += max(0LL, target - B[i]);
}
for(int i = 1; i <= n; i++)
cnt_under[B[i]]++,
sum_under[B[i]] += B[i],
sum_pref[i] = sum_pref[i - 1] + B[i];
for(int i = 1; i <= n; i++)
cnt_under[i] += cnt_under[i - 1],
sum_under[i] += sum_under[i - 1];
//for(int i = 1; i <= n; i++)
//cout << B[i] << ' ';
//cout << '\n';
ll lambda = 0;
for(int i = 40; i >= 0; i--) {
if(check(lambda + (1LL << i)))
lambda += (1LL << i);
}
check(lambda);
cout << dp[1].val - lambda * k + fixing << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
23128 KB |
Output is correct |
2 |
Correct |
3 ms |
23132 KB |
Output is correct |
3 |
Correct |
3 ms |
23132 KB |
Output is correct |
4 |
Correct |
3 ms |
23132 KB |
Output is correct |
5 |
Correct |
4 ms |
23132 KB |
Output is correct |
6 |
Correct |
3 ms |
23132 KB |
Output is correct |
7 |
Correct |
3 ms |
23132 KB |
Output is correct |
8 |
Correct |
4 ms |
23128 KB |
Output is correct |
9 |
Correct |
4 ms |
23132 KB |
Output is correct |
10 |
Correct |
4 ms |
23156 KB |
Output is correct |
11 |
Correct |
3 ms |
23128 KB |
Output is correct |
12 |
Correct |
3 ms |
23132 KB |
Output is correct |
13 |
Correct |
4 ms |
23184 KB |
Output is correct |
14 |
Correct |
4 ms |
23132 KB |
Output is correct |
15 |
Correct |
4 ms |
23168 KB |
Output is correct |
16 |
Correct |
3 ms |
23132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
23128 KB |
Output is correct |
2 |
Correct |
3 ms |
23132 KB |
Output is correct |
3 |
Correct |
3 ms |
23132 KB |
Output is correct |
4 |
Correct |
3 ms |
23132 KB |
Output is correct |
5 |
Correct |
4 ms |
23132 KB |
Output is correct |
6 |
Correct |
3 ms |
23132 KB |
Output is correct |
7 |
Correct |
3 ms |
23132 KB |
Output is correct |
8 |
Correct |
4 ms |
23128 KB |
Output is correct |
9 |
Correct |
4 ms |
23132 KB |
Output is correct |
10 |
Correct |
4 ms |
23156 KB |
Output is correct |
11 |
Correct |
3 ms |
23128 KB |
Output is correct |
12 |
Correct |
3 ms |
23132 KB |
Output is correct |
13 |
Correct |
4 ms |
23184 KB |
Output is correct |
14 |
Correct |
4 ms |
23132 KB |
Output is correct |
15 |
Correct |
4 ms |
23168 KB |
Output is correct |
16 |
Correct |
3 ms |
23132 KB |
Output is correct |
17 |
Correct |
3 ms |
23128 KB |
Output is correct |
18 |
Correct |
4 ms |
23132 KB |
Output is correct |
19 |
Correct |
4 ms |
23164 KB |
Output is correct |
20 |
Correct |
4 ms |
23128 KB |
Output is correct |
21 |
Correct |
4 ms |
23132 KB |
Output is correct |
22 |
Correct |
4 ms |
23132 KB |
Output is correct |
23 |
Correct |
4 ms |
23208 KB |
Output is correct |
24 |
Correct |
4 ms |
23132 KB |
Output is correct |
25 |
Correct |
4 ms |
23132 KB |
Output is correct |
26 |
Correct |
3 ms |
23132 KB |
Output is correct |
27 |
Correct |
4 ms |
23132 KB |
Output is correct |
28 |
Correct |
5 ms |
23144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
23128 KB |
Output is correct |
2 |
Correct |
3 ms |
23132 KB |
Output is correct |
3 |
Correct |
3 ms |
23132 KB |
Output is correct |
4 |
Correct |
3 ms |
23132 KB |
Output is correct |
5 |
Correct |
4 ms |
23132 KB |
Output is correct |
6 |
Correct |
3 ms |
23132 KB |
Output is correct |
7 |
Correct |
3 ms |
23132 KB |
Output is correct |
8 |
Correct |
4 ms |
23128 KB |
Output is correct |
9 |
Correct |
4 ms |
23132 KB |
Output is correct |
10 |
Correct |
4 ms |
23156 KB |
Output is correct |
11 |
Correct |
3 ms |
23128 KB |
Output is correct |
12 |
Correct |
3 ms |
23132 KB |
Output is correct |
13 |
Correct |
4 ms |
23184 KB |
Output is correct |
14 |
Correct |
4 ms |
23132 KB |
Output is correct |
15 |
Correct |
4 ms |
23168 KB |
Output is correct |
16 |
Correct |
3 ms |
23132 KB |
Output is correct |
17 |
Correct |
3 ms |
23128 KB |
Output is correct |
18 |
Correct |
4 ms |
23132 KB |
Output is correct |
19 |
Correct |
4 ms |
23164 KB |
Output is correct |
20 |
Correct |
4 ms |
23128 KB |
Output is correct |
21 |
Correct |
4 ms |
23132 KB |
Output is correct |
22 |
Correct |
4 ms |
23132 KB |
Output is correct |
23 |
Correct |
4 ms |
23208 KB |
Output is correct |
24 |
Correct |
4 ms |
23132 KB |
Output is correct |
25 |
Correct |
4 ms |
23132 KB |
Output is correct |
26 |
Correct |
3 ms |
23132 KB |
Output is correct |
27 |
Correct |
4 ms |
23132 KB |
Output is correct |
28 |
Correct |
5 ms |
23144 KB |
Output is correct |
29 |
Correct |
7 ms |
25436 KB |
Output is correct |
30 |
Correct |
8 ms |
25436 KB |
Output is correct |
31 |
Correct |
8 ms |
25456 KB |
Output is correct |
32 |
Correct |
6 ms |
25180 KB |
Output is correct |
33 |
Correct |
6 ms |
25252 KB |
Output is correct |
34 |
Correct |
7 ms |
25436 KB |
Output is correct |
35 |
Correct |
7 ms |
25452 KB |
Output is correct |
36 |
Correct |
9 ms |
25436 KB |
Output is correct |
37 |
Correct |
9 ms |
25436 KB |
Output is correct |
38 |
Correct |
6 ms |
25436 KB |
Output is correct |
39 |
Correct |
8 ms |
25436 KB |
Output is correct |
40 |
Correct |
7 ms |
25512 KB |
Output is correct |
41 |
Correct |
6 ms |
25432 KB |
Output is correct |
42 |
Correct |
6 ms |
25176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
23128 KB |
Output is correct |
2 |
Correct |
3 ms |
23132 KB |
Output is correct |
3 |
Correct |
3 ms |
23132 KB |
Output is correct |
4 |
Correct |
3 ms |
23132 KB |
Output is correct |
5 |
Correct |
4 ms |
23132 KB |
Output is correct |
6 |
Correct |
3 ms |
23132 KB |
Output is correct |
7 |
Correct |
3 ms |
23132 KB |
Output is correct |
8 |
Correct |
4 ms |
23128 KB |
Output is correct |
9 |
Correct |
4 ms |
23132 KB |
Output is correct |
10 |
Correct |
4 ms |
23156 KB |
Output is correct |
11 |
Correct |
3 ms |
23128 KB |
Output is correct |
12 |
Correct |
3 ms |
23132 KB |
Output is correct |
13 |
Correct |
4 ms |
23184 KB |
Output is correct |
14 |
Correct |
4 ms |
23132 KB |
Output is correct |
15 |
Correct |
4 ms |
23168 KB |
Output is correct |
16 |
Correct |
3 ms |
23132 KB |
Output is correct |
17 |
Correct |
3 ms |
23128 KB |
Output is correct |
18 |
Correct |
4 ms |
23132 KB |
Output is correct |
19 |
Correct |
4 ms |
23164 KB |
Output is correct |
20 |
Correct |
4 ms |
23128 KB |
Output is correct |
21 |
Correct |
4 ms |
23132 KB |
Output is correct |
22 |
Correct |
4 ms |
23132 KB |
Output is correct |
23 |
Correct |
4 ms |
23208 KB |
Output is correct |
24 |
Correct |
4 ms |
23132 KB |
Output is correct |
25 |
Correct |
4 ms |
23132 KB |
Output is correct |
26 |
Correct |
3 ms |
23132 KB |
Output is correct |
27 |
Correct |
4 ms |
23132 KB |
Output is correct |
28 |
Correct |
5 ms |
23144 KB |
Output is correct |
29 |
Correct |
7 ms |
25436 KB |
Output is correct |
30 |
Correct |
8 ms |
25436 KB |
Output is correct |
31 |
Correct |
8 ms |
25456 KB |
Output is correct |
32 |
Correct |
6 ms |
25180 KB |
Output is correct |
33 |
Correct |
6 ms |
25252 KB |
Output is correct |
34 |
Correct |
7 ms |
25436 KB |
Output is correct |
35 |
Correct |
7 ms |
25452 KB |
Output is correct |
36 |
Correct |
9 ms |
25436 KB |
Output is correct |
37 |
Correct |
9 ms |
25436 KB |
Output is correct |
38 |
Correct |
6 ms |
25436 KB |
Output is correct |
39 |
Correct |
8 ms |
25436 KB |
Output is correct |
40 |
Correct |
7 ms |
25512 KB |
Output is correct |
41 |
Correct |
6 ms |
25432 KB |
Output is correct |
42 |
Correct |
6 ms |
25176 KB |
Output is correct |
43 |
Correct |
63 ms |
26828 KB |
Output is correct |
44 |
Correct |
106 ms |
28612 KB |
Output is correct |
45 |
Correct |
108 ms |
29524 KB |
Output is correct |
46 |
Correct |
59 ms |
25444 KB |
Output is correct |
47 |
Correct |
58 ms |
27092 KB |
Output is correct |
48 |
Correct |
62 ms |
28624 KB |
Output is correct |
49 |
Correct |
63 ms |
29384 KB |
Output is correct |
50 |
Correct |
108 ms |
27092 KB |
Output is correct |
51 |
Correct |
114 ms |
27084 KB |
Output is correct |
52 |
Correct |
61 ms |
29392 KB |
Output is correct |
53 |
Correct |
60 ms |
28624 KB |
Output is correct |
54 |
Correct |
114 ms |
27092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
23128 KB |
Output is correct |
2 |
Correct |
3 ms |
23132 KB |
Output is correct |
3 |
Correct |
3 ms |
23132 KB |
Output is correct |
4 |
Correct |
3 ms |
23132 KB |
Output is correct |
5 |
Correct |
4 ms |
23132 KB |
Output is correct |
6 |
Correct |
3 ms |
23132 KB |
Output is correct |
7 |
Correct |
3 ms |
23132 KB |
Output is correct |
8 |
Correct |
4 ms |
23128 KB |
Output is correct |
9 |
Correct |
4 ms |
23132 KB |
Output is correct |
10 |
Correct |
4 ms |
23156 KB |
Output is correct |
11 |
Correct |
3 ms |
23128 KB |
Output is correct |
12 |
Correct |
3 ms |
23132 KB |
Output is correct |
13 |
Correct |
4 ms |
23184 KB |
Output is correct |
14 |
Correct |
4 ms |
23132 KB |
Output is correct |
15 |
Correct |
4 ms |
23168 KB |
Output is correct |
16 |
Correct |
3 ms |
23132 KB |
Output is correct |
17 |
Correct |
3 ms |
23128 KB |
Output is correct |
18 |
Correct |
4 ms |
23132 KB |
Output is correct |
19 |
Correct |
4 ms |
23164 KB |
Output is correct |
20 |
Correct |
4 ms |
23128 KB |
Output is correct |
21 |
Correct |
4 ms |
23132 KB |
Output is correct |
22 |
Correct |
4 ms |
23132 KB |
Output is correct |
23 |
Correct |
4 ms |
23208 KB |
Output is correct |
24 |
Correct |
4 ms |
23132 KB |
Output is correct |
25 |
Correct |
4 ms |
23132 KB |
Output is correct |
26 |
Correct |
3 ms |
23132 KB |
Output is correct |
27 |
Correct |
4 ms |
23132 KB |
Output is correct |
28 |
Correct |
5 ms |
23144 KB |
Output is correct |
29 |
Correct |
7 ms |
25436 KB |
Output is correct |
30 |
Correct |
8 ms |
25436 KB |
Output is correct |
31 |
Correct |
8 ms |
25456 KB |
Output is correct |
32 |
Correct |
6 ms |
25180 KB |
Output is correct |
33 |
Correct |
6 ms |
25252 KB |
Output is correct |
34 |
Correct |
7 ms |
25436 KB |
Output is correct |
35 |
Correct |
7 ms |
25452 KB |
Output is correct |
36 |
Correct |
9 ms |
25436 KB |
Output is correct |
37 |
Correct |
9 ms |
25436 KB |
Output is correct |
38 |
Correct |
6 ms |
25436 KB |
Output is correct |
39 |
Correct |
8 ms |
25436 KB |
Output is correct |
40 |
Correct |
7 ms |
25512 KB |
Output is correct |
41 |
Correct |
6 ms |
25432 KB |
Output is correct |
42 |
Correct |
6 ms |
25176 KB |
Output is correct |
43 |
Correct |
63 ms |
26828 KB |
Output is correct |
44 |
Correct |
106 ms |
28612 KB |
Output is correct |
45 |
Correct |
108 ms |
29524 KB |
Output is correct |
46 |
Correct |
59 ms |
25444 KB |
Output is correct |
47 |
Correct |
58 ms |
27092 KB |
Output is correct |
48 |
Correct |
62 ms |
28624 KB |
Output is correct |
49 |
Correct |
63 ms |
29384 KB |
Output is correct |
50 |
Correct |
108 ms |
27092 KB |
Output is correct |
51 |
Correct |
114 ms |
27084 KB |
Output is correct |
52 |
Correct |
61 ms |
29392 KB |
Output is correct |
53 |
Correct |
60 ms |
28624 KB |
Output is correct |
54 |
Correct |
114 ms |
27092 KB |
Output is correct |
55 |
Correct |
685 ms |
71468 KB |
Output is correct |
56 |
Correct |
828 ms |
75004 KB |
Output is correct |
57 |
Correct |
1080 ms |
75580 KB |
Output is correct |
58 |
Correct |
561 ms |
49416 KB |
Output is correct |
59 |
Correct |
545 ms |
63424 KB |
Output is correct |
60 |
Correct |
580 ms |
75472 KB |
Output is correct |
61 |
Correct |
570 ms |
74188 KB |
Output is correct |
62 |
Correct |
1120 ms |
61628 KB |
Output is correct |
63 |
Correct |
1179 ms |
62136 KB |
Output is correct |
64 |
Correct |
561 ms |
74416 KB |
Output is correct |
65 |
Correct |
582 ms |
75892 KB |
Output is correct |
66 |
Correct |
1179 ms |
75004 KB |
Output is correct |
67 |
Correct |
576 ms |
74164 KB |
Output is correct |