Hi all, I just write my solution to solve problem 285C. but i has "Time limit exceeded on test 8" error, but another person wrote a solution like mine, but it doesn't have this probelm, what's wrong? thanks for any help. my code:
public class Main {
public static void main(String[] args) {
// TODO code application logic here
Scanner sc=new Scanner(System.in);
PrintWriter pw=new PrintWriter(System.out);
long c=0;
int n=sc.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
Arrays.sort(a);
for(int i=0;i<n;i++)
c+=Math.abs(a[i]-(i+1));
pw.print(c);
pw.flush();
}
}
other guy's code:
public class Contest {
public static void main (String [] args) {
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = in.nextInt();
List <Integer> aa = new ArrayList <Integer> ();
int [] a = new int[n];
int t = 7;
long counter = 0;
for(int i = 0; i < n; ++i) {
a[(i + t) % n] = in.nextInt();
}
Arrays.sort(a);
for(int i = 0; i < n; ++i){
counter += Math.abs((i + 1) - a[i]);
}
out.println(counter);
out.flush();
}
}