Submission #1037772

# Submission time Handle Problem Language Result Execution time Memory
1037772 2024-07-29T08:12:37 Z ㅇㅇ(223.39)(#11011) Chorus (JOI23_chorus) C++17
87 / 100
1004 ms 59124 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=-1e10,R=1e10;
	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 0 ms 2392 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 0 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 0 ms 2396 KB Output is correct
7 Correct 0 ms 2396 KB Output is correct
8 Correct 0 ms 2396 KB Output is correct
9 Correct 1 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 0 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 0 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 2392 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 0 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 0 ms 2396 KB Output is correct
7 Correct 0 ms 2396 KB Output is correct
8 Correct 0 ms 2396 KB Output is correct
9 Correct 1 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 0 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 0 ms 2396 KB Output is correct
16 Correct 1 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 2392 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 1 ms 2392 KB Output is correct
25 Correct 1 ms 2396 KB Output is correct
26 Correct 1 ms 2396 KB Output is correct
27 Correct 1 ms 2396 KB Output is correct
28 Correct 1 ms 2560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 2392 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 0 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 0 ms 2396 KB Output is correct
7 Correct 0 ms 2396 KB Output is correct
8 Correct 0 ms 2396 KB Output is correct
9 Correct 1 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 0 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 0 ms 2396 KB Output is correct
16 Correct 1 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 2392 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 1 ms 2392 KB Output is correct
25 Correct 1 ms 2396 KB Output is correct
26 Correct 1 ms 2396 KB Output is correct
27 Correct 1 ms 2396 KB Output is correct
28 Correct 1 ms 2560 KB Output is correct
29 Correct 6 ms 4700 KB Output is correct
30 Correct 6 ms 4700 KB Output is correct
31 Correct 5 ms 4700 KB Output is correct
32 Correct 5 ms 4852 KB Output is correct
33 Correct 6 ms 4844 KB Output is correct
34 Correct 6 ms 4696 KB Output is correct
35 Correct 5 ms 4840 KB Output is correct
36 Correct 8 ms 4784 KB Output is correct
37 Correct 8 ms 4700 KB Output is correct
38 Correct 6 ms 4700 KB Output is correct
39 Correct 7 ms 4700 KB Output is correct
40 Correct 5 ms 4700 KB Output is correct
41 Correct 5 ms 4700 KB Output is correct
42 Correct 4 ms 4700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 2392 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 0 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 0 ms 2396 KB Output is correct
7 Correct 0 ms 2396 KB Output is correct
8 Correct 0 ms 2396 KB Output is correct
9 Correct 1 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 0 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 0 ms 2396 KB Output is correct
16 Correct 1 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 2392 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 1 ms 2392 KB Output is correct
25 Correct 1 ms 2396 KB Output is correct
26 Correct 1 ms 2396 KB Output is correct
27 Correct 1 ms 2396 KB Output is correct
28 Correct 1 ms 2560 KB Output is correct
29 Correct 6 ms 4700 KB Output is correct
30 Correct 6 ms 4700 KB Output is correct
31 Correct 5 ms 4700 KB Output is correct
32 Correct 5 ms 4852 KB Output is correct
33 Correct 6 ms 4844 KB Output is correct
34 Correct 6 ms 4696 KB Output is correct
35 Correct 5 ms 4840 KB Output is correct
36 Correct 8 ms 4784 KB Output is correct
37 Correct 8 ms 4700 KB Output is correct
38 Correct 6 ms 4700 KB Output is correct
39 Correct 7 ms 4700 KB Output is correct
40 Correct 5 ms 4700 KB Output is correct
41 Correct 5 ms 4700 KB Output is correct
42 Correct 4 ms 4700 KB Output is correct
43 Correct 68 ms 8276 KB Output is correct
44 Correct 150 ms 10984 KB Output is correct
45 Correct 103 ms 10984 KB Output is correct
46 Correct 104 ms 13032 KB Output is correct
47 Correct 92 ms 13032 KB Output is correct
48 Correct 94 ms 13032 KB Output is correct
49 Correct 88 ms 13032 KB Output is correct
50 Correct 166 ms 10984 KB Output is correct
51 Correct 171 ms 11036 KB Output is correct
52 Correct 88 ms 13032 KB Output is correct
53 Correct 95 ms 13028 KB Output is correct
54 Correct 157 ms 10896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 2392 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 0 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 0 ms 2396 KB Output is correct
7 Correct 0 ms 2396 KB Output is correct
8 Correct 0 ms 2396 KB Output is correct
9 Correct 1 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 0 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 0 ms 2396 KB Output is correct
16 Correct 1 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 2392 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 1 ms 2392 KB Output is correct
25 Correct 1 ms 2396 KB Output is correct
26 Correct 1 ms 2396 KB Output is correct
27 Correct 1 ms 2396 KB Output is correct
28 Correct 1 ms 2560 KB Output is correct
29 Correct 6 ms 4700 KB Output is correct
30 Correct 6 ms 4700 KB Output is correct
31 Correct 5 ms 4700 KB Output is correct
32 Correct 5 ms 4852 KB Output is correct
33 Correct 6 ms 4844 KB Output is correct
34 Correct 6 ms 4696 KB Output is correct
35 Correct 5 ms 4840 KB Output is correct
36 Correct 8 ms 4784 KB Output is correct
37 Correct 8 ms 4700 KB Output is correct
38 Correct 6 ms 4700 KB Output is correct
39 Correct 7 ms 4700 KB Output is correct
40 Correct 5 ms 4700 KB Output is correct
41 Correct 5 ms 4700 KB Output is correct
42 Correct 4 ms 4700 KB Output is correct
43 Correct 68 ms 8276 KB Output is correct
44 Correct 150 ms 10984 KB Output is correct
45 Correct 103 ms 10984 KB Output is correct
46 Correct 104 ms 13032 KB Output is correct
47 Correct 92 ms 13032 KB Output is correct
48 Correct 94 ms 13032 KB Output is correct
49 Correct 88 ms 13032 KB Output is correct
50 Correct 166 ms 10984 KB Output is correct
51 Correct 171 ms 11036 KB Output is correct
52 Correct 88 ms 13032 KB Output is correct
53 Correct 95 ms 13028 KB Output is correct
54 Correct 157 ms 10896 KB Output is correct
55 Correct 681 ms 36040 KB Output is correct
56 Correct 895 ms 54260 KB Output is correct
57 Correct 1004 ms 48104 KB Output is correct
58 Incorrect 987 ms 59124 KB Output isn't correct
59 Halted 0 ms 0 KB -