fork download
  1. #include <iostream>
  2. #include <cstring>
  3. #include <array>
  4. #include <vector>
  5.  
  6. using u32 = unsigned int;
  7.  
  8. #define hint( msg )
  9.  
  10. enum RESULT
  11. {
  12. win,
  13. even,
  14. lose,
  15. result_count, hint( "sizer" )
  16. };
  17.  
  18. struct TEAM_SCORE
  19. {
  20. int counts[ result_count ];
  21. };
  22.  
  23. std::vector< TEAM_SCORE > results;
  24. std::vector< TEAM_SCORE > suppose;
  25.  
  26. //----------------------------------------------------------------------------------------
  27.  
  28. std::vector< std::pair< int, int > > matches;
  29.  
  30. void build_matches( const int team_count )
  31. {
  32. matches.clear();
  33.  
  34. const int triangular_size = team_count * ( team_count - 1 ) / 2;
  35. matches.reserve( triangular_size );
  36.  
  37. for( int a = 0; a < team_count - 1; ++a )
  38. for( int b = a + 1; b < team_count; ++b )
  39. matches.emplace_back( std::make_pair( a, b ) );
  40. }
  41. //----------------------------------------------------------------------------------------
  42.  
  43. bool possible;
  44.  
  45. void play_game1( u32 game_index );
  46.  
  47. inline void test_case1( const u32 game_index, const RESULT result_a, const RESULT result_b )
  48. {
  49. const u32 team_a = matches[ game_index ].first;
  50. const u32 team_b = matches[ game_index ].second;
  51.  
  52. if( suppose[ team_a ].counts[ result_a ] + 1 <=
  53. results[ team_a ].counts[ result_a ] &&
  54.  
  55. suppose[ team_b ].counts[ result_b ] + 1 <=
  56. results[ team_b ].counts[ result_b ] )
  57. {
  58. suppose[ team_a ].counts[ result_a ]++;
  59. suppose[ team_b ].counts[ result_b ]++;
  60. play_game1( game_index + 1 );
  61. suppose[ team_a ].counts[ result_a ]--;
  62. suppose[ team_b ].counts[ result_b ]--;
  63. }
  64. }
  65.  
  66. void play_game1( u32 game_index )
  67. {
  68. if( game_index >= matches.size() )
  69. {
  70. possible = true;
  71. return;
  72. }
  73.  
  74. test_case1( game_index, win, lose );
  75. if( possible ) return;
  76.  
  77. test_case1( game_index, lose, win );
  78. if( possible ) return;
  79.  
  80. test_case1( game_index, even, even );
  81. if( possible ) return;
  82. }
  83. //----------------------------------------------------------------------------------------
  84.  
  85. #include <setjmp.h>
  86.  
  87. jmp_buf env;
  88.  
  89. void play_game2( u32 game_index );
  90.  
  91. inline void test_case2( const u32 game_index, const RESULT result_a, const RESULT result_b )
  92. {
  93. const u32 team_a = matches[ game_index ].first;
  94. const u32 team_b = matches[ game_index ].second;
  95.  
  96. if( suppose[ team_a ].counts[ result_a ] + 1 <=
  97. results[ team_a ].counts[ result_a ] &&
  98.  
  99. suppose[ team_b ].counts[ result_b ] + 1 <=
  100. results[ team_b ].counts[ result_b ] )
  101. {
  102. suppose[ team_a ].counts[ result_a ]++;
  103. suppose[ team_b ].counts[ result_b ]++;
  104. play_game2( game_index + 1 );
  105. suppose[ team_a ].counts[ result_a ]--;
  106. suppose[ team_b ].counts[ result_b ]--;
  107. }
  108. }
  109.  
  110. void play_game2( u32 game_index )
  111. {
  112. if( game_index >= matches.size() )
  113. longjmp( env, 1 );
  114.  
  115. test_case2( game_index, win, lose );
  116. test_case2( game_index, lose, win );
  117. test_case2( game_index, even, even );
  118. }
  119. //----------------------------------------------------------------------------------------
  120.  
  121. using namespace std;
  122.  
  123. constexpr int team_count = 6;
  124. constexpr int problem_count = 4;
  125.  
  126. array< array< TEAM_SCORE, team_count >, problem_count > inputs
  127. { {
  128. { { { 5, 0, 0 }, { 3, 0, 2 }, { 2, 0, 3 }, { 0, 0, 5 }, { 4, 0, 1 }, { 1, 0, 4 } } },
  129. { { { 4, 1, 0 }, { 3, 0, 2 }, { 4, 1, 0 }, { 1, 1, 3 }, { 0, 0, 5 }, { 1, 1, 3 } } },
  130. { { { 5, 0, 0 }, { 4, 0, 1 }, { 2, 2, 1 }, { 2, 0, 3 }, { 1, 0, 4 }, { 0, 0, 5 } } },
  131. { { { 5, 0, 0 }, { 3, 1, 1 }, { 2, 1, 2 }, { 2, 0, 3 }, { 0, 0, 5 }, { 1, 0, 4 } } },
  132. } };
  133.  
  134. int main()
  135. {
  136. build_matches( team_count );
  137. suppose.resize( team_count );
  138.  
  139. for( u32 i = 0; i < inputs.size(); ++i )
  140. {
  141. std::fill( suppose.begin(), suppose.end(), TEAM_SCORE{ 0, 0, 0 } );
  142. results.assign( inputs[ i ].begin(), inputs[ i ].end() );
  143. possible = false;
  144. play_game1( 0 );
  145. cout << possible << " ";
  146. }
  147. cout << endl;
  148.  
  149. for( u32 i = 0; i < inputs.size(); ++i )
  150. {
  151. std::fill( suppose.begin(), suppose.end(), TEAM_SCORE{ 0, 0, 0 } );
  152. results.assign( inputs[ i ].begin(), inputs[ i ].end() );
  153. int possible = setjmp( env );
  154. if( !possible )
  155. play_game2( 0 );
  156. cout << possible << " ";
  157. }
  158. cout << endl;
  159.  
  160. return 0;
  161. }
Success #stdin #stdout 0s 3464KB
stdin
Standard input is empty
stdout
1 1 0 0 
1 1 0 0