QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#71391#4273. Good Gamezhouhuanyi0 198ms51536kbC++113.7kb2023-01-09 21:40:012023-01-09 21:40:03

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-01-09 21:40:03]
  • 评测
  • 测评结果:0
  • 用时:198ms
  • 内存:51536kb
  • [2023-01-09 21:40:01]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<set>
#define N 2000000
#define inf 1e9
using namespace std;
int read()
{
    char c=0;
    int sum=0;
    while (c<'0'||c>'9') c=getchar();
    while ('0'<=c&&c<='9') sum=sum*10+c-'0',c=getchar();
    return sum;
}
struct reads
{
    int num,data;
    bool operator < (const reads &t)const
    {
	return (data!=t.data)?data>t.data:num<t.num;
    }
};
reads st[N+1];
set<reads>s;
int n,mid,length,leng,tong[N+1],pv[N+1],nt[N+1],rt[N+1],L[N+1],R[N+1],sc[N+1];
bool used[N+1],used2[N+1];
char c[N+1];
int lowbit(int x)
{
    return x&(-x);
}
void add(int x,int d)
{
    for (;x<=length;x+=lowbit(x)) sc[x]+=d;
    return;
}
int query(int x)
{
    int res=0;
    for (;x>=1;x-=lowbit(x)) res+=sc[x];
    return res;
}
void del(int x)
{
    int s1=pv[x],s2=nt[x];
    nt[s1]=s2,pv[s2]=s1;
    if (tong[x]==1)
    {
	s.erase((reads){rt[x],R[rt[x]]-L[rt[x]]+1});
	if (L[rt[x]]==x) L[rt[x]]++;
	else R[rt[x]]--;
	if (L[rt[x]]<=R[rt[x]]) s.insert((reads){rt[x],R[rt[x]]-L[rt[x]]+1});
    }
    return;
}
void solve(int x)
{
    int s1,s2;
    for (int i=1;i<=(tong[x]>>1);++i) st[++leng]=(reads){query(x-1)+1,2};
    if (tong[x]&1) st[leng].data++;
    if (pv[x]&&nt[x]!=length+1)
    {
	s1=pv[x],s2=nt[x],add(s1,-tong[s1]),add(s2,-tong[s2]),add(x,tong[s1]+tong[s2]-tong[x]),tong[x]=tong[s1]+tong[s2],del(s1),del(s2);
	if (mid<=s1) mid=pv[mid];
	else if (mid>=s2) mid=nt[mid];
	nt[s1]=pv[s1]=nt[s2]=pv[s2]=-1;
    }
    else add(x,-tong[x]),del(x),nt[x]=pv[x]=-1;
    return;
}
void solver(int l,int r)
{
    int top,lst=0,cnt=0;
    for (int i=1;i<=length;++i) sc[i]=0;
    for (int i=l;i<=r;++i) add(i,tong[i]);
    for (int i=l;i<=r;++i)
    {
	if ((i==l||tong[i-1]>=2)&&tong[i]==1) lst=i;
	rt[i]=lst;
	if ((i==r||tong[i+1]>=2)&&tong[i]==1) L[lst]=lst,R[lst]=i,s.insert((reads){lst,R[lst]-L[lst]+1});
    }
    for (int i=l;i<=r-1;++i) nt[i]=i+1,pv[i+1]=i;
    nt[0]=l,pv[l]=0,nt[r]=length+1,pv[length+1]=r,mid=(l+r)>>1;
    if (tong[mid]>=2)
    {
	for (int i=1;i<=((r-l+2)>>1);++i) solve(mid);
	return;
    }
    while (!s.empty())
    {
	top=(*s.begin()).num;
	if (pv[L[top]]!=-1&&pv[pv[L[top]]]&&tong[pv[L[top]]]>=2) solve(pv[L[top]]);
	else solve(nt[R[top]]);
	if (tong[mid]>=2)
	{
	    for (int i=l;i<=r;++i)
		if (nt[i]!=-1)
		    cnt++;
	    for (int i=1;i<=((cnt+1)>>1);++i) solve(mid);
	    return;
	}
    }
    for (int i=l;i<=r;++i)
	if (nt[i]!=-1)
	    solve(i);
    return;
}
int main()
{
    int lst=0,res=0;
    n=read();
    for (int i=1;i<=n;++i) cin>>c[i];
    for (int i=1;i<=n;++i)
	if (i==n||c[i]!=c[i+1])
	    tong[++length]=i-lst,lst=i;
    for (int i=1;i<=length;++i)
    {
	if ((i==1||tong[i-1]>=2)&&tong[i]==1) lst=i;
        if (i!=1&&tong[i-1]==1&&tong[i]>=2) res=max(res,((i-lst)<<1)+(lst!=1)+1);
	if (tong[i]==1) used[i]=(max(res,((i-lst+1)<<1)+(lst!=1))<=i);
	else used[i]=(res<=i);
    }
    res=0;
    for (int i=length;i>=1;--i)
    {
	if ((i==length||tong[i+1]>=2)&&tong[i]==1) lst=i;
	if (i!=length-1&&tong[i+1]==1&&tong[i]>=2) res=max(res,((lst-i)<<1)+(lst!=length)+1);
	if (tong[i]==1) used2[i]=(max(res,((lst-i+1)<<1)+(lst!=length))<=length-i+1);
	else used2[i]=(res<=length-i+1);
    }
    if (length&1)
    {
	if (used[length])
	{
	    puts("Yes"),solver(1,length);
	    printf("%d\n",leng);
	    for (int i=1;i<=leng;++i) printf("%d %d\n",st[i].num,st[i].data);
	}
	else puts("No");
    }
    else
    {
	for (int i=1;i<=length;i+=2)
	    if (used[i]&&used2[i+1])
	    {
		puts("Yes"),solver(1,i),solver(i+1,length);
		printf("%d\n",leng);
		for (int i=1;i<=leng;++i) printf("%d %d\n",st[i].num,st[i].data);
		return 0;
	    }
	puts("No");
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 3ms
memory: 22128kb

input:

9
ABAABBBAA

output:

Yes
4
3 2
2 2
2 2
1 3

result:

wrong output format Expected integer, but "Yes" found

Subtask #2:

score: 0
Wrong Answer

Test #51:

score: 0
Wrong Answer
time: 3ms
memory: 9636kb

input:

299
ABABABABABABABABABABABABABABABABABBAABBAABBAABBAAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBBAABBAABBAABBAABBAABBAABBAABBAABBABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABA...

output:

No

result:

wrong output format Expected integer, but "No" found

Subtask #3:

score: 0
Wrong Answer

Test #102:

score: 0
Wrong Answer
time: 0ms
memory: 24184kb

input:

5998
BABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB...

output:

Yes
2998
1996 2
1996 2
3991 2
3991 2
3991 2
3991 3
1995 2
3988 2
1994 2
3985 2
1993 2
3982 2
1992 2
3979 2
1991 2
3976 2
1990 2
3973 2
1989 2
3970 2
1988 2
3967 2
1987 2
3964 2
1986 2
3961 2
1985 2
3958 2
1984 2
3955 2
1983 2
3952 2
1982 2
3949 2
1981 2
3946 2
1980 2
3943 2
1979 2
3940 2
1978 2
3937...

result:

wrong output format Expected integer, but "Yes" found

Subtask #4:

score: 0
Wrong Answer

Test #153:

score: 0
Wrong Answer
time: 198ms
memory: 51536kb

input:

999997
ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABA...

output:

Yes
499998
333328 2
333328 2
333328 2
666655 2
666655 2
666655 2
666655 2
666655 2
333327 2
666652 2
333326 2
666649 2
333325 2
666646 2
333324 2
666643 2
333323 2
666640 2
333322 2
666637 2
333321 2
666634 2
333320 2
666631 2
333319 2
666628 2
333318 2
666625 2
333317 2
666622 2
333316 2
666619 2
3...

result:

wrong output format Expected integer, but "Yes" found