C. Soldier and Cards
time limit per test
memory limit per test
input
output
n cards, numbered from 1 to n, all values are different. They divide cards between them in some manner, it's possible that they have different number of cards. Then they play a "war"-like card game.
fight happens. Each of them picks card from the top of his stack and puts on the table. The one whose card value is bigger wins this fight
fights
Input
n (2 ≤ n ≤ 10), the number of cards.
k1 (1 ≤ k1 ≤ n - 1), the number of the first soldier's cards. Then follow k1
k2 (k1 + k2 = n), the number of the second soldier's cards. Then follow k2
All card values are different.
Output
2 integers where the first one stands for the number of fights before end of game and the second one is 1 or 2
- 1.
Sample test(s)
input
4 2 1 3 2 4 2
output
6 2
input
3 1 2 2 1 3
output
-1
deque:(双向队列)
push_back(a);
push_front(a);
pop_back();
pop_front();
q[a] 指向
题解:
本题11!种状态会T
但是大部分状态不可能出现,
于是卡时间即可。
(官方:给出的极限情况是106次,这游戏人类可玩。。)
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
#include<set>
#include<deque>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (10)
#define MAXStage (100000)
typedef long long ll;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
deque<int> qa,qb;
void work()
{
int a=qa[0],b=qb[0];
qa.pop_front();qb.pop_front();
if (a<b) qb.push_back(a),qb.push_back(b);
if (a>b) qa.push_back(b),qa.push_back(a);
}
set< pair< deque < int > , deque< int > > > h;
int n;
int main()
{
// freopen("CF546C.in","r",stdin);
cin>>n;
int k1,k2;
cin>>k1;
For(i,k1)
{
int p;
cin>>p;
qa.push_back(p);
}
cin>>k2;
For(i,k2)
{
int p;
cin>>p;
qb.push_back(p);
}
Rep(ans,MAXStage)
{
if (qa.empty())
{
printf("%d 2\n",ans);
return 0;
}
else if (qb.empty())
{
printf("%d 1\n",ans);
return 0;
}
else
{
if (h.find(make_pair(qa,qb))!=h.end())
{
cout<<"-1"<<endl;
return 0;
}
h.insert(make_pair(qa,qb));
work();
// Rep(i,qa.size()) cout<<qa[i]<<' ';cout<<endl;
// Rep(i,qb.size()) cout<<qb[i]<<' ';cout<<endl;
}
}
return 0;
}