Advertisement
Guest User

Untitled

a guest
Oct 25th, 2014
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. s+=(n+c)-((n*(n+1))/2);
  2.  
  3. public static void main(String[] args) {
  4.  
  5. int A[]={3,4,8,5,1,4,6,8,7,2,2,4};//output 44
  6. int K=6;
  7. //long start = System.currentTimeMillis();;
  8. //for(int i=0;i<100000;i++){
  9. System.out.println(mezmeriz4r(A,K));
  10. //}
  11. //long end = System.currentTimeMillis();;
  12.  
  13. // System.out.println((end - start) + " ms");
  14.  
  15. }
  16. public static int mezmeriz4r(int[]A,int K){
  17. int s=0;
  18. int ml=s;
  19. int mxl=s;
  20. int sz=1;
  21. int t=s;
  22. int c=sz;
  23. int lol=50000;
  24. int end=A.length;
  25. for(int i=sz;i<end;i++){
  26. if(A[i]>A[mxl]){
  27. mxl=i;
  28. }else if(A[i]<A[ml]){
  29. ml=i;
  30. }
  31. if(Math.abs(A[ml]-A[mxl])<=K){
  32. sz++;
  33. if(sz>=lol)return 1000000000;
  34. if(sz>1){
  35. c+=sz;
  36. }
  37. }else{
  38. if(A[ml]!=A[i]){
  39. t=i-ml;
  40. s+=(t+c)-((t*(t+1))/(short)2);
  41. i=ml;
  42. ml++;
  43. mxl=ml;
  44. }else{
  45. t=i-mxl;
  46. s+=(t+c)-((t*(t+1))/(short)2);
  47. i=mxl;
  48. mxl++;
  49. ml=mxl;
  50. }
  51. c=1;
  52. sz=0;
  53. }
  54. }
  55. if(s>1000000000)return 1000000000;
  56. return s+c;
  57. }
  58.  
  59. public static int solution(int[]A,int K){
  60. int minIndex=0;
  61. int maxIndex=0;
  62. int end=A.length;
  63. int slize = end;
  64. int startIndex = 0;
  65. int diff = 0;
  66. int minMaxIndexDiff = 0;
  67. for(int currIndex=1;currIndex<end;currIndex++){
  68. if(A[currIndex]>A[maxIndex]){
  69. maxIndex=currIndex;
  70. }else if(A[currIndex]<A[minIndex]){
  71. minIndex=currIndex;
  72. }
  73. if( (A[maxIndex]-A[minIndex]) >K){
  74. minMaxIndexDiff= currIndex- startIndex;
  75. if (minMaxIndexDiff > 1){
  76. slize+= ((minMaxIndexDiff*(minMaxIndexDiff-1)) >> 1);
  77. if (diff > 0 ) {
  78. slize = slize + (diff * minMaxIndexDiff);
  79. }
  80. }
  81.  
  82. if (minIndex == currIndex){
  83. diff = currIndex - (maxIndex + 1);
  84. }else{
  85. diff = currIndex - (minIndex + 1);
  86. }
  87. if (slize > 1000000000) {
  88. return 1000000000;
  89. }
  90. minIndex = currIndex;
  91. maxIndex = currIndex;
  92. startIndex = currIndex;
  93. }
  94. }
  95. if ( (startIndex +1) == end){
  96. return slize;
  97. }
  98. if (slize > 1000000000) {
  99. return 1000000000;
  100. }
  101. minMaxIndexDiff= end- startIndex;
  102. if (minMaxIndexDiff > 1){
  103. slize+= ((minMaxIndexDiff*(minMaxIndexDiff-1)) >> 1);
  104. if (diff > 0 ) {
  105. slize = slize + (diff * minMaxIndexDiff);
  106. }
  107. }
  108.  
  109. return slize;
  110. }
  111.  
  112. s+=(n+c)-((n*(n+1))/2);
  113.  
  114. s+=c-((n*(n-1))>>1);
  115.  
  116. t=i-ml;
  117. s+=(t+c)-((t*(t+1))/(short)2);
  118. i=ml;
  119. ml++;
  120. mxl=ml;
  121.  
  122. s+=((t=i-ml)+c);
  123. s-=((t*(t+1))/(short)2);
  124. i=ml;
  125. mxl=++ml;
  126.  
  127. if(Math.abs(A[ml]-A[mxl])<=K)
  128.  
  129. int diff = A[ml]-A[mxl];
  130. if(diff<=K && diff>=-K)
  131.  
  132. /2
  133.  
  134. >>1
  135.  
  136. ml++;
  137. mxl=ml;
  138.  
  139. mxl=++ml;
  140.  
  141. for(int i=0;i<100000;i++){
  142. System.out.println(mezmeriz4r(A,K));
  143. }
  144.  
  145. int dummy = 0;
  146. for(int i=0;i<100000;i++){
  147. dummy = mezmeriz4r(A,K);
  148. }
  149. //Use dummy otherwise optimisation can remove mezmeriz4r
  150. System.out.print("finished: " + dummy);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement