fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef short int si;
  6.  
  7. void print_card(pair <si, char> cards[], si start, si end);
  8.  
  9. int main()
  10. {
  11.  
  12. si sum;
  13. pair <si, char> cards[5];
  14.  
  15. // Keep the assumption in mind that the boy plays the worst he can and the girl plays the best she can.
  16.  
  17. while (true)
  18. {
  19. // Input
  20. for (si i = 0; i < 5; i++)
  21. {
  22. cin >> cards[i].first;
  23. cards[i].second = (i < 3 ? 'g' : 'b');
  24. }
  25.  
  26. // checking end of input
  27. sum = 0;
  28. for (si i = 0; i < 5; i++)
  29. sum += cards[i].first;
  30.  
  31. if (sum == 0)
  32. break;
  33.  
  34.  
  35. sort(cards, cards + 5);
  36.  
  37. // If boy has 2 biggest cards he cant lose
  38. if (cards[4].second == 'b' && cards[3].second == 'b')
  39. {
  40. print_card(cards, 1, 52);
  41. continue;
  42. }
  43. else if (cards[4].second == 'g' && cards[3].second == 'b')
  44. {
  45. // if cards are _ _ g b g, the girl will win two rounds irrespective of where of two places b card is
  46. if (cards[2].second == 'g')
  47. {
  48. print_card(cards, 53, 53);
  49. continue;
  50. }
  51. // if cards are _ _ b b g, the girl will win with his biggest card and so will the boy. we need a valid card bigger than the second largest girl card.
  52. else
  53. {
  54. print_card(cards, cards[1].first + 1, 52);
  55. continue;
  56. }
  57. }
  58. else if (cards[4].second == 'b' && cards[3].second == 'g')
  59. {
  60. // if cards are _ _ b g b, the boy will win with his biggest card and so will the girl. we need a valid card bigger than the second largest girl card.
  61. if (cards[2].second == 'b')
  62. {
  63. print_card(cards, cards[1].first + 1, 52);
  64. continue;
  65. }
  66. // if cards are _ _ g g b, the girl will win two rounds irrespective of where of two places b card is
  67. else
  68. {
  69. print_card(cards, cards[3].first + 1, 52);
  70. continue;
  71. }
  72. }
  73. // The boy is doomed to lose
  74. else
  75. {
  76. print_card(cards, 53, 53);
  77. }
  78.  
  79. }
  80.  
  81. return 0;
  82. }
  83.  
  84. // Just a few checks to see if the next bigger card is a valid card to print.
  85. void print_card(pair <si, char> cards[], si start, si end)
  86. {
  87. if (start >= 52)
  88. {
  89. cout << -1 << '\n';
  90. return;
  91. }
  92.  
  93. bool card_used = false;
  94. for (si i = start; i <= end; i++)
  95. {
  96. for (si j = 0; j < 5; j++)
  97. {
  98. card_used = false;
  99. if (cards[j].first == i)
  100. {
  101. card_used = true;
  102. break;
  103. }
  104. }
  105.  
  106. if (!card_used)
  107. {
  108. cout << i << '\n';
  109. break;
  110. }
  111. else if (start == 52)
  112. {
  113. cout << -1 << '\n';
  114. }
  115. }
  116. }
Success #stdin #stdout 0s 4512KB
stdin
11 17 4 16 39
32 18 14 8 30
50 46 51 23 5
44 32 45 40 21
34 43 52 37 27
19 27 23 18 52
1 4 17 32 47
31 12 41 44 47
44 35 19 12 22
16 45 40 15 5
42 34 32 40 27
31 41 6 23 20
1 6 10 40 49
49 7 31 44 18
28 35 38 15 49
8 4 7 14 3
27 42 3 9 6
23 10 39 8 25
11 33 32 17 52
25 44 34 11 6
6 29 37 12 26
3 47 5 10 8
7 12 25 38 48
6 2 24 13 18
18 9 51 2 48
52 33 43 5 18
26 39 5 27 24
51 42 1 50 2
47 20 14 31 51
13 49 35 11 26
7 11 34 49 43
51 50 29 31 52
29 7 8 12 15
33 29 2 22 36
28 34 8 10 21
33 16 31 43 40
49 17 14 1 48
13 29 2 20 40
44 26 25 47 9
7 3 37 36 24
20 11 34 27 21
52 4 18 13 32
41 25 9 42 21
24 33 22 25 3
49 15 52 9 22
32 7 20 16 24
11 15 18 16 5
8 48 19 37 33
7 33 23 6 41
33 50 30 13 35
41 23 50 34 14
30 42 9 48 26
17 30 34 39 35
23 6 16 18 25
43 9 24 23 19
38 3 16 15 43
51 31 42 24 12
13 34 52 4 51
25 46 35 24 45
1 9 23 17 51
16 33 18 45 48
18 50 28 48 9
22 36 42 49 39
17 21 9 51 44
1 52 28 23 44
2 12 34 6 14
18 34 39 45 29
24 42 40 13 39
2 5 35 10 32
3 38 7 6 9
50 8 20 7 13
27 49 29 42 18
5 13 7 20 1
21 22 6 4 7
37 34 45 19 39
1 16 46 48 50
35 5 30 44 12
25 19 37 2 8
20 5 23 1 36
5 13 17 49 31
33 2 20 21 22
4 30 35 32 31
0 0 0 0 0
stdout
12
-1
-1
-1
-1
28
2
1
-1
-1
-1
-1
2
-1
39
9
-1
-1
34
-1
-1
6
1
7
-1
-1
-1
-1
21
-1
1
-1
9
34
-1
1
-1
14
45
8
22
-1
43
-1
-1
-1
-1
20
34
-1
-1
43
1
17
-1
39
-1
-1
-1
10
1
-1
37
1
-1
-1
40
-1
6
-1
-1
-1
14
-1
-1
2
36
-1
24
1
23
33