1400*C. Soldier and Cards(贪心&模拟)

Problem - 546C - Codeforces 

Soldier and Cards - 洛谷

1400*C. Soldier and Cards(贪心&模拟)_第1张图片

1400*C. Soldier and Cards(贪心&模拟)_第2张图片解析:

        模拟即可,当循环次数过大的时候跳出循环打印 -1

#include
using namespace std;
#define int long long
const int N=2e5+5;
int n,x,k1,k2,cnt;
queuea,b;
signed main(){
	cin>>n;
	cin>>k1;
	for(int i=1;i<=k1;i++){
		cin>>x;
		a.push(x);
	}
	cin>>k2;
	for(int i=1;i<=k2;i++){
		cin>>x;
		b.push(x);
	}
	while(a.size()&&b.size()){
		cnt++;
		if(cnt>=10000) break;
		int t1=a.front();
		int t2=b.front();
		a.pop(),b.pop();
		if(t1>t2){
			a.push(t2);
			a.push(t1);
		}
		else{
			b.push(t1);
			b.push(t2);
		}
	}
	if(cnt>=10000) printf("-1");
	else{
		printf("%lld %d",cnt,a.size()?1:2);
	}
	return 0;
}

你可能感兴趣的:(codeforces,c语言,算法,开发语言,数据结构,c++)