QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#178721#6338. Choruslrher#0 2ms10052kbC++143.3kb2023-09-14 11:42:062024-07-04 01:57:06

Judging History

你现在查看的是最新测评结果

  • [2024-07-04 01:57:06]
  • 评测
  • 测评结果:0
  • 用时:2ms
  • 内存:10052kb
  • [2023-09-14 11:42:06]
  • 提交

answer

#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<stack>
#include<queue>
#include<deque>
#include<string>
#include<bitset>
#include<vector>
#include<cstdio>
#include<random>
#include<complex>
#include<cstdlib>
#include<climits>
#include<iomanip>
#include<cstring>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<unordered_map>
using namespace std;
// #define getchar()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
// char buf[1<<21],*p1=buf,*p2=buf;
const long long _base=107374183;
inline int read()
{
    char c=getchar();int x=0;bool f=0;
    for(;!isdigit(c);c=getchar())f^=!(c^45);
    for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
    if(f)x=-x;return x;
}
inline void write(unsigned long long x)
{
    int tot=(x==0),writetemp[30]={0};
    unsigned long long t;
	while(x) t=x/10,writetemp[++tot]=x-(t<<1)-(t<<3),x=t;
    for(int i=tot;i>=1;i--) putchar(writetemp[i]+'0');
	return ;
}
priority_queue<pair<long long,int>,vector<pair<long long,int> >,greater<pair<long long,int> > > q;
char s[1000010];
int n,k,num,nowk,nowsum,lstsum,sum[1000010];
int tot,pos[1000010];
long long ans;
bool vis[1000010];
struct block
{
    int l,r;
    int numa,numb;
    long long sum;
}a[1000010];
struct list
{
    int lst,nxt;
}l[1000010];
long long calc(block a,block b)
{
    return b.sum+(b.l-a.r)*b.numa;
}
block merge(block a,block b)
{
    block ans;
    ans.numa=a.numa+b.numa;
    ans.numb=a.numb+b.numb;
    ans.l=a.l,ans.r=a.r+b.numa;
    ans.sum=a.sum+a.numb*b.numa;
    return ans;
}
void get_new(int x)
{
    if(l[x].lst) q.push(make_pair(calc(a[l[x].lst],a[x]),x));
    return ;
}
void del(int x)
{
    vis[x]=1,num--;
    a[l[x].lst]=merge(a[l[x].lst],a[x]);
    if(l[x].lst)
    {
        l[l[x].lst].nxt=l[x].nxt;
        get_new(l[x].lst);
    }
    if(l[x].nxt<=nowk)
    {
        l[l[x].nxt].lst=l[x].lst;
        get_new(l[x].nxt);
    }
    return ;
}
int main()
{
    scanf("%d%d%s",&n,&k,s+1),n<<=1;
    for(int i=1;i<=n;i++) if(s[i]=='A') pos[++tot]=i;
    for(int i=1;i<=n;i++)
    {
        if(s[i]=='A') nowsum++;
        else
        {
            if(nowsum==0)
            {
                int nowpos=lower_bound(pos+1,pos+tot+1,i)-pos;
                swap(s[i],s[pos[nowpos]]),ans+=pos[nowpos]-i,pos[nowpos]=i,nowsum++;
            }
            else nowsum--;
        }
    }
    // printf("%s\n%d\n",s+1,ans);
    for(int i=1;i<=n;i++)
    {
        if(s[i]=='A')
        {
            if(nowsum==0) a[++nowk].l=i;
            nowsum++;
        }
        else
        {
            if(lstsum==0) a[nowk].r=i,lstsum=nowsum,nowsum=0;
            lstsum--;
        }
    }
    // printf("%d\n",nowk);
    if(nowk<=k) return printf("%lld\n",ans),0;
    for(int i=1;i<=nowk;i++)
    {
        l[i].lst=i-1,l[i].nxt=i+1;
        for(int j=a[i].l;j<a[i].r;j++) if(s[j]=='A') a[i].numa++,a[i].sum+=a[i].numb; else a[i].numb++;
    }
    for(int i=2;i<=nowk;i++) q.push(make_pair(calc(a[i-1],a[i]),i));
    num=nowk;
    while(num>k)
    {
        long long cost=q.top().first;
        int id=q.top().second;q.pop();
        if(l[id].lst&&calc(a[l[id].lst],a[id])==cost&&!vis[id]) ans+=cost,del(id);
    }
    printf("%lld\n",ans);
    return 0;
}
/*
AABB AABB AABB AABB AABB
*/

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 16
Accepted
time: 2ms
memory: 10052kb

input:

1 1
BA

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: 0
Accepted
time: 0ms
memory: 7988kb

input:

7 5
ABBAAABBABABBA

output:

3

result:

ok 1 number(s): "3"

Test #3:

score: -16
Wrong Answer
time: 1ms
memory: 7936kb

input:

10 3
BABBABAABAABBABBBAAA

output:

27

result:

wrong answer 1st numbers differ - expected: '26', found: '27'

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%