Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. public class onepunchman {
  2. static int r;
  3. static int[] x;
  4. static int[] a;
  5.  
  6. public static void main(String[] args) {
  7. Scanner sc = new Scanner(System.in);
  8. int tc = sc.nextInt();
  9. sc.nextLine();
  10. for (int i = 0; i < tc; i++) {
  11. int n = sc.nextInt();
  12. r = sc.nextInt();
  13. int k = sc.nextInt();
  14. sc.nextLine();
  15. x = new int[n];
  16. a = new int[n];
  17. int total = 0;
  18. for (int j = 0; j < n; j++) {
  19. x[j] = sc.nextInt();
  20. total += sc.nextInt();
  21. a[j] = total;
  22. sc.nextLine();
  23. }
  24. states = new int[n][k + 1];
  25. for (int j = 0; j < n; j++) {
  26. for (int h = 0; h <= k; h++) {
  27. states[j][h] = -1;
  28. }
  29. }
  30. for (int j = n - 1; j >= 0; j--) {
  31. for (int q = 0; q <= k; q++) {
  32. dp(j, q);
  33. }
  34. }
  35. System.out.println(dp(0, k));
  36. }
  37. }
  38.  
  39. static int[][] states;
  40.  
  41. static int dp(int i, int k) {
  42. if (i == x.length) {
  43. return 0;
  44. } else {
  45. if (states[i][k] == -1) {
  46. int value;
  47.  
  48. if (k > 0) {
  49. int low = i;
  50. int high = x.length - 1;
  51. int max = r * 2 + x[i];
  52. int j = i;
  53.  
  54. while (high >= low) {
  55. int middle = (low + high) / 2;
  56. if (x[middle] <= max) {
  57. low = middle + 1;
  58. j = middle;
  59. }
  60. if (x[middle] > max) {
  61. high = middle - 1;
  62. }
  63. }
  64. int s;
  65. if (i > 0) {
  66. s = a[j] - a[i - 1];
  67. } else {
  68. s = a[j];
  69. }
  70. value = Math.max(dp(i + 1, k), dp(j + 1, k - 1) + s);
  71. } else {
  72. value = dp(i + 1, k);
  73. }
  74.  
  75. states[i][k] = value;
  76. return value;
  77. } else {
  78. return states[i][k];
  79. }
  80. }
  81. }
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement