Submission #1037774

# Submission time Handle Problem Language Result Execution time Memory
1037774 2024-07-29T08:13:16 Z ㅇㅇ(223.39)(#11011) Chorus (JOI23_chorus) C++17
100 / 100
1977 ms 59184 KB
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
using ll=long long;
using db=double;
using pll=array<ll,2>;
using tll=array<ll,3>;
const ll inf=1e18;
const int N=1000005;
int n,k,a[N],m,f[N];
string s;
ll S[N];
pll dp[N];
ll base,ans;
struct ConvexHullTrick{
	tll L[N];
	int sz,p;
	db cross(tll a,tll b){
		return (db)(b[1]-a[1])/(a[0]-b[0]);
	}
	void add_line(tll l){
		while(sz>=2&&cross(L[sz-1],L[sz])>=cross(L[sz],l)) sz--;
		L[++sz]=l;
		p=min(p,sz);
	}
	pll qry(ll x){
		if(!sz) return {-1,-1};
		while(p<sz&&cross(L[p],L[p+1])<=x) p++;
		return {L[p][0]*x+L[p][1],L[p][2]};
	}
}CHT;
pll Do(ll C){
	dp[0]={0,0};
	deque<int> dq;
	dq.push_back(0);
	CHT.sz=0; CHT.p=1;
	for(int i=1;i<=n;i++){
		dp[i]={inf,inf};
		while(dq.size()&&dq.front()<=f[i]-2) dq.pop_front();
		if(dq.size()) dp[i]={dp[dq.front()][0]+C,dp[dq.front()][1]+1};
		for(int j=max(0,f[i-1]-1);j<=f[i]-2;j++) CHT.add_line({-j,dp[j][0]-(ll)j*(j+1)/2+S[j]+C,dp[j][1]+1});
		pll Q=CHT.qry(i);
		if(Q[1]!=-1) dp[i]=min(dp[i],{Q[0]+(ll)(f[i]-1)*i+(ll)(f[i]-1)*f[i]/2-S[f[i]-1],Q[1]});
		while(dq.size()&&dp[dq.back()]>=dp[i]) dq.pop_back();
		dq.push_back(i);
	}
	return dp[n];
}
int main(){
	ios::sync_with_stdio(false); cin.tie(0);
	cin>>n>>k>>s;
	for(int i=1;i<=2*n;i++) if(s[i-1]=='B') a[++m]=i;
	for(int i=1;i<=n;i++) if(a[i]<2*i){
		base+=2*i-a[i];
		a[i]=2*i;
	}
	for(int i=1;i<=n;i++) S[i]=S[i-1]+a[i];
	f[1]=1;
	for(int i=2;i<=n;i++){
		f[i]=f[i-1];
		while(i+f[i]-a[f[i]]>0) f[i]++;
	}
	ll L=-1e12,R=1e12;
	while(L<=R){
		ll M=(L+R)>>1;
		auto [dp,cnt]=Do(M);
		ans=max(ans,dp-k*M);
		if(cnt<=k) R=M-1;
		else L=M+1;
	}
	cout<<base+ans;
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 0 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 0 ms 2396 KB Output is correct
11 Correct 0 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 0 ms 2392 KB Output is correct
15 Correct 0 ms 2396 KB Output is correct
16 Correct 0 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 0 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 0 ms 2396 KB Output is correct
11 Correct 0 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 0 ms 2392 KB Output is correct
15 Correct 0 ms 2396 KB Output is correct
16 Correct 0 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2392 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 1 ms 2396 KB Output is correct
23 Correct 1 ms 2568 KB Output is correct
24 Correct 2 ms 2396 KB Output is correct
25 Correct 2 ms 2396 KB Output is correct
26 Correct 2 ms 2396 KB Output is correct
27 Correct 1 ms 2396 KB Output is correct
28 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 0 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 0 ms 2396 KB Output is correct
11 Correct 0 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 0 ms 2392 KB Output is correct
15 Correct 0 ms 2396 KB Output is correct
16 Correct 0 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2392 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 1 ms 2396 KB Output is correct
23 Correct 1 ms 2568 KB Output is correct
24 Correct 2 ms 2396 KB Output is correct
25 Correct 2 ms 2396 KB Output is correct
26 Correct 2 ms 2396 KB Output is correct
27 Correct 1 ms 2396 KB Output is correct
28 Correct 1 ms 2396 KB Output is correct
29 Correct 7 ms 4700 KB Output is correct
30 Correct 6 ms 4788 KB Output is correct
31 Correct 6 ms 4700 KB Output is correct
32 Correct 6 ms 4648 KB Output is correct
33 Correct 6 ms 4700 KB Output is correct
34 Correct 6 ms 4700 KB Output is correct
35 Correct 6 ms 4700 KB Output is correct
36 Correct 9 ms 4788 KB Output is correct
37 Correct 9 ms 4796 KB Output is correct
38 Correct 6 ms 4696 KB Output is correct
39 Correct 8 ms 4820 KB Output is correct
40 Correct 6 ms 4696 KB Output is correct
41 Correct 6 ms 4700 KB Output is correct
42 Correct 5 ms 4776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 0 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 0 ms 2396 KB Output is correct
11 Correct 0 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 0 ms 2392 KB Output is correct
15 Correct 0 ms 2396 KB Output is correct
16 Correct 0 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2392 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 1 ms 2396 KB Output is correct
23 Correct 1 ms 2568 KB Output is correct
24 Correct 2 ms 2396 KB Output is correct
25 Correct 2 ms 2396 KB Output is correct
26 Correct 2 ms 2396 KB Output is correct
27 Correct 1 ms 2396 KB Output is correct
28 Correct 1 ms 2396 KB Output is correct
29 Correct 7 ms 4700 KB Output is correct
30 Correct 6 ms 4788 KB Output is correct
31 Correct 6 ms 4700 KB Output is correct
32 Correct 6 ms 4648 KB Output is correct
33 Correct 6 ms 4700 KB Output is correct
34 Correct 6 ms 4700 KB Output is correct
35 Correct 6 ms 4700 KB Output is correct
36 Correct 9 ms 4788 KB Output is correct
37 Correct 9 ms 4796 KB Output is correct
38 Correct 6 ms 4696 KB Output is correct
39 Correct 8 ms 4820 KB Output is correct
40 Correct 6 ms 4696 KB Output is correct
41 Correct 6 ms 4700 KB Output is correct
42 Correct 5 ms 4776 KB Output is correct
43 Correct 72 ms 8028 KB Output is correct
44 Correct 176 ms 10728 KB Output is correct
45 Correct 128 ms 10728 KB Output is correct
46 Correct 95 ms 12772 KB Output is correct
47 Correct 105 ms 12772 KB Output is correct
48 Correct 138 ms 12792 KB Output is correct
49 Correct 100 ms 12772 KB Output is correct
50 Correct 171 ms 10724 KB Output is correct
51 Correct 172 ms 10724 KB Output is correct
52 Correct 104 ms 12776 KB Output is correct
53 Correct 127 ms 12776 KB Output is correct
54 Correct 179 ms 10728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 0 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 0 ms 2396 KB Output is correct
11 Correct 0 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 0 ms 2392 KB Output is correct
15 Correct 0 ms 2396 KB Output is correct
16 Correct 0 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2392 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 1 ms 2396 KB Output is correct
23 Correct 1 ms 2568 KB Output is correct
24 Correct 2 ms 2396 KB Output is correct
25 Correct 2 ms 2396 KB Output is correct
26 Correct 2 ms 2396 KB Output is correct
27 Correct 1 ms 2396 KB Output is correct
28 Correct 1 ms 2396 KB Output is correct
29 Correct 7 ms 4700 KB Output is correct
30 Correct 6 ms 4788 KB Output is correct
31 Correct 6 ms 4700 KB Output is correct
32 Correct 6 ms 4648 KB Output is correct
33 Correct 6 ms 4700 KB Output is correct
34 Correct 6 ms 4700 KB Output is correct
35 Correct 6 ms 4700 KB Output is correct
36 Correct 9 ms 4788 KB Output is correct
37 Correct 9 ms 4796 KB Output is correct
38 Correct 6 ms 4696 KB Output is correct
39 Correct 8 ms 4820 KB Output is correct
40 Correct 6 ms 4696 KB Output is correct
41 Correct 6 ms 4700 KB Output is correct
42 Correct 5 ms 4776 KB Output is correct
43 Correct 72 ms 8028 KB Output is correct
44 Correct 176 ms 10728 KB Output is correct
45 Correct 128 ms 10728 KB Output is correct
46 Correct 95 ms 12772 KB Output is correct
47 Correct 105 ms 12772 KB Output is correct
48 Correct 138 ms 12792 KB Output is correct
49 Correct 100 ms 12772 KB Output is correct
50 Correct 171 ms 10724 KB Output is correct
51 Correct 172 ms 10724 KB Output is correct
52 Correct 104 ms 12776 KB Output is correct
53 Correct 127 ms 12776 KB Output is correct
54 Correct 179 ms 10728 KB Output is correct
55 Correct 771 ms 34540 KB Output is correct
56 Correct 1127 ms 52468 KB Output is correct
57 Correct 1121 ms 46320 KB Output is correct
58 Correct 981 ms 57076 KB Output is correct
59 Correct 1101 ms 59124 KB Output is correct
60 Correct 1137 ms 59044 KB Output is correct
61 Correct 1016 ms 59120 KB Output is correct
62 Correct 1921 ms 48272 KB Output is correct
63 Correct 1977 ms 48116 KB Output is correct
64 Correct 1122 ms 59184 KB Output is correct
65 Correct 1029 ms 59124 KB Output is correct
66 Correct 1933 ms 50164 KB Output is correct
67 Correct 1044 ms 59128 KB Output is correct