CodeForces 546C (Queue), codeforces546c
CodeForces 546C Soldier and Cards
Time Limit:2000 MSMemory Limit:262144KB64bit IO Format:% I64d & % I64u
Description
Two bored soldiers are playing card war. Their card deck consists of exactlyNCards, numbered from 1N, 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 exactly fights will happen and who will win the game, or state that game won't end.
Input
First line contains a single integerN(2 cores ≤ CoresNLimit ≤ limit 10), the number of cards.
Second line contains integerK1 (1 digit ≤ DigitK1 bytes ≤ bytesNRows-limit 1), the number of the first soldier's cards. Then followK1 integers that are the values on the first soldier's cards, from top to bottom of his stack.
Third line contains integerK2 (K1 worker + workerK2 bytes = bytesN), The number of the second soldier's cards. Then followK2 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 limit-limit 1.
Sample Input
Input
4
2 1 3
2 4 2
Output
6 2
Input
3
1 2
2 1 3
Output
-1
Question: Two soldiers play cards in order. If a is bigger than B, put B's card at the end of a, and then put a's card at the back of B's card. Obviously, two queues can be set for the queue problem. Just simulate the problem according to the question meaning. Note that there are still no results under a certain number of cycles, so there is no solution; In addition, you can directly mark the status search. You can mark only the first one or all of them, but the information you write is messy.
Queue usage:
# Include <iostream>
# Include <queue>
Using namespace std;
Const int n= 1000;
Queue <int> x, y;
Int main ()
{
Int I, t, a, B, n, m;
Cin> t;
Cin> n;
For (I = 0; I <n; I ++)
{
Cin>;
X. push ();
}
Cin> m;
For (I = 0; I <m; I ++)
{
Cin> B;
Y. push (B );
}
Int s = 0;
While (! X. empty ()&&! Y. empty ())
{
S ++;
If (s> N) break;
Int p = x. front ();
Int q = y. front ();
X. pop ();
Y. pop ();
If (p <q)
{
Y. push (p );
Y. push (q );
}
If (p> q)
{
X. push (q );
X. push (p );
}
}
If (x. empty ())
Cout <s <"<" 2 "<endl;
Else if (y. empty ())
Cout <s <"<" 1 "<endl;
Else cout <"-1" <endl;
Return 0;
}