CF Soldier and Cards
2015-06-18 12:33
387 查看
C. Soldier and Cards
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Two bored soldiers are playing card war. Their card deck consists of exactly 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.
The rules are following. On each turn a 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 and
takes both cards from the table to the bottom of his stack. More precisely, he first takes his opponent's card and puts to the bottom of his stack, and then he puts his card to the bottom of his stack. If after some turn one of the player's stack becomes empty,
he loses and the other one wins.
You have to calculate how many fights will happen and who will win the game, or state that game won't end.
Input
First line contains a single integer n (2 ≤ n ≤ 10),
the number of cards.
Second line contains integer k1 (1 ≤ k1 ≤ n - 1),
the number of the first soldier's cards. Then follow k1 integers
that are the values on the first soldier's cards, from top to bottom of his stack.
Third line contains integer k2 (k1 + k2 = n),
the number of the second soldier's cards. Then follow k2 integers
that are the values on the second soldier's cards, from top to bottom of his stack.
All card values are different.
Output
If somebody wins in this game, print 2 integers where the first one stands for the number of fights before
end of game and the second one is 1 or 2 showing
which player has won.
If the game won't end and will continue forever output - 1.
Sample test(s)
input
output
input
output
Note
First sample:
Second sample:
题意:给一个n(<=10)表示两人手中共有n张牌,接下来一行表示第1个人有k1张牌,k1 v1[1] v1[2]......v1[k1], v1[i]表示第i 张牌的大小,第三行表示第2个人有k2张牌,k2 v2[1] v2[2]......v2[k2],
v2[i]表示第i 张牌的大小。每一轮,两人从牌顶部各出一张,谁出的牌大则两张牌归谁,放入到自己牌的底部,直到其中一个人手中没有牌出,则那个人输了。问需要多少轮,哪个人赢了。如果没有解则输出-1.
解题:直接模拟一下过程,主要是标记一下两个人手中牌的状态,用map<string,map<string,bool> >vist 标记一下。
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Two bored soldiers are playing card war. Their card deck consists of exactly 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.
The rules are following. On each turn a 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 and
takes both cards from the table to the bottom of his stack. More precisely, he first takes his opponent's card and puts to the bottom of his stack, and then he puts his card to the bottom of his stack. If after some turn one of the player's stack becomes empty,
he loses and the other one wins.
You have to calculate how many fights will happen and who will win the game, or state that game won't end.
Input
First line contains a single integer n (2 ≤ n ≤ 10),
the number of cards.
Second line contains integer k1 (1 ≤ k1 ≤ n - 1),
the number of the first soldier's cards. Then follow k1 integers
that are the values on the first soldier's cards, from top to bottom of his stack.
Third line contains integer k2 (k1 + k2 = n),
the number of the second soldier's cards. Then follow k2 integers
that are the values on the second soldier's cards, from top to bottom of his stack.
All card values are different.
Output
If somebody wins in this game, print 2 integers where the first one stands for the number of fights before
end of game and the second one is 1 or 2 showing
which player has won.
If the game won't end and will continue forever output - 1.
Sample test(s)
input
4 2 1 3 2 4 2
output
6 2
input
3 1 2 2 1 3
output
-1
Note
First sample:
Second sample:
题意:给一个n(<=10)表示两人手中共有n张牌,接下来一行表示第1个人有k1张牌,k1 v1[1] v1[2]......v1[k1], v1[i]表示第i 张牌的大小,第三行表示第2个人有k2张牌,k2 v2[1] v2[2]......v2[k2],
v2[i]表示第i 张牌的大小。每一轮,两人从牌顶部各出一张,谁出的牌大则两张牌归谁,放入到自己牌的底部,直到其中一个人手中没有牌出,则那个人输了。问需要多少轮,哪个人赢了。如果没有解则输出-1.
解题:直接模拟一下过程,主要是标记一下两个人手中牌的状态,用map<string,map<string,bool> >vist 标记一下。
#include <stdio.h> #include <string.h> #include <algorithm> #include<string> #include<map> using namespace std; int main() { int n , valu[15] , ta[15], tb[15] , topa,topb; char a[15],b[15]; while(~scanf("%d",&n)) { scanf("%d",&topa); for(int i=0;i<topa; i++){ scanf("%d",&valu[i]); ta[i]=valu[i]; } scanf("%d",&topb); for(int i=0; i<topb; i++){ scanf("%d",&tb[i]); valu[i+topa]=tb[i]; } sort(valu,valu+n); for(int i=0; i<topa; i++) for(int j=0;j<n; j++) if(ta[i]==valu[j]) a[i]=j+'0'; a[topa]='\0'; for(int i=0; i<topb; i++) for(int j=0;j<n; j++) if(tb[i]==valu[j]) b[i]=j+'0'; b[topb]='\0'; map<string,map<string,int> >vist; vist[a][b]=1; int ans=0; while(topa&&topb){ char aa=a[0] , bb=b[0]; for(int i=0;i<topa;i++) a[i]=a[i+1]; topa--; for(int i=0;i<topb;i++) b[i]=b[i+1]; topb--; if(aa>bb){ a[topa++]=bb; a[topa++]=aa; a[topa]='\0'; } else { b[topb++]=aa; b[topb++]=bb; b[topb]='\0'; } int& tmp= vist[a][b]; if(tmp==1){ ans=-1; break; } ans++; tmp=1; } if(ans!=-1) { printf("%d ",ans); if(topa==0) printf("%d\n",2); else printf("%d\n",1); } else printf("-1\n"); } return 0; }
相关文章推荐
- Git 命令
- nodeName、nodeValue 以及 nodeType
- .NET跨平台:在Ubuntu上用自己编译的dnx运行ASP.NET 5示例程序
- cgteamwork 观感
- 网站如何接入支付宝(转)
- git使用之如何将github库下载到本地与如何将代码上传github
- 排序---直接插入
- Unix环境之文件IO
- java——hello word
- 计算机科学箴言集 -- <编程珠玑续>
- 《疯狂的站长》读后感1
- ubuntu 下sublime不能输中文解决方法(汇总)
- URAL1009 K-based Numbers
- 跟我学Java安全编程系列
- phpMyAdmin最常见的两个错误解决
- Genymotion安卓模拟器系统字体小如何解决
- 浅析手机充电时不断重启
- 转的:运维新手们,别再问需不需要学PYTHON了
- JAVA语言的特点
- 使用ClippingNode对精灵进行遮罩处理