QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#112469 | #6338. Chorus | Flamire | 0 | 8ms | 34992kb | C++17 | 2.0kb | 2023-06-11 22:08:53 | 2023-06-11 22:08:55 |
Judging History
answer
#include <bits/stdc++.h>
#define N 2000011
#define ll long long
#define lll __int128
using namespace std;
int n,k;char s[N];ll res;
namespace part1
{
set<int> A,B;
void main()
{
for(int i=1;i<=2*n;++i)if(s[i]=='A')A.insert(i);else B.insert(i);
for(int _=1;_<=n;++_)
{
int Ai=*A.begin(),Bi=*B.begin();
if(Ai<Bi){A.erase(Ai);B.erase(Bi);continue;}
res+=Ai-Bi;
B.insert(Ai);
B.erase(Bi);B.erase(Bi+1);
A.erase(Ai);
swap(s[Ai],s[Bi]);
}
}
};
struct ST{ll v;int w;ST(){v=1e18;w=0;}ST(ll _v,int _w){v=_v;w=_w;}};
ST dp[N];int h[N];ll sh[N];
bool operator<(ST a,ST b){return a.v<b.v||a.v==b.v&&a.w<b.w;}
ST operator+(ST a,ll b){return ST(a.v+b,a.w);}
int q[N],hl,hr;
ll A(int j){return 8*dp[j-h[j]].v+1ll*(j-h[j])*(j-h[j])+2*sh[j]-2ll*h[j]*h[j];}
void pushP(int x)
{
if(x&&x-h[x]==x-1-x[h-1])return;
while(hr-hl>1&&(lll)(q[x]-h[q[x]]-q[hr-1]+h[q[hr-1]])*(A(q[hr-1])-A(q[hr-2]))>(lll)(q[hr-1]-h[q[hr-1]]-q[hr-2]+h[q[hr-2]])*(A(q[x])-A(q[hr-1])))--hr;
q[hr++]=x;
}
int q2[N],hl2,hr2;
void solve(ll K)
{
for(int i=1;i<=2*n;++i)h[i]=h[i-1]+(s[i]=='A'?1:-1);
hl=hr=hl2=hr2=0;
for(int i=1;i<=2*n;++i)sh[i]=sh[i-1]+(h[i]+h[i-1]);
for(int i=0;i<=2*n;++i)dp[i]=ST();
dp[0]=ST(0,0);
int t=0;
for(int i=1;i<=2*n;++i)
{
if(i%2==0)
{
while(h[t]<i-t)++t,pushP(t-1);
while(hr-hl>1&&A(q[hl+1])-A(q[hl])<2ll*i*(q[hl+1]-h[q[hl+1]]-q[hl]+h[q[hl]]))++hl;
if(hl!=hr)
{
int J=q[hl];
dp[i]=min(dp[i],dp[J-h[J]]+(1ll*(i-(J-h[J]))*(i-(J-h[J]))-2ll*(sh[t]-sh[J])-2ll*h[J]*h[J]-2ll*h[t]*h[t]+8*K)/8);
}
while(hl2!=hr2&&q2[hl2]<t)++hl2;
if(hl2!=hr2)
{
int J=q2[hl2];
dp[i]=min(dp[i],dp[J-h[J]]+K);
}
++dp[i].w;
}
while(hl2!=hr2&&dp[i-h[i]]<dp[q2[hr2-1]-h[q2[hr2-1]]])--hr2;q2[hr2++]=i;
}
}
int main()
{
scanf("%d%d%s",&n,&k,s+1);
part1::main();
ll L=0,R=1e12,ans=0;
while(L<=R)
{
ll M=L+R>>1;
solve(M);
if(dp[2*n].w<=k)ans=M,R=M-1;else L=M+1;
}
solve(ans);
printf("%lld\n",res+dp[2*n].v-ans*k);return 0;
}
詳細信息
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 16
Accepted
time: 1ms
memory: 34756kb
input:
1 1 BA
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 8ms
memory: 34752kb
input:
7 5 ABBAAABBABABBA
output:
3
result:
ok 1 number(s): "3"
Test #3:
score: 0
Accepted
time: 1ms
memory: 34992kb
input:
10 3 BABBABAABAABBABBBAAA
output:
26
result:
ok 1 number(s): "26"
Test #4:
score: -16
Wrong Answer
time: 5ms
memory: 34764kb
input:
10 2 AAABBABABBAAABBBAABB
output:
20
result:
wrong answer 1st numbers differ - expected: '11', found: '20'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #1:
0%
Subtask #4:
score: 0
Skipped
Dependency #1:
0%
Subtask #5:
score: 0
Skipped
Dependency #1:
0%