Created
December 21, 2016 17:38
-
-
Save Jack-Saleem/f91982eb69bd2190631bd1ad88d8492b to your computer and use it in GitHub Desktop.
codeforces 567A Lineland Mail program in java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class LinelandMail | |
{ | |
static int sub(int a,int b){ | |
if(a >= 0 && b >= 0){ | |
return Math.abs(a-b); | |
}else if(a <= 0 && b <= 0){ | |
return Math.abs((a*-1)-(b*-1)); | |
}else if(a >= 0 && b <= 0){ | |
return Math.abs(a+(b*-1)); | |
}else if(b >= 0 && a <= 0){ | |
return Math.abs(b+(a*-1)); | |
}else | |
return 0; | |
} | |
static int getMinMax(int i,int j,int n,int[] ip,int[] op){ | |
if(i == 0){ | |
op[j] = LinelandMail.sub(ip[0], ip[1]); | |
op[++j] = LinelandMail.sub(ip[0], ip[n-1]); | |
}else if(i == n-1){ | |
op[++j] = LinelandMail.sub(ip[n-1], ip[n-2]); | |
op[++j] = LinelandMail.sub(ip[n-1], ip[0]); | |
}else{ | |
int mina = LinelandMail.sub(ip[i], ip[i+1]); | |
int minb = LinelandMail.sub(ip[i], ip[i-1]); | |
int minc = LinelandMail.sub(ip[i], ip[0]); | |
int mind = LinelandMail.sub(ip[i], ip[n-1]); | |
op[++j] = Math.min(mina, minb); | |
op[++j] = Math.max(minc, mind); | |
} | |
return j; | |
} | |
public static void main(String[] args) | |
{ | |
Scanner z = new Scanner(System.in); | |
int n = z.nextInt(); | |
int[] ip = new int[n]; | |
int[] op = new int[n*2]; | |
for(int i=0;i<n;i++) | |
ip[i] = z.nextInt(); | |
int j=0; | |
for(int i=0;i<n;i++){ | |
if(ip[i] < 0){ | |
j = LinelandMail.getMinMax(i, j, n, ip, op); | |
}else{ | |
j = LinelandMail.getMinMax(i, j, n, ip, op); | |
} | |
} | |
for(int i=0;i<n*2-1;i+=2) | |
System.out.println(op[i]+" "+op[i+1]); | |
z.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment