Codeforces Round #304 (Div. 2)——Cdfs——Soldier and Cards
3225 단어 codeforces
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 samp
시뮬레이션 창고.간단한 dfs, step가 크면 -1을 출력하면 돼요.
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a1[11],a2[11];
int n,m;
int dfs(int x1,int x2,int step)
{
if(step >= 10000){
printf("-1
");
return 0;
}
if(x1 == 0){
printf("%d 2
",step);
return 0;
}
if(x2 == 0){
printf("%d 1
",step);
return 0;
}
if(a1[x1] < a2[x2]){
int temp = a2[x2];
for(int i = x2-1; i >= 1; i--)
a2[i+2] = a2[i];
a2[2] = a1[x1];
a2[1] = temp;
dfs(--x1,++x2,step+1);
}
else {
int temp = a1[x1];
for(int i = x1-1 ; i >= 1; i--)
a1[i+2] = a1[i];
a1[2] = a2[x2];
a1[1] = temp;
dfs(++x1,--x2,step+1);
}
}
int main()
{
int T;
scanf("%d",&T);
scanf("%d",&n);
for(int i = n; i >= 1 ;i--)
scanf("%d",&a1[i]);
scanf("%d",&m);
for(int i = m; i >= 1 ;i--)
scanf("%d",&a2[i]);
dfs(n,m,0);
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Codeforces Round #715 Div. 2C The Sports Festival: 구간 DP전형구간 DP의 초전형. 이하, 0-indexed. 입력을 정렬하여 어디서나 시작하고 최적으로 좌우로 계속 유지하면 좋다는 것을 알 수 있습니다. {2000})$의 주문이 된다. 우선, 입력을 소트하여 n개의 요소를 $...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.