QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#176491#6794. Sequence to SequencekkioWA 129ms8308kbC++172.2kb2023-09-11 18:34:072023-09-11 18:34:08

Judging History

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

  • [2023-09-11 18:34:08]
  • 评测
  • 测评结果:WA
  • 用时:129ms
  • 内存:8308kb
  • [2023-09-11 18:34:07]
  • 提交

answer

#include <bits/stdc++.h>
#define int long long 
using namespace std;
const int maxn=1e5+10,inf=1e18;
int n;
int s[maxn],t[maxn];
int ql[maxn],h1,t1,qr[maxn],al[maxn],ar[maxn],h2,t2;
int x,y,ptr,ans;
int cnt=0;
inline void upd(int LEF)
{
	while(h1<=t1&&ql[h1]<LEF)h1++;
	while(h2<=t2&&qr[h2]<LEF)h2++;
	while(ptr<=n+1&&x>=al[ptr]&&x<=ar[ptr])
	{
		ptr++;
		while(h1<=t1&&al[ql[t1]]<=al[ptr])t1--;
		ql[++t1]=ptr;
		while(h2<=t2&&ar[qr[t2]]>=ar[ptr])t2--;
		qr[++t2]=ptr;
	}
}
void solve()
{
	scanf("%lld",&n);
	for(int i=1;i<=n;i++)scanf("%lld",&s[i]);
	for(int i=1;i<=n;i++)scanf("%lld",&t[i]);
	for(int i=1;i<=n;i++)
	{
		if(s[i]==0&&t[i]>0)
		{
			puts("-1");
			return;
		}
		if(s[i]>t[i])
		{
			if(t[i]==0)al[i]=s[i],ar[i]=inf;
			else al[i]=s[i]-t[i],ar[i]=s[i]-1;
		}
		else 
		{
			al[i]=0;
			if(t[i]!=0)ar[i]=s[i]-1;
			else ar[i]=inf;
		}
	}
	al[n+1]=ar[n+1]=0;
	//for(int i=1;i<=n;i++)
	//	printf("%lld %lld\n",al[i],ar[i]);
	ptr=1,ans=0;
	x=0,y=0; 
	h1=h2=1,t1=t2=0;
	while(h1<=t1&&al[ql[t1]]<=al[1])t1--;
	ql[++t1]=1;
	while(h2<=t2&&ar[qr[t2]]>=ar[1])t2--;
	qr[++t2]=1;
	for(int i=1;i<=n;i++)
	{
		//printf("%lld %lld %lld %lld\n",x,y,al[i],ar[i]);
		upd(i);int dx;
		if(t[i]==0){if(i==ptr)ans+=al[i]-x,x=al[i];upd(i);continue;}
		ans+=abs(dx=(s[i]-t[i]-x+y));
		//printf("!%lld %lld\n",dx,ptr);
		if(x<al[ptr])
		{
			if(dx<0)dx=0;
			while(ptr<=n+1&&x<al[ptr])
			{
				int lim;
				if((lim=(h2<=t2?ar[qr[h2]]:inf))<al[ptr]){x=min(lim,x+dx);break;}
				else if(x+dx<al[i]){if(i==ptr)ans+=(al[i]-x-dx)*2,x=al[i];else x+=dx;break;}
				else {dx-=al[ptr]-x;x=al[ptr];upd(i);}
			}
		}
		else if(x>al[ptr])
		{
			if(dx>0)dx=0;dx=-dx;
			while(ptr<=n+1&&x>ar[ptr])
			{
				int lim;
				if((lim=(h1<=t1?al[ql[h1]]:0))>ar[ptr]){x=max(lim,x-dx);break;}
				else if(x-dx>ar[i]){if(i==ptr)ans+=(x-dx-ar[i])*2,x=ar[i];else x-=dx;break;}
				else {dx-=x-ar[ptr];x=ar[ptr];upd(i);}
			}
		}
		y=t[i]-s[i]+x;
	//	printf("%lld %lld\n",x,y);
	}ans+=x+y;
	printf("%lld\n",ans/2); 
}
signed main()
{
//	freopen("data.in","r",stdin);
//	freopen("1.out","w",stdout);
	int T;
	scanf("%lld",&T);
	while(T--)solve();
	return 0;
}
/*
1
10
1 5 5 9 10 6 0 1 9 7 
4 1 3 6 8 5 0 7 4 0 

*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

2
5
1 1 1 1 1
2 0 2 0 2
7
3 1 2 3 2 1 4
2 0 0 0 0 0 2

output:

3
3

result:

ok 2 tokens

Test #2:

score: -100
Wrong Answer
time: 129ms
memory: 8308kb

input:

110121
5
0 0 0 0 0
1 4 5 4 1
5
1 0 0 0 0
0 6 8 6 1
5
2 0 0 0 0
4 4 1 3 6
5
3 0 0 0 0
5 1 1 7 6
5
4 0 0 0 0
6 8 7 0 8
5
5 0 0 0 0
5 9 7 7 5
5
6 0 0 0 0
9 2 2 8 0
5
7 0 0 0 0
9 4 7 0 9
5
8 0 0 0 0
6 7 3 7 5
5
9 0 0 0 0
4 0 9 1 4
5
0 1 0 0 0
0 6 6 3 0
5
1 1 0 0 0
3 4 3 4 9
5
2 1 0 0 0
0 4 0 1 4
5
3 1 0...

output:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
...

result:

wrong answer 1678th words differ - expected: '12', found: '11'