fork download
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class D implements Runnable {
  5. static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
  6. static StringTokenizer st = new StringTokenizer("");
  7.  
  8. public static String next() {
  9. try {
  10. while (!st.hasMoreTokens()) {
  11. String s = br.readLine();
  12. if (s == null)
  13. return null;
  14. st = new StringTokenizer(s);
  15. }
  16. return st.nextToken();
  17. } catch(Exception e) {
  18. return null;
  19. }
  20. }
  21.  
  22. public static void main(String[] asda) throws Exception {
  23. new Thread(null, new D(), "D", 1<<26).start();
  24. }
  25.  
  26. public void run() {
  27. // size of tournament, wins
  28. int[][] winsToRank = new int[20][8];
  29. winsToRank[1][0] = 1;
  30.  
  31. winsToRank[2][0] = 2;
  32. winsToRank[2][1] = 1;
  33.  
  34. winsToRank[4][0] = 3;
  35. winsToRank[4][1] = 2;
  36. winsToRank[4][2] = 1;
  37.  
  38. winsToRank[8][0] = 5;
  39. winsToRank[8][1] = 3;
  40. winsToRank[8][2] = 2;
  41. winsToRank[8][3] = 1;
  42.  
  43. winsToRank[16][0] = 9;
  44. winsToRank[16][1] = 5;
  45. winsToRank[16][2] = 3;
  46. winsToRank[16][3] = 2;
  47. winsToRank[16][4] = 1;
  48.  
  49. // generate();
  50. int CASES = Integer.parseInt( next() );
  51. int ccc = 1;
  52. while (CASES-- > 0) {
  53. out.println("Case #" + ccc++ + ":");
  54.  
  55. N = Integer.parseInt( next() );
  56. beats = new boolean[N][N];
  57. for (int a = 0; a < N; a++) for (int b = 0; b < N; b++)
  58. beats[a][b] = next().equals("1");
  59.  
  60. if (N == 1) {
  61. out.println("1 1");
  62. continue;
  63. }
  64. if (N == 2) {
  65. if (beats[0][1]) {
  66. out.println("1 1");
  67. out.println("2 2");
  68. } else {
  69. out.println("2 2");
  70. out.println("1 1");
  71. }
  72. continue;
  73. }
  74.  
  75. // max number of wins a player can have in a tournament
  76. int[] maxWins = new int[N];
  77. // canWin(p, players) = can p win the tournament if players are playing? players is len = 2,4,8,16
  78. Set<Integer>[][] canWin = new TreeSet[N][N+1];
  79. for (int p = 0; p < N; p++) for (int q = 0; q <= N; q++)
  80. canWin[p][q] = new TreeSet<>();
  81.  
  82. for (int p = 0; p < N; p++) {
  83. Set<Integer> result = canWin[p][2];
  84. for (int q = 0; q < N; q++) if (q != p && beats[p][q]) {
  85. maxWins[p] = 1;
  86. result.add( (1<<p)|(1<<q) );
  87. }
  88. }
  89. for (int players = 4, wins = 2; players <= N; players *= 2, wins++) {
  90. loop: for (int p = 0; p < N; p++) if (!canWin[p][players/2].isEmpty()) {
  91. Set<Integer> result = canWin[p][players];
  92.  
  93. for (int q = 0; q < N; q++) if (q != p && beats[p][q] && !canWin[q][players/2].isEmpty()) {
  94. for (int qMatch : canWin[q][players/2]) for (int pMatch : canWin[p][players/2]) if ( (qMatch&pMatch) == 0 ) {
  95. result.add( qMatch|pMatch );
  96. if (players == N) {
  97. maxWins[p] = wins;
  98. continue loop;
  99. }
  100. }
  101. }
  102.  
  103. if (!result.isEmpty()) {
  104. maxWins[p] = wins;
  105. }
  106. }
  107. }
  108.  
  109. int[] minRank = new int[N];
  110. for (int p = 0; p < N; p++) {
  111. int wins = 0;
  112. for (int q = 0; q < N; q++) if (q != p && beats[p][q]) {
  113. wins++;
  114. }
  115.  
  116. if (wins == N-1) {
  117. minRank[p] = 1;
  118. continue;
  119. }
  120.  
  121. if (N == 4) {
  122. minRank[p] = 3;
  123. } else if (N==8) {
  124. minRank[p] = 5;
  125. } else {
  126. minRank[p] = 9;
  127. }
  128. }
  129. for (int p = 0; p < N; p++) {
  130. int wins = maxWins[p];
  131. out.println( winsToRank[N][wins] + " " + minRank[p] );
  132. }
  133. }
  134. //
  135. out.flush();
  136. System.exit(0);
  137. }
  138. void generate() {
  139. Random r = new Random();
  140. int N = 16;
  141. out.println(N);
  142. int[][] val = new int[N][N];
  143. for (int p = 0; p < N; p++) for (int q = p+1; q < N; q++) {
  144. if (r.nextBoolean()) {
  145. val[p][q] = 1;
  146. } else {
  147. val[q][p] = 1;
  148. }
  149. }
  150. for (int p = 0; p < N; p++) {
  151. for (int q = 0; q < N; q++) {
  152. out.print(val[p][q] + " ");
  153. }
  154. out.println();
  155. }
  156. }
  157.  
  158. int N; // number of competitors
  159. boolean[][] beats; // beats[a][b] = true if a beats b in a match
  160. }
  161.  
  162.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:4: error: class D is public, should be declared in a file named D.java
public class D implements Runnable {
       ^
Note: Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
stdout
Standard output is empty