fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. /**
  8.  * PART 1: Predicts rounds based on "higher rank always wins".
  9.  * Optimized to process the simulation in-place to save memory.
  10.  */
  11. void predictTournament(vector<int> players) {
  12. int round = 1;
  13. int currentSize = players.size();
  14.  
  15. while (currentSize > 0) {
  16. cout << "Round " << round << ": ";
  17. for (int i = 0; i < currentSize; ++i) {
  18. cout << players[i] << (i == currentSize - 1 ? "" : ", ");
  19. }
  20. cout << endl;
  21.  
  22. if (currentSize == 1) break;
  23.  
  24. // Winners are moved to the front half of the vector
  25. for (int i = 0; i < currentSize; i += 2) {
  26. players[i / 2] = min(players[i], players[i + 1]);
  27. }
  28.  
  29. currentSize /= 2;
  30. round++;
  31. }
  32. }
  33.  
  34. /**
  35.  * PART 2: Generates a balanced draw (Power of 2).
  36.  * Ensures top seeds meet as late as possible.
  37.  */
  38. vector<int> generateBalancedDraw(int n) {
  39. vector<int> draw;
  40. draw.reserve(n);
  41. draw.push_back(1);
  42. draw.push_back(2);
  43.  
  44. while (draw.size() < n) {
  45. vector<int> nextDraw;
  46. nextDraw.reserve(draw.size() * 2);
  47.  
  48. // Sum property: Seed + Opponent = (Current Pool Size * 2) + 1
  49. int targetSum = (draw.size() * 2) + 1;
  50.  
  51. for (int seed : draw) {
  52. nextDraw.push_back(seed);
  53. nextDraw.push_back(targetSum - seed);
  54. }
  55. draw = move(nextDraw); // Use move semantics for efficiency
  56. }
  57. return draw;
  58. }
  59.  
  60. int main() {
  61. int n = 8;
  62.  
  63. cout << "--- Part 2: Balanced Draw Generator ---" << endl;
  64. vector<int> draw = generateBalancedDraw(n);
  65. for (int i = 0; i < draw.size(); ++i) {
  66. cout << draw[i] << (i == draw.size() - 1 ? "" : " ");
  67. }
  68. cout << "\n\n";
  69.  
  70. cout << "--- Part 1: Tournament Prediction ---" << endl;
  71. predictTournament(draw);
  72.  
  73. return 0;
  74. }
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
--- Part 2: Balanced Draw Generator ---
1 8 4 5 2 7 3 6

--- Part 1: Tournament Prediction ---
Round 1: 1, 8, 4, 5, 2, 7, 3, 6
Round 2: 1, 4, 2, 3
Round 3: 1, 2
Round 4: 1