Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.38 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. public class Main {
  4.  
  5.  
  6.  
  7. /************************ SOLUTION STARTS HERE ************************/
  8.  
  9. static final int INF = (int) 1e9;
  10. static int compress[][];
  11. static int sz;
  12. static int memo[][][];
  13.  
  14. static int rec(int idx , int rem , int hUp , int hDown , int v) {
  15. int mask = (hUp << 2) | (hDown << 1) | v;
  16. if(rem < 0)
  17. return INF;
  18. else if(idx == sz - 1)
  19. return 0;
  20. else if(memo[idx][rem][mask] != -1)
  21. return memo[idx][rem][mask];
  22. else {
  23. int min = INF;
  24. // Create new rectangle
  25. if(compress[idx + 1][1] == 0)
  26. min = Math.min(min , 1 + rec(idx + 1, rem - 1, 1, 0, 0));
  27. if(compress[idx + 1][1] == 1)
  28. min = Math.min(min , 1 + rec(idx + 1, rem - 1, 0, 1, 0));
  29. if(compress[idx + 1][1] == 2)
  30. min = Math.min(min , 2 + rec(idx + 1, rem - 2, 1, 1, 0));
  31.  
  32. min = Math.min(min , 2 + rec(idx + 1, rem - 1, 0, 0, 1));
  33.  
  34. // Extend
  35. if(v == 1)
  36. min = Math.min(min , 2 * (compress[idx + 1][0] - compress[idx][0]) +
  37. rec(idx + 1, rem, 0, 0, 1));
  38. else if(hUp == 1 && hDown == 0) {
  39. if(compress[idx + 1][1] == 0)
  40. min = Math.min(min , (compress[idx + 1][0] - compress[idx][0])
  41. + rec(idx + 1, rem, 1, 0, 0));
  42. else
  43. min = Math.min(min , (compress[idx + 1][0] - compress[idx][0]) + 1
  44. + rec(idx + 1, rem - 1, 1, 1, 0));
  45. }
  46. else if(hUp == 0 && hDown == 1) {
  47. if(compress[idx + 1][1] == 1)
  48. min = Math.min(min , (compress[idx + 1][0] - compress[idx][0])
  49. + rec(idx + 1, rem, 0, 1, 0));
  50. else
  51. min = Math.min(min , (compress[idx + 1][0] - compress[idx][0]) + 1
  52. + rec(idx + 1, rem - 1, 1, 1, 0));
  53. }
  54. else {
  55. if(compress[idx + 1][1] == 0)
  56. min = Math.min(min , (compress[idx + 1][0] - compress[idx][0])
  57. + rec(idx + 1, rem, 1, 0, 0));
  58. else if(compress[idx + 1][1] == 1)
  59. min = Math.min(min , (compress[idx + 1][0] - compress[idx][0])
  60. + rec(idx + 1, rem, 0, 1, 0));
  61.  
  62. min = Math.min(min , 2 * (compress[idx + 1][0] - compress[idx][0])
  63. + rec(idx + 1, rem, 1, 1, 0));
  64. }
  65.  
  66. return memo[idx][rem][mask] = min;
  67. }
  68. }
  69.  
  70. private static void solve2() {
  71.  
  72. int N = nextInt();
  73. int K = nextInt();
  74. int B = nextInt();
  75.  
  76. int arr[][] = new int[N][];
  77. for(int i = 0; i < N; i++)
  78. arr[i] = nextIntArray(2);
  79.  
  80.  
  81. // long st = System.nanoTime();
  82.  
  83. Arrays.sort(arr , new Comparator<int[]>() {
  84. @Override
  85. public int compare(int[] o1, int[] o2) {
  86. if(o1[1] != o2[1])
  87. return o1[1] - o2[1];
  88. else
  89. return o1[0] - o2[0];
  90. }
  91. });
  92.  
  93. sz = 1;
  94. for(int i = 1; i < N; i++)
  95. sz += arr[i][1] != arr[i - 1][1] ? 1 : 0;
  96.  
  97. compress = new int[sz][2]; // 0 - up , 1 - down , 2 - both
  98. int ptr = 0;
  99. for(int i = 0; i < N; ) {
  100. compress[ptr][0] = arr[i][1];
  101. if(i + 1 < N && arr[i][1] == arr[i + 1][1]) {
  102. compress[ptr++][1] = 2;
  103. i += 2;
  104. } else {
  105. compress[ptr++][1] = arr[i][0] - 1;
  106. i++;
  107. }
  108. }
  109.  
  110. memo = new int[sz][K][8];
  111. for(int a[][] : memo)
  112. for(int b[] : a)
  113. Arrays.fill(b, -1);
  114.  
  115. int min = INF;
  116. if(compress[0][1] == 0)
  117. min = Math.min(min , 1 + rec(0, K - 1, 1, 0, 0));
  118. else if(compress[0][1] == 1)
  119. min = Math.min(min , 1 + rec(0, K - 1, 0, 1, 0));
  120. else
  121. min = Math.min(min , 2 + rec(0, K - 2, 1, 1, 0));
  122.  
  123. min = Math.min(min , 2 + rec(0, K - 1, 0, 0, 1));
  124.  
  125. println(min);
  126. }
  127.  
  128.  
  129. /************************ SOLUTION ENDS HERE ************************/
  130.  
  131.  
  132.  
  133.  
  134.  
  135. /************************ TEMPLATE STARTS HERE **********************/
  136.  
  137. public static void main(String[] args) throws IOException {
  138. reader = new BufferedReader(new InputStreamReader(System.in));
  139. writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)), false);
  140. st = null;
  141. solve2();
  142. reader.close();
  143. writer.close();
  144. }
  145.  
  146. static BufferedReader reader;
  147. static PrintWriter writer;
  148. static StringTokenizer st;
  149.  
  150. static String next()
  151. {while(st == null || !st.hasMoreTokens()){try{String line = reader.readLine();if(line == null){return null;}
  152. st = new StringTokenizer(line);}catch (Exception e){throw new RuntimeException();}}return st.nextToken();}
  153. static String nextLine() {String s=null;try{s=reader.readLine();}catch(IOException e){e.printStackTrace();}return s;}
  154. static int nextInt() {return Integer.parseInt(next());}
  155. static long nextLong() {return Long.parseLong(next());}
  156. static double nextDouble(){return Double.parseDouble(next());}
  157. static char nextChar() {return next().charAt(0);}
  158. static int[] nextIntArray(int n) {int[] a= new int[n]; int i=0;while(i<n){a[i++]=nextInt();} return a;}
  159. static long[] nextLongArray(int n) {long[]a= new long[n]; int i=0;while(i<n){a[i++]=nextLong();} return a;}
  160. static int[] nextIntArrayOneBased(int n) {int[] a= new int[n+1]; int i=1;while(i<=n){a[i++]=nextInt();} return a;}
  161. static long[] nextLongArrayOneBased(int n){long[]a= new long[n+1];int i=1;while(i<=n){a[i++]=nextLong();}return a;}
  162. static void print(Object o) { writer.print(o); }
  163. static void println(Object o){ writer.println(o);}
  164.  
  165. /************************ TEMPLATE ENDS HERE ************************/
  166.  
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement