Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- s+=(n+c)-((n*(n+1))/2);
- public static void main(String[] args) {
- int A[]={3,4,8,5,1,4,6,8,7,2,2,4};//output 44
- int K=6;
- //long start = System.currentTimeMillis();;
- //for(int i=0;i<100000;i++){
- System.out.println(mezmeriz4r(A,K));
- //}
- //long end = System.currentTimeMillis();;
- // System.out.println((end - start) + " ms");
- }
- public static int mezmeriz4r(int[]A,int K){
- int s=0;
- int ml=s;
- int mxl=s;
- int sz=1;
- int t=s;
- int c=sz;
- int lol=50000;
- int end=A.length;
- for(int i=sz;i<end;i++){
- if(A[i]>A[mxl]){
- mxl=i;
- }else if(A[i]<A[ml]){
- ml=i;
- }
- if(Math.abs(A[ml]-A[mxl])<=K){
- sz++;
- if(sz>=lol)return 1000000000;
- if(sz>1){
- c+=sz;
- }
- }else{
- if(A[ml]!=A[i]){
- t=i-ml;
- s+=(t+c)-((t*(t+1))/(short)2);
- i=ml;
- ml++;
- mxl=ml;
- }else{
- t=i-mxl;
- s+=(t+c)-((t*(t+1))/(short)2);
- i=mxl;
- mxl++;
- ml=mxl;
- }
- c=1;
- sz=0;
- }
- }
- if(s>1000000000)return 1000000000;
- return s+c;
- }
- public static int solution(int[]A,int K){
- int minIndex=0;
- int maxIndex=0;
- int end=A.length;
- int slize = end;
- int startIndex = 0;
- int diff = 0;
- int minMaxIndexDiff = 0;
- for(int currIndex=1;currIndex<end;currIndex++){
- if(A[currIndex]>A[maxIndex]){
- maxIndex=currIndex;
- }else if(A[currIndex]<A[minIndex]){
- minIndex=currIndex;
- }
- if( (A[maxIndex]-A[minIndex]) >K){
- minMaxIndexDiff= currIndex- startIndex;
- if (minMaxIndexDiff > 1){
- slize+= ((minMaxIndexDiff*(minMaxIndexDiff-1)) >> 1);
- if (diff > 0 ) {
- slize = slize + (diff * minMaxIndexDiff);
- }
- }
- if (minIndex == currIndex){
- diff = currIndex - (maxIndex + 1);
- }else{
- diff = currIndex - (minIndex + 1);
- }
- if (slize > 1000000000) {
- return 1000000000;
- }
- minIndex = currIndex;
- maxIndex = currIndex;
- startIndex = currIndex;
- }
- }
- if ( (startIndex +1) == end){
- return slize;
- }
- if (slize > 1000000000) {
- return 1000000000;
- }
- minMaxIndexDiff= end- startIndex;
- if (minMaxIndexDiff > 1){
- slize+= ((minMaxIndexDiff*(minMaxIndexDiff-1)) >> 1);
- if (diff > 0 ) {
- slize = slize + (diff * minMaxIndexDiff);
- }
- }
- return slize;
- }
- s+=(n+c)-((n*(n+1))/2);
- s+=c-((n*(n-1))>>1);
- t=i-ml;
- s+=(t+c)-((t*(t+1))/(short)2);
- i=ml;
- ml++;
- mxl=ml;
- s+=((t=i-ml)+c);
- s-=((t*(t+1))/(short)2);
- i=ml;
- mxl=++ml;
- if(Math.abs(A[ml]-A[mxl])<=K)
- int diff = A[ml]-A[mxl];
- if(diff<=K && diff>=-K)
- /2
- >>1
- ml++;
- mxl=ml;
- mxl=++ml;
- for(int i=0;i<100000;i++){
- System.out.println(mezmeriz4r(A,K));
- }
- int dummy = 0;
- for(int i=0;i<100000;i++){
- dummy = mezmeriz4r(A,K);
- }
- //Use dummy otherwise optimisation can remove mezmeriz4r
- System.out.print("finished: " + dummy);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement