Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. package vn.nvanhuong.coding.exercise;
  2.  
  3. import java.util.Scanner;
  4.  
  5. /**
  6. * All squares (original & sub-squares) has these following characteristics:
  7. * [1]. sorted entries
  8. * + right entry - left entry = 1
  9. * + below entry - above entry = dimension
  10. * [2]. if we know smallest entry (T), dimension (N) & orientation -> we can know the rest entries
  11. */
  12. public class Solution {
  13. public static void main(String[] args) {
  14. //Step 1. Find smallest entry of each input square. Time complexity: O(S)
  15. //The value at cell (i,j) of a great square whose smallest entry is T is T + i*N + j
  16.  
  17. Scanner scanner = new Scanner(System.in);
  18. int n = scanner.nextInt(); //N: size of original square
  19. int s = scanner.nextInt(); //S: numbers of commands
  20.  
  21. long[] a = new long[s+1]; //entered rows
  22. long[] b = new long[s+1]; //entered columns
  23. long[] d = new long[s+1]; //entered dimension
  24. long[] t = new long[s+1]; //T: found smallest entries
  25.  
  26. //init the original square (already known)
  27. a[0] = 1;
  28. b[0] = 1;
  29. d[0] = n-1;
  30. t[0] = 0;
  31.  
  32. //input and find smallest entries
  33. int orientationNum = 4;
  34. for (int i = 1; i <= s; i++) {
  35. a[i] = scanner.nextInt();
  36. b[i] = scanner.nextInt();
  37. d[i] = scanner.nextInt();
  38.  
  39. /* for example: T0 = 0, N = 7
  40. * 0 1 2 3 4 5 6
  41. * 7 8 9 10 11 12 13
  42. * 14 15 16 17 18 19 20
  43. * 21 22 23 24 25 26 27
  44. * 28 29 30 31 32 33 34
  45. * 35 36 37 38 39 40 41
  46. * 42 43 44 45 46 47 48
  47. * */
  48.  
  49. //apply characteristic [2] and formula: V = T + i*N + j
  50. if (i % orientationNum == 1) {//TOP
  51.  
  52. /* command (1,2,4) -> T1 = 0 + (1-1)*7 + (2-1) = 1
  53. /* % 29 22 15 8 1 %
  54. * % 30 23 16 9 2 %
  55. * % 31 24 17 10 3 %
  56. * % 32 25 28 11 4 %
  57. * % 33 26 19 12 5 %
  58. * % % % % % % %
  59. * % % % % % % %
  60. * */
  61. t[i] = t[i-1] + (a[i]-a[i-1])*n + (b[i]-b[i-1]);
  62. } else if (i % orientationNum == 2) {//RIGHT
  63.  
  64. /* command (2,3,3) -> T2 = 1 + ((2+4)-(3+3))*7 + (2-1) = 2
  65. /* % % % % % % %
  66. * % % 26 25 24 23 %
  67. * % % 19 18 17 16 %
  68. * % % 12 11 10 9 %
  69. * % % 5 4 3 2 %
  70. * % % % % % % %
  71. * % % % % % % %
  72. * */
  73. t[i] = t[i-1]+((b[i-1]+d[i-1])-(b[i]+d[i]))*n+(a[i]-a[i-1]);
  74. } else if (i % orientationNum == 3) {//BOTTOM
  75. t[i] = t[i-1]+((a[i-1]+d[i-1])-(a[i]+d[i]))*n+((b[i-1]+d[i-1])-(b[i]+d[i]));
  76. } else if (i % orientationNum == 0) {//LEFT
  77. t[i] = t[i-1]+(b[i]-b[i-1])*n+((a[i-1]+d[i-1])-(a[i]+d[i]));
  78. }
  79. }
  80.  
  81. //Step 2. Determine some values
  82. //Each square is contained in the previous one -> binary search. Time complexity: O(log S)
  83. //The location of a value v in a great square whose smallest entry is T is ((v-T)/N, (v-T)%N)
  84.  
  85. int l = scanner.nextInt(); //L: number of queries
  86. long[] w = new long[l];
  87. for (int i = 0; i < l; i++) {
  88. w[i] = scanner.nextLong();
  89. int low = 0;
  90. int high = s;
  91.  
  92. //find position of smallest entry
  93. while (low != high) {
  94. int mid = (low+high+1)/2;
  95. if (w[i] >= t[mid] && w[i] < t[mid]+(d[mid]+1)*n && w[i]%n >= t[mid]%n && w[i]%n <= (t[mid]%n)+d[mid]){
  96. low = mid;
  97. }else{
  98. high = mid-1;
  99. }
  100. }
  101.  
  102. //find position of input number with formula: (i, j) = ((v-T)/N, (v-T)%N)
  103. long off1 = (w[i]-t[low])/n;
  104. long off2 = (w[i]-t[low])%n;
  105.  
  106. if (low % orientationNum == 0) {//TOP
  107. System.out.println((a[low]+off1)+" "+(b[low]+off2));
  108. } else if (low % orientationNum == 1) {//RIGHT
  109. System.out.println((a[low]+off2)+" "+(b[low]+d[low]-off1));
  110. } else if (low % orientationNum == 2) {//BOTTOM
  111. System.out.println((a[low]+d[low]-off1)+" "+(b[low]+d[low]-off2));
  112. } else if (low % orientationNum == 3) {//LEFT
  113. System.out.println((a[low]+d[low]-off2)+" "+(b[low]+off1));
  114. }
  115. }
  116.  
  117. scanner.close();
  118. }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement