95求助
查看原帖
95求助
150508
zhanghapi楼主2020/11/14 11:57

改了好多遍,现在剩第5个点re,数组已经开大很多倍了,也没找到其他的问题,求大佬帮看,思路是从题解上找的

#include<queue>
#include<cstdio>
#include<iostream>
#define int long long
using namespace std;
const int maxn=10000000+10;
int read(){
	int f=1,res=0;char ch=getchar();
	while(!isdigit(ch)){
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while(isdigit(ch)){
		res=(res<<3)+(res<<1)+(ch^48);
		ch=getchar();
	}
	return f*res;
}
int t,n,a[maxn],k,ans;
signed main()
{
//	freopen("snakes4.in","r",stdin);
//	freopen("ans.out","w",stdout);
	t=read();
	for(int w=1;w<=t;w++)
	{
		if(w==1){
			n=read();
			for(int i=1;i<=n;i++)
				a[i]=read();
		} 
		else{
			k=read();
			for(int i=1;i<=k;i++){
				int b,c;
				b=read();
				c=read();
				a[b]=c;
			}
		}
		if(n==3){
			if(a[3]>=a[2]+a[1])
				cout<<1<<endl;
			else cout<<3<<endl;
			continue;
		}
		if(n==2){
			cout<<1<<endl;
			continue;
		}
		deque<int> q1;
		deque<int> q2;
		for(int i=1;i<=n;i++){
			q1.push_back(a[i]);
		}
		while(true){
			int cur=0,cur2=0;
			int flag=0;
			if((q2.empty())||q1.back()>q2.back()){
				cur=q1.back();
				flag=1;
				q1.pop_back();
			}
			else{
				cur=q2.back();
				flag=2;
				q2.pop_back();
			}
			cur2=q1.front();
			q1.pop_front();
			int sum=cur-cur2;
			if(sum>=q1.front()){
				q2.push_front(sum);
			}
			else{
				if(flag==1) q1.push_back(cur);
				else q1.push_back(cur);
				q1.push_front(cur2);
				break;
			}
		}
		int cur=0,cur2=q1.front();
		int t=0;
		ans=q1.size()+q2.size();
		q1.pop_front();
		while(true)
		{
			int flag=0;
			if(q2.empty()||q1.back()>q2.back()){
				cur=q1.back();
				flag=1;
				q1.pop_back();
			}
			else{
				cur=q2.back();
				flag=2;
				q2.pop_back();
			}
			cur2=cur-cur2;
			if(cur2>q1.front()){
				ans-=((t+1)%2);
				break;
			}
			else if(q1.size()+q2.size()==2){
				ans-=((t+1)%2);
				break;
			}
			t++;
		}
		printf("%d\n",ans);
	}
	return 0;
}
2020/11/14 11:57
加载中...