QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#113309#2211. IOI Problem RevisedyoungsystemAC ✓1046ms72052kbC++205.5kb2023-06-16 23:49:202023-06-16 23:49:23

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-16 23:49:23]
  • 评测
  • 测评结果:AC
  • 用时:1046ms
  • 内存:72052kb
  • [2023-06-16 23:49:20]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read()
{
	int n=0,f=1,ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		n=n*10+ch-'0';
		ch=getchar();
	}
	return n*f;
}
int a[800005],qzh[800005];
int na[800005];
int zy[800005];
int dp[800005],sl[800005];
int n,k,len;
int findzy(int l,int r)
{
	assert(l<=r);
	int mid=((l+r+1)>>1);
	return dp[l]+na[mid]*(mid-l)-qzh[mid]+qzh[l]+qzh[r]-qzh[mid]-na[mid]*(r-mid);
}
bool bijiao(int x,int y,int r)
{
	int sth1=findzy(x,r),sth2=findzy(y,r);
	if(sth1<sth2)return true;
	if(sth1==sth2&&sl[x]<sl[y])return true;
	return false;
}
int gz[400005],cnt;
void findgz(int x)
{
	if(x==0)return;
	gz[++cnt]=x;
	findgz(zy[x]);
}
int gz1[400005],cnt1;
int gz2[400005],cnt2;
int que[800005],zl[800005],zr[800005],ql,qr;
int solve(int mid)
{
	dp[0]=0;
	sl[0]=0;
	ql=qr=1;
	que[1]=0;
	zl[1]=1;
	zr[1]=n;
	for(int i=1;i<=n;i++)
	{
		//printf("vis:%lld\n",i);
		if(i>zr[ql])ql++;
		if(ql<=qr&&i==zl[ql])zl[ql]++;
		int nh=findzy(que[ql],i)+mid;
		dp[i]=nh;
		sl[i]=sl[que[ql]]+1;
		zy[i]=que[ql];
		if(i==n)break;
		while(qr>=ql&&bijiao(i,que[qr],zl[qr]))qr--;
		if(qr<ql)
		{
			que[++qr]=i;
			zl[qr]=i+1;
			zr[qr]=n;
			continue;
		}
		int l=zl[qr],r=n,mid;
		while(l<=r)
		{
			mid=(l+r)>>1;
			if(bijiao(i,que[qr],mid))r=mid-1;
			else l=mid+1;
		}
		zr[qr]=r;
		if(l<=n)
		{
			que[++qr]=i;
			zl[qr]=l;
			zr[qr]=n;
		}
	}
	cnt=0;
	findgz(zy[n]);
	gz[++cnt]=n;
	return cnt;
}
int qans=1000000000000000000LL;
vector<int>sc;
int dy[800005];
int pos[800005];
int findzy2(int l,int r)
{
	int sth=dp[l];
	l=pos[l];
	if(l>=r)r+=n;
	int mid=((l+r+1)>>1);
	return sth+na[mid]*(mid-l)-qzh[mid]+qzh[l]+qzh[r]-qzh[mid]-na[mid]*(r-mid);
}
bool bijiao2(int x,int y,int r)
{
	int sth1=findzy2(x,r),sth2=findzy2(y,r);
	if(sth1<=sth2)return true;
	return false;
}
void fenzhi(int l,int r,vector<int> vl,vector<int> vr)
{
	if(l>r)return;
	//printf("???%lld %lld\n",l,r);
	//for(int i=0;i<vl.size();i++)printf("%lld %lld\n",vl[i],vr[i]);
	int mid=((l+r)>>1);
	ql=1;
	qr=1;
	que[1]=1;
	dp[1]=0;
	pos[1]=mid;
	zl[1]=vl[0]; 
	zr[1]=vr[0];
	int tmp=1,tl=0,tr=0;
	for(int i=0;i<vl.size();i++)
	{
		tl=tmp+1;
		for(int j=vl[i];j<=vr[i];j++)
		{
			if(zr[ql]<j)ql++;
			dp[++tmp]=findzy2(que[ql],j);
			pos[tmp]=j;
			//printf("%lld %lld %lld %lld\n",que[ql],tmp,pos[tmp],dp[tmp]);
			zy[tmp]=que[ql];
		}
		tr=tmp;
		if(i==vl.size()-1)break;
		ql=1;
		qr=1;
		int xj=vl[i+1],sj=vr[i+1];
		que[1]=tl;
		zl[1]=xj;
		zr[1]=sj;
		for(int j=tl+1;j<=tr;j++)
		{
			while(qr>=ql&&bijiao2(j,que[qr],zl[qr]))qr--;
			if(qr<ql)
			{
				que[++qr]=j;
				zl[qr]=xj;
				zr[qr]=sj;
				continue;
			}
			int l=zl[qr],r=sj,nmid;
			while(l<=r)
			{
				nmid=(l+r)>>1;
				if(bijiao2(j,que[qr],nmid))r=nmid-1;
				else l=nmid+1;
			}
			zr[qr]=r;
			if(l<=sj)
			{
				que[++qr]=j;
				zl[qr]=l;
				zr[qr]=sj;
			}
		}
		//for(int j=ql;j<=qr;j++)printf("%d %d %d\n",que[j],zl[j],zr[j]);
	}
	int minn=1000000000000000000LL,mpos=0;
	for(int i=tl;i<=tr;i++)
	{
		if(findzy2(i,mid)<minn)
		{
			minn=findzy2(i,mid);
			mpos=i;
		}
	}
	vector<int>sy;
	int sth=mpos;
	while(sth!=1)
	{
		assert(zy[sth]!=sth);
		sy.push_back(pos[sth]);
		sth=zy[sth];
	}
	reverse(sy.begin(),sy.end());
	if(minn<qans)
	{
		qans=minn;
		sc=sy;
		sc.push_back(mid);
	}
	fenzhi(l,mid-1,vl,sy);
	fenzhi(mid+1,r,sy,vr);
}
bool xz[200005];
signed main()
{
	n=read();
	k=read();
	len=read();
	for(int i=1;i<=n;i++)a[i]=read();
	for(int i=1;i<=n;i++)na[i]=a[i]-a[1];
	for(int i=1;i<=n;i++)na[i+n]=na[i]+len;
	for(int i=1;i<=2*n;i++)qzh[i]=qzh[i-1]+na[i];
	int l=0,r=n*len/k,mid;
	while(l<=r)
	{
		mid=((l+r)>>1);
		if(solve(mid)<=k)r=mid-1;
		else l=mid+1;
	}
	//printf("!!!\n");
	solve(r);
	cnt1=cnt;
	for(int i=1;i<=cnt1;i++)gz1[i]=gz[i];
	sort(gz1+1,gz1+cnt1+1);
	solve(l);
	cnt2=cnt; 
	for(int i=1;i<=cnt2;i++)gz2[i]=gz[i];
	sort(gz2+1,gz2+cnt2+1);
	if(cnt2==k)
	{
		cnt=k;
		for(int i=1;i<=cnt;i++)gz[i]=gz2[i];
	}
	else
	{
		int gb=k-cnt2;
		if(cnt2>=k||cnt1<=k)
		{
			printf("no\n");
			return 0;
		}
		bool flag=false;
		for(int i=1;i<=cnt2;i++)
		{
			if(gz2[i]<=gz1[i+gb]&&gz2[i+1]>=gz1[i+1+gb])
			{
				cnt=0;
				for(int j=1;j<=i+gb;j++)gz[++cnt]=gz1[j];
				for(int j=i+1;j<=cnt2;j++)gz[++cnt]=gz2[j];
				flag=true;
				break;
			}
		}
		assert(flag);
	}
	sort(gz+1,gz+cnt+1);
	/*printf("%d\n",cnt);
	for(int i=1;i<=cnt;i++)printf("%d ",gz[i]);
	printf("\n");
	printf("%d\n",cnt1);
	for(int i=1;i<=cnt1;i++)printf("%d ",gz1[i]);
	printf("\n");
	printf("%d\n",cnt2);
	for(int i=1;i<=cnt2;i++)printf("%d ",gz2[i]);
	printf("\n");*/
	//return 0;
	int minn=n+1,mpos=0;
	for(int i=0;i<=cnt-1;i++)
	{
		if(gz[i+1]-gz[i]<minn)
		{
			minn=gz[i+1]-gz[i];
			mpos=i;
		}
	}
	//printf("%d\n",mpos);
	int nl=max(gz[mpos],1LL),nr=gz[mpos+1];
	vector<int>vl,vr;
	for(int i=(mpos+1)%cnt;i!=mpos;i=(i+1)%cnt)
	{
		vl.push_back(max(1LL,gz[i]));
		vr.push_back(gz[i+1]);
	}
	//printf("orz\n");
	//printf("%lld %lld\n",nl,nr);
	//for(int i=0;i<vl.size();i++)printf("%lld %lld\n",vl[i],vr[i]);
	//printf("\n");
	fenzhi(nl,nr,vl,vr);
	printf("%lld\n",qans);
	sort(sc.begin(),sc.end());
	for(int i=0;i+1<sc.size();i++)
	{
		//printf("%lld %lld\n",sc[i],sc[i+1]);
		xz[(sc[i]+sc[i+1]+1)/2]=true;
	}
	int sth=(sc[sc.size()-1]+sc[0]+n+1)/2;
	xz[(sth-1)%n+1]=true;
	for(int i=1;i<=n;i++)if(xz[i])printf("%lld ",a[i]);
	return 0;
}

这程序好像有点Bug,我给组数据试试?

Details

Test #1:

score: 100
Accepted
time: 0ms
memory: 26184kb

Test #2:

score: 0
Accepted
time: 1ms
memory: 26176kb

Test #3:

score: 0
Accepted
time: 1ms
memory: 26156kb

Test #4:

score: 0
Accepted
time: 1ms
memory: 26180kb

Test #5:

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

Test #6:

score: 0
Accepted
time: 1ms
memory: 26172kb

Test #7:

score: 0
Accepted
time: 2ms
memory: 26332kb

Test #8:

score: 0
Accepted
time: 3ms
memory: 26272kb

Test #9:

score: 0
Accepted
time: 3ms
memory: 26368kb

Test #10:

score: 0
Accepted
time: 3ms
memory: 26384kb

Test #11:

score: 0
Accepted
time: 101ms
memory: 32984kb

Test #12:

score: 0
Accepted
time: 67ms
memory: 30316kb

Test #13:

score: 0
Accepted
time: 103ms
memory: 33108kb

Test #14:

score: 0
Accepted
time: 69ms
memory: 33432kb

Test #15:

score: 0
Accepted
time: 95ms
memory: 27328kb

Test #16:

score: 0
Accepted
time: 490ms
memory: 58768kb

Test #17:

score: 0
Accepted
time: 577ms
memory: 52368kb

Test #18:

score: 0
Accepted
time: 546ms
memory: 50060kb

Test #19:

score: 0
Accepted
time: 799ms
memory: 43012kb

Test #20:

score: 0
Accepted
time: 492ms
memory: 56028kb

Test #21:

score: 0
Accepted
time: 483ms
memory: 65004kb

Test #22:

score: 0
Accepted
time: 467ms
memory: 65912kb

Test #23:

score: 0
Accepted
time: 659ms
memory: 49708kb

Test #24:

score: 0
Accepted
time: 561ms
memory: 52156kb

Test #25:

score: 0
Accepted
time: 519ms
memory: 66112kb

Test #26:

score: 0
Accepted
time: 646ms
memory: 49724kb

Test #27:

score: 0
Accepted
time: 623ms
memory: 48232kb

Test #28:

score: 0
Accepted
time: 519ms
memory: 59328kb

Test #29:

score: 0
Accepted
time: 715ms
memory: 55224kb

Test #30:

score: 0
Accepted
time: 402ms
memory: 61684kb

Test #31:

score: 0
Accepted
time: 505ms
memory: 63852kb

Test #32:

score: 0
Accepted
time: 494ms
memory: 67388kb

Test #33:

score: 0
Accepted
time: 455ms
memory: 56032kb

Test #34:

score: 0
Accepted
time: 492ms
memory: 59832kb

Test #35:

score: 0
Accepted
time: 488ms
memory: 57668kb

Test #36:

score: 0
Accepted
time: 443ms
memory: 60712kb

Test #37:

score: 0
Accepted
time: 514ms
memory: 63288kb

Test #38:

score: 0
Accepted
time: 480ms
memory: 65628kb

Test #39:

score: 0
Accepted
time: 812ms
memory: 40304kb

Test #40:

score: 0
Accepted
time: 428ms
memory: 58260kb

Test #41:

score: 0
Accepted
time: 789ms
memory: 40940kb

Test #42:

score: 0
Accepted
time: 474ms
memory: 57940kb

Test #43:

score: 0
Accepted
time: 782ms
memory: 45452kb

Test #44:

score: 0
Accepted
time: 462ms
memory: 72052kb

Test #45:

score: 0
Accepted
time: 432ms
memory: 66484kb

Test #46:

score: 0
Accepted
time: 464ms
memory: 63964kb

Test #47:

score: 0
Accepted
time: 603ms
memory: 58660kb

Test #48:

score: 0
Accepted
time: 722ms
memory: 45808kb

Test #49:

score: 0
Accepted
time: 778ms
memory: 50568kb

Test #50:

score: 0
Accepted
time: 528ms
memory: 58064kb

Test #51:

score: 0
Accepted
time: 504ms
memory: 51440kb

Test #52:

score: 0
Accepted
time: 496ms
memory: 54196kb

Test #53:

score: 0
Accepted
time: 679ms
memory: 49564kb

Test #54:

score: 0
Accepted
time: 705ms
memory: 58568kb

Test #55:

score: 0
Accepted
time: 475ms
memory: 58664kb

Test #56:

score: 0
Accepted
time: 771ms
memory: 53908kb

Test #57:

score: 0
Accepted
time: 658ms
memory: 55588kb

Test #58:

score: 0
Accepted
time: 722ms
memory: 49876kb

Test #59:

score: 0
Accepted
time: 530ms
memory: 57500kb

Test #60:

score: 0
Accepted
time: 423ms
memory: 69392kb

Test #61:

score: 0
Accepted
time: 701ms
memory: 58152kb

Test #62:

score: 0
Accepted
time: 537ms
memory: 50884kb

Test #63:

score: 0
Accepted
time: 846ms
memory: 43624kb

Test #64:

score: 0
Accepted
time: 430ms
memory: 60548kb

Test #65:

score: 0
Accepted
time: 237ms
memory: 44732kb

Test #66:

score: 0
Accepted
time: 220ms
memory: 54940kb

Test #67:

score: 0
Accepted
time: 205ms
memory: 54296kb

Test #68:

score: 0
Accepted
time: 218ms
memory: 49000kb

Test #69:

score: 0
Accepted
time: 1039ms
memory: 37544kb

Test #70:

score: 0
Accepted
time: 1022ms
memory: 43088kb

Test #71:

score: 0
Accepted
time: 1046ms
memory: 49600kb

Test #72:

score: 0
Accepted
time: 1042ms
memory: 39304kb

Extra Test:

score: 0
Extra Test Passed