【MnZn求助】最后两个点挂了
  • 板块P4940 Portal2
  • 楼主SCWLine
  • 当前回复2
  • 已保存回复2
  • 发布时间2021/12/7 14:11
  • 上次更新2023/11/3 22:44:44
查看原帖
【MnZn求助】最后两个点挂了
171521
SCWLine楼主2021/12/7 14:11

代码参考第一篇题解
莫名其妙输了个负数实在不知道哪来的

#include<iostream>
#include<math.h>
#include<deque>
#include<string>
#define ll long long int
using namespace std;

int ord[2]={0,1};
bool tag[2]={false,false};
deque<ll> f[2];
string op;
ll x,y;

string Sswap()
{
	swap(ord[0],ord[1]);
	return "SUCCESS";
}

string Spush(int _x,int _n)
{
	if(!tag[ord[_x]])
		f[ord[_x]].push_back(_n);
	else
		f[ord[_x]].push_front(_n);
	return "SUCCESS";
}

string Spop(int _x)
{
	if(f[ord[_x]].empty())	return "UNSUCCESS";
	if(!tag[ord[_x]])
		f[ord[_x]].pop_back();
	else
		f[ord[_x]].pop_front();
	return "SUCCESS";
}

string Sadd(int _x)
{
	if(f[0].empty()||f[1].empty())	return "UNSUCCESS";
	ll temp=0;
	if(!tag[0])
		temp+=f[0].back(),f[0].pop_back();
	else
		temp+=f[0].front(),f[0].pop_front();
	
	if(!tag[1])
		temp+=f[1].back(),f[1].pop_back();
	else
		temp+=f[1].front(),f[1].pop_front();
	Spush(_x,temp);
	return "SUCCESS";
}

string Ssub(int _x)
{
	if(f[0].empty()||f[1].empty())	return "UNSUCCESS";
	ll temp1=0,temp2=0;
	if(!tag[0])
		temp1=f[0].back(),f[0].pop_back();
	else
		temp1=f[0].front(),f[0].pop_front();
	
	if(!tag[1])
		temp2=f[1].back(),f[1].pop_back();
	else
		temp2=f[1].front(),f[1].pop_front();
	Spush(_x,abs(temp1-temp2));
	return "SUCCESS";
}

string Sdel(int _x)
{
	while(!f[ord[_x]].empty())
		f[ord[_x]].pop_back();
	return "SUCCESS";	
}

string Smov(int _x,int _y)
{
	if(f[ord[_x]].size()>=f[ord[_y]].size())
	{
		while(!f[ord[_y]].empty())
		{
			if(!tag[ord[_y]])
			{
				if(!tag[ord[_x]])
				{
					f[ord[_x]].push_back(f[ord[_y]].back());
					f[ord[_y]].pop_back();
				} 
				else
				{
					f[ord[_x]].push_front(f[ord[_y]].back());
					f[ord[_y]].pop_back();
				} 
			}
			else
			{
				if(!tag[ord[_x]])
				{
					f[ord[_x]].push_back(f[ord[_y]].front());
					f[ord[_y]].pop_front();
				} 
				else
				{
					f[ord[_x]].push_front(f[ord[_y]].front());
					f[ord[_y]].pop_front();
				} 
			}	
		}
	}
	else
	{
		Smov(_y,_x);
		tag[ord[_y]]=(!tag[ord[_y]]);
		Sswap();
	}
	return "SUCCESS";
}

void Send()
{
	cout<<"SUCCESS"<<endl;
	if(f[ord[0]].empty()) cout<<"NONE";
	else
	{
		while(!f[ord[0]].empty())
		{
			if(!tag[ord[0]])
			{
				cout<<f[ord[0]].back()<<' ';
				f[ord[0]].pop_back();
			}
			else
			{
				cout<<f[ord[0]].front()<<' ';
				f[ord[0]].pop_front();
			}
		}
	}
	
	cout<<endl;
	if(f[ord[1]].empty()) cout<<"NONE";
	else
	{
		while(!f[ord[1]].empty())
		{
			if(!tag[ord[1]])
			{
				cout<<f[ord[1]].back()<<' ';
				f[ord[1]].pop_back();
			}
			else
			{
				cout<<f[ord[1]].front()<<' ';
				f[ord[1]].pop_front();
			}
		}
	}
	
	cout<<endl;
}
int main()
{
	while(true)
	{
		cin>>op;
		if(!op.compare("PUSH"))	
		{
			cin>>x>>y;
			cout<<Spush(x,y)<<endl;
		}
		
		else if(!op.compare("POP"))
		{
			cin>>x;
			cout<<Spop(x)<<endl;
		}
		
		else if(!op.compare("ADD"))
		{
			cin>>x;
			cout<<Sadd(x)<<endl;
		}
		else if(!op.compare("SUB"))
		{
			cin>>x;
			cout<<Ssub(x)<<endl;
		}
		else if(!op.compare("DEL"))
		{
			cin>>x;
			cout<<Sdel(x)<<endl;
		}
		else if(!op.compare("MOVE"))
		{
			cin>>x>>y;
			cout<<Smov(x,y)<<endl;
		}
		else if(!op.compare("SWAP"))
		{
			cout<<Sswap()<<endl;
		}
		else if(!op.compare("END"))
		{
            Send();
			break;
		}
	}
	return 0;
}
2021/12/7 14:11
加载中...