fork download
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.FileReader;
  4. import java.io.PrintWriter;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7.  
  8. public class Osmos {
  9.  
  10. public static String caseString = "Case #?: ";
  11.  
  12. public static String[] read(String file) throws Exception {
  13. String line = null;
  14. List<String> arrayList = new ArrayList<String>();
  15. while((line = br.readLine()) != null) {
  16. if(!line.trim().equalsIgnoreCase("")) {
  17. arrayList.add(line);
  18. }
  19. }
  20. return arrayList.toArray(new String[0]);
  21. }
  22.  
  23. public static void write(String file, String[] content) throws Exception {
  24. for (int i = 0; i < content.length; i++) {
  25. String caseS = caseString.replace("?", (i+1)+"");
  26. bw.append(caseS+content[i]+"\n");
  27. }
  28. bw.flush();
  29. bw.close();
  30. }
  31.  
  32. public static void main(String[] args) throws Exception {
  33. String inputfile = "/Users/sumitkumar/Desktop/codejam/1/A-large-practice.in";
  34. String outputfile = "/Users/sumitkumar/Desktop/codejam/1/A-large-practice.out";
  35. String[] input = read(inputfile);
  36. int testCases = Integer.parseInt(input[0]);
  37. String[] output = new String[testCases];
  38. int counter = 1;
  39. System.out.println("testCases ::"+testCases);
  40. for (int i = 0; i < testCases; i++) {
  41. System.out.println(i);
  42. String [] line1 = input[counter++].split(" ");
  43. int yourBot = Integer.parseInt(line1[0]);
  44. int botsCount = Integer.parseInt(line1[1]);
  45. String [] botsString = input[counter++].split(" ");
  46. int [] bots = new int[botsCount];
  47. for (int j = 0; j < botsCount; j++) {
  48. bots[j] = Integer.parseInt(botsString[j]);
  49. }
  50. output[i] = solve(yourBot, bots, botsCount);
  51. }
  52. write(outputfile, output);
  53. }
  54.  
  55. private static String solve(int yourBot, int[] bots, int botsCount) {
  56. sort(bots, botsCount);
  57. int myBot = yourBot;
  58. int change = 0;
  59. int i = 0;
  60. for (i = 0; i < botsCount; i++) {
  61. if(myBot > bots[i]) {
  62. myBot += bots[i];
  63. continue;
  64. }
  65. if((2*myBot-1) > bots[i]) {
  66. myBot = 2*myBot-1 + bots[i];
  67. change++;
  68. continue;
  69. }
  70. break;
  71. }
  72. int changesWithoutAddition = changesWithoutAddition(myBot, bots, i, botsCount);
  73. int changesWithAddition = -1;
  74. if(i < botsCount)
  75. changesWithAddition = changesWithAddition(myBot, bots, i, botsCount);
  76. int min = changesWithoutAddition;
  77. if(changesWithAddition >= 0) {
  78. min = (changesWithoutAddition < changesWithAddition) ? changesWithoutAddition : changesWithAddition;
  79. }
  80. change+=min;
  81. return change+"";
  82. }
  83.  
  84. private static int changesWithoutAddition(int myBot, int bots[], int index, int botsCount) {
  85. System.out.println("changesWithoutAddition"+index);
  86. return botsCount-index;
  87. }
  88. private static int changesWithAddition(int myBot, int bots[], int index, int botsCount) {
  89. System.out.println("changesWithAddition"+index);
  90. int tempBot = myBot;
  91. int counter = 0;
  92. while(bots[index] >= tempBot) {
  93. tempBot = 2*tempBot - 1;
  94. counter++;
  95. if(counter >= (botsCount-index)) {
  96. return -1;
  97. }
  98. }
  99. for (int i = index; i < botsCount; i++) {
  100. if(tempBot > bots[i]) {
  101. tempBot += bots[i];
  102. continue;
  103. }
  104. if((2*tempBot-1) > bots[i]) {
  105. tempBot = 2*tempBot-1 + bots[i];
  106. counter++;
  107. continue;
  108. }
  109. int changesWithoutAddition = changesWithoutAddition(tempBot, bots, i, botsCount);
  110. int changesWithAddition = -1;
  111. if(i < botsCount)
  112. changesWithAddition = changesWithAddition(tempBot, bots, i, botsCount);
  113. int min = changesWithoutAddition;
  114. if(changesWithAddition >= 0) {
  115. min = (changesWithoutAddition < changesWithAddition) ? changesWithoutAddition : changesWithAddition;
  116. }
  117. counter+=min;
  118. break;
  119. }
  120. return counter;
  121. }
  122.  
  123. public static void sort(int[] values, int size) {
  124. if (values ==null || size==0){
  125. return ;
  126. }
  127. quicksort(0, size - 1, values);
  128. }
  129.  
  130. private static void quicksort(int low, int high,int[] numbers) {
  131. int i = low, j = high;
  132. int pivot = numbers[low + (high-low)/2];
  133. while (i <= j) {
  134. while (numbers[i] < pivot) {
  135. i++;
  136. }
  137. while (numbers[j] > pivot) {
  138. j--;
  139. }
  140. if (i <= j) {
  141. exchange(i, j, numbers);
  142. i++;
  143. j--;
  144. }
  145. }
  146. if (low < j)
  147. quicksort(low, j, numbers);
  148. if (i < high)
  149. quicksort(i, high,numbers);
  150. }
  151.  
  152. private static void exchange(int i, int j, int[] numbers) {
  153. int temp = numbers[i];
  154. numbers[i] = numbers[j];
  155. numbers[j] = temp;
  156. }
  157.  
  158. }
  159.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:8: error: class Osmos is public, should be declared in a file named Osmos.java
public class Osmos {
       ^
1 error
stdout
Standard output is empty