fork(2) download
  1. import java.util.*;
  2. import java.util.regex.*;
  3. import java.text.*;
  4. import java.math.*;
  5.  
  6.  
  7. class Arrfix
  8. {
  9. public int mindiff(int[] A, int[] B, int[] F)
  10. {
  11. int[] c = new int[1010];
  12. for(int x: F) c[x]++;
  13. int ret = 0;
  14.  
  15. boolean[] d = new boolean[A.length];
  16. for(int i = 0; i < A.length; i++){
  17. if(A[i] == B[i]){
  18. //A[i] = B[i];
  19. continue;
  20. }
  21. if(c[B[i]] > 0){
  22. d[i] = true;
  23. A[i] = B[i];
  24. c[B[i]]--;
  25. }
  26. }
  27. for(int i = 0; i < A.length; i++)
  28. System.out.print(A[i]+",");
  29. System.out.println();
  30. //System.out.println(ret);
  31. for(int i = 0; i < F.length; i++){
  32. if(c[F[i]] == 0) continue;
  33. boolean found = false;
  34. int indx = -1;
  35. for(int j = 0; j < A.length; j++){
  36. if(!d[j] && B[j] == F[i] && A[j] != B[j]){
  37. found = true;
  38. indx = j;
  39. break;
  40. }
  41. }
  42. if(found){
  43. A[indx] = B[indx];
  44. d[indx] = true;
  45. c[F[i]]--;
  46. continue;
  47. }
  48. else{
  49. for(int j = 0; j < A.length; j++){
  50. if(!d[j] && B[j] == F[i]){
  51. found = true;
  52. indx = j;
  53. break;
  54. }
  55. }
  56. if(found){
  57. A[indx] = B[indx];
  58. d[indx] = true;
  59. c[F[i]]--;
  60. continue;
  61. }
  62. int j = 0;
  63. while(j < A.length && (d[j] || c[B[j]] > 0 || A[j] == B[j])) j++;
  64. if(j == A.length){
  65. j = 0;
  66. //while(j < A.length && (d[j] || c[B[j]] > 0)) j++;
  67. while(j < A.length && d[j]) j++;
  68. }
  69. if(j == A.length){
  70. j = 0;
  71. while(j < A.length && d[j]) j++;
  72. }
  73. System.out.println("Filling "+F[i]+" at "+A[j]);
  74.  
  75. A[j] = F[i];
  76. d[j] = true;
  77. c[F[i]]--;
  78. }
  79. }
  80. for(int i = 0; i < A.length; i++)
  81. if(A[i] != B[i]) ret++;
  82. for(int i = 0; i < A.length; i++)
  83. System.out.print(A[i]+",");
  84. System.out.println();
  85. return ret;
  86. }
  87.  
  88. //<%:testing-code%>
  89. // BEGIN KAWIGIEDIT TESTING
  90. // Generated by KawigiEdit-pf 2.3.0
  91. private static boolean KawigiEdit_RunTest(int testNum, int[] p0, int[] p1, int[] p2, boolean hasAnswer, int p3) {
  92. System.out.print("Test " + testNum + ": [" + "{");
  93. for (int i = 0; p0.length > i; ++i) {
  94. if (i > 0) {
  95. System.out.print(",");
  96. }
  97. System.out.print(p0[i]);
  98. }
  99. System.out.print("}" + "," + "{");
  100. for (int i = 0; p1.length > i; ++i) {
  101. if (i > 0) {
  102. System.out.print(",");
  103. }
  104. System.out.print(p1[i]);
  105. }
  106. System.out.print("}" + "," + "{");
  107. for (int i = 0; p2.length > i; ++i) {
  108. if (i > 0) {
  109. System.out.print(",");
  110. }
  111. System.out.print(p2[i]);
  112. }
  113. System.out.print("}");
  114. System.out.println("]");
  115. Arrfix obj;
  116. int answer;
  117. obj = new Arrfix();
  118. long startTime = System.currentTimeMillis();
  119. answer = obj.mindiff(p0, p1, p2);
  120. long endTime = System.currentTimeMillis();
  121. boolean res;
  122. res = true;
  123. System.out.println("Time: " + (endTime - startTime) / 1000.0 + " seconds");
  124. if (hasAnswer) {
  125. System.out.println("Desired answer:");
  126. System.out.println("\t" + p3);
  127. }
  128. System.out.println("Your answer:");
  129. System.out.println("\t" + answer);
  130. if (hasAnswer) {
  131. res = answer == p3;
  132. }
  133. if (!res) {
  134. System.out.println("DOESN'T MATCH!!!!");
  135. } else if ((endTime - startTime) / 1000.0 >= 2) {
  136. System.out.println("FAIL the timeout");
  137. res = false;
  138. } else if (hasAnswer) {
  139. System.out.println("Match :-)");
  140. } else {
  141. System.out.println("OK, but is it right?");
  142. }
  143. System.out.println("");
  144. return res;
  145. }
  146. public static void main(String[] args) {
  147. boolean all_right;
  148. boolean disabled;
  149. boolean tests_disabled;
  150. all_right = true;
  151. tests_disabled = false;
  152.  
  153. int[] p0;
  154. int[] p1;
  155. int[] p2;
  156. int p3;
  157.  
  158. // ----- test 0 -----
  159. disabled = false;
  160. p0 = new int[]{1,1,1};
  161. p1 = new int[]{2,2,2};
  162. p2 = new int[]{2};
  163. p3 = 2;
  164. all_right = (disabled || KawigiEdit_RunTest(0, p0, p1, p2, true, p3) ) && all_right;
  165. tests_disabled = tests_disabled || disabled;
  166. // ------------------
  167.  
  168. // ----- test 1 -----
  169. disabled = false;
  170. p0 = new int[]{1,1,1};
  171. p1 = new int[]{2,2,1};
  172. p2 = new int[]{2,2};
  173. p3 = 0;
  174. all_right = (disabled || KawigiEdit_RunTest(1, p0, p1, p2, true, p3) ) && all_right;
  175. tests_disabled = tests_disabled || disabled;
  176. // ------------------
  177.  
  178. // ----- test 2 -----
  179. disabled = false;
  180. p0 = new int[]{1,2,3};
  181. p1 = new int[]{3,2,1};
  182. p2 = new int[]{};
  183. p3 = 2;
  184. all_right = (disabled || KawigiEdit_RunTest(2, p0, p1, p2, true, p3) ) && all_right;
  185. tests_disabled = tests_disabled || disabled;
  186. // ------------------
  187.  
  188. // ----- test 3 -----
  189. disabled = false;
  190. p0 = new int[]{2,2,2};
  191. p1 = new int[]{2,2,2};
  192. p2 = new int[]{1,2,3};
  193. p3 = 2;
  194. all_right = (disabled || KawigiEdit_RunTest(3, p0, p1, p2, true, p3) ) && all_right;
  195. tests_disabled = tests_disabled || disabled;
  196. // ------------------
  197.  
  198. // ----- test 4 -----
  199. disabled = false;
  200. p0 = new int[]{1,3,3,3};
  201. p1 = new int[]{2,3,3,3};
  202. p2 = new int[]{2,2};
  203. p3 = 1;
  204. all_right = (disabled || KawigiEdit_RunTest(4, p0, p1, p2, true, p3) ) && all_right;
  205. tests_disabled = tests_disabled || disabled;
  206. // ------------------
  207.  
  208. // ----- test 4 -----
  209. disabled = false;
  210. p0 = new int[]{3,5,7};
  211. p1 = new int[]{3,5,7};
  212. p2 = new int[]{2,3};
  213. p3 = 1;
  214. all_right = (disabled || KawigiEdit_RunTest(4, p0, p1, p2, true, p3) ) && all_right;
  215. tests_disabled = tests_disabled || disabled;
  216. // ------------------
  217.  
  218.  
  219. if (all_right) {
  220. if (tests_disabled) {
  221. System.out.println("You're a stud (but some test cases were disabled)!");
  222. } else {
  223. System.out.println("You're a stud (at least on given cases)!");
  224. }
  225. } else {
  226. System.out.println("Some of the test cases had errors.");
  227. }
  228. }
  229. // END KAWIGIEDIT TESTING
  230. }
  231.  
  232. //Powered by KawigiEdit-pf 2.3.0!
  233.  
Success #stdin #stdout 0.1s 320576KB
stdin
Standard input is empty
stdout
Test 0: [{1,1,1},{2,2,2},{2}]
2,1,1,
2,1,1,
Time: 0.0 seconds
Desired answer:
	2
Your answer:
	2
Match :-)

Test 1: [{1,1,1},{2,2,1},{2,2}]
2,2,1,
2,2,1,
Time: 0.001 seconds
Desired answer:
	0
Your answer:
	0
Match :-)

Test 2: [{1,2,3},{3,2,1},{}]
1,2,3,
1,2,3,
Time: 0.0 seconds
Desired answer:
	2
Your answer:
	2
Match :-)

Test 3: [{2,2,2},{2,2,2},{1,2,3}]
2,2,2,
Filling 1 at 2
Filling 3 at 2
1,2,3,
Time: 0.0 seconds
Desired answer:
	2
Your answer:
	2
Match :-)

Test 4: [{1,3,3,3},{2,3,3,3},{2,2}]
2,3,3,3,
Filling 2 at 3
2,2,3,3,
Time: 0.001 seconds
Desired answer:
	1
Your answer:
	1
Match :-)

Test 4: [{3,5,7},{3,5,7},{2,3}]
3,5,7,
Filling 2 at 3
Filling 3 at 5
2,3,7,
Time: 0.001 seconds
Desired answer:
	1
Your answer:
	2
DOESN'T MATCH!!!!

Some of the test cases had errors.