Problem - 546C - Codeforces
Soldier and Cards - 洛谷
解析:
模拟即可,当循环次数过大的时候跳出循环打印 -1
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+5;
int n,x,k1,k2,cnt;
queue<int>a,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;
}