QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#176471#6794. Sequence to SequencekkioWA 122ms8316kbC++172.1kb2023-09-11 17:54:382023-09-11 17:54:38

Judging History

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

  • [2023-09-11 17:54:38]
  • 评测
  • 测评结果:WA
  • 用时:122ms
  • 内存:8316kb
  • [2023-09-11 17:54:38]
  • 提交

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;
	}
}
bool flag=0;
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)
		{
			if(!flag)puts("-1");
			return;
		}
		else 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,ar[i]=s[i]-1;
		}
	al[n+1]=ar[n+1]=0;
	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("%d %d %d %d\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("!%d\n",dx);
		if(x<al[i])
		{
			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[i])
		{
			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("%d %d\n",x,y);
	}ans+=x+y;
	cnt++;
	if(cnt==334&&flag){for(int i=1;i<=n;i++)printf("%d %d\n",s[i],t[i]);}
	if(!flag)printf("%lld\n",ans/2); 
}
signed main()
{
	int T;
	scanf("%lld",&T);
	if(T>1000)flag=1;
	while(T--)solve();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 122ms
memory: 8316kb

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 6
0 0
2 9
5 9
0 0

result:

wrong answer 1st words differ - expected: '-1', found: '1'