queue+模拟 Codeforces Round #304 (Div. 2) C. Soldier and Cards

题目传送门

 1 /*
 2     题意:两堆牌,每次拿出上面的牌做比较,大的一方收走两张牌,直到一方没有牌
 3     queue容器:模拟上述过程,当次数达到最大值时判断为-1
 4 */
 5 #include <cstdio>
 6 #include <iostream>
 7 #include <algorithm>
 8 #include <cstring>
 9 #include <string>
10 #include <stack>
11 #include <cmath>
12 #include <queue>
13 using namespace std;
14
15 const int MAXN = 20;
16 const int INF = 0x3f3f3f3f;
17 int x[MAXN];
18 queue<int> Q1;
19 queue<int> Q2;
20
21 int main(void)        //Codeforces Round #304 (Div. 2) C. Soldier and Cards
22 {
23     int n, m1, m2;
24     while (scanf ("%d", &n) == 1)
25     {
26         while (!Q1.empty ())    Q1.pop ();
27         while (!Q2.empty ())    Q2.pop ();
28
29         int x;
30         scanf ("%d", &m1);
31         for (int i=1; i<=m1; ++i)
32         {
33             scanf ("%d", &x);    Q1.push (x);
34         }
35
36         scanf ("%d", &m2);
37         for (int i=1; i<=m2; ++i)
38         {
39             scanf ("%d", &x);    Q2.push (x);
40         }
41
42         int cnt = 0;
43         while (!Q1.empty () && !Q2.empty ())
44         {
45             int x = Q1.front ();    int y = Q2.front ();
46             Q1.pop ();    Q2.pop ();
47             if (x < y)    {Q2.push (x);    Q2.push (y);}
48             else    {Q1.push (y);    Q1.push (x);}
49             ++cnt;
50             if (cnt == 10000)    break;
51         }
52
53         if (cnt == 10000)    puts ("-1");
54         else
55         {
56             printf ("%d %d\n", cnt, (Q1.empty ()) ? 2 : 1);
57         }
58     }
59
60     return 0;
61 }
62
63 /*
64 4
65 2 1 3
66 2 4 2
67 3
68 1 2
69 2 1 3
70 */
时间: 2024-10-14 23:40:42

queue+模拟 Codeforces Round #304 (Div. 2) C. Soldier and Cards的相关文章

Codeforces Round #304 (Div. 2)——Cdfs——Soldier and Cards

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 pla

数学+DP Codeforces Round #304 (Div. 2) D. Soldier and Number Game

题目传送门 1 /* 2 题意:这题就是求b+1到a的因子个数和. 3 数学+DP:a[i]保存i的最小因子,dp[i] = dp[i/a[i]] +1;再来一个前缀和 4 */ 5 /************************************************ 6 Author :Running_Time 7 Created Time :2015-8-1 14:08:34 8 File Name :B.cpp 9 ******************************

DP+埃氏筛法 Codeforces Round #304 (Div. 2) D. Soldier and Number Game

题目传送门 1 /* 2 题意:b+1,b+2,...,a 所有数的素数个数和 3 DP+埃氏筛法:dp[i] 记录i的素数个数和,若i是素数,则为1:否则它可以从一个数乘以素数递推过来 4 最后改为i之前所有素数个数和,那么ans = dp[a] - dp[b]: 5 详细解释:http://blog.csdn.net/catglory/article/details/45932593 6 */ 7 #include <cstdio> 8 #include <algorithm>

水题 Codeforces Round #304 (Div. 2) A. Soldier and Bananas

题目传送门 1 /* 2 水题:ans = (1+2+3+...+n) * k - n,开long long 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstring> 7 #include <cmath> 8 using namespace std; 9 10 typedef long long ll; 11 12 int main(void) //Codeforces Roun

贪心 Codeforces Round #304 (Div. 2) B. Soldier and Badges

题目传送门 1 /* 2 题意:问最少增加多少值使变成递增序列 3 贪心:排序后,每一个值改为前一个值+1,有可能a[i-1] = a[i] + 1,所以要 >= 4 */ 5 #include <cstdio> 6 #include <cstring> 7 #include <algorithm> 8 using namespace std; 9 10 typedef long long ll; 11 12 const int MAXN = 3e3 + 10;

Codeforces Round #304 (Div. 2) E. Soldier and Traveling 最大流 Dinic EK 算法

E. Soldier and Traveling time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output In the country there are n cities and m bidirectional roads between them. Each city has an army. Army of the i-th ci

codeforces水题100道 第五题 Codeforces Round #304 (Div. 2) A. Soldier and Bananas (math)

题目链接:http://www.codeforces.com/problemset/problem/546/A题意:一个人现在有n元,它买第i根香蕉需要i*k元,问他要买w根香蕉的话,需要问他的朋友借多少钱?C++代码: #include <iostream> using namespace std; int n, k, w; int main() { cin >> k >> n >> w; cout << max(0, w*(w+1)/2*k-

Codeforces Round #304 (Div. 2)——1002—— Soldier and Badges

Colonel has n badges. He wants to give one badge to every of his n soldiers. Each badge has a coolness factor, which shows how much it's owner reached. Coolness factor can be increased by one for the cost of one coin. For every pair of soldiers one o

模拟 Codeforces Round #297 (Div. 2) A. Vitaliy and Pie

题目传送门 1 /* 2 模拟:这就是一道模拟水题,看到标签是贪心,还以为错了呢 3 题目倒是很长:) 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <iostream> 8 #include <algorithm> 9 #include <cstring> 10 using namespace std; 11 12 const int MAXN = 2e5 + 10; 13