Can someone please help me with what is wrong with my solution?
The idea is to either pick a interval from start till the first smallest
value (suppose if smallest is 3, then we can have win for 1 and 2 by
picking 2), largest value to K (suppose if largest is 7 and k is 10, then
we can have win for 8,9 and 10 by picking 8) as well as other possibilites
which will be in between intervals (like [3, 7]--> pick 4, then win for
4,5).
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
PrintWriter out = new PrintWriter(System.out);
StringBuilder str = new StringBuilder("");
for(int h=1;h<=t;h++)
{
str.append("Case #"+h+": ");
String[] in = br.readLine().trim().split(" ");
int n = Integer.parseInt(in[0]);
long k = Long.parseLong(in[1]);
long arr[] = new long[n];
in = br.readLine().trim().split(" ");
for(int i=0;i<n;i++){
arr[i]=Long.parseLong(in[i]);
}
List<Long> diff = new ArrayList<>();
Arrays.sort(arr);
for(int i=1;i<arr.length;i++){
long d = arr[i]-arr[i-1];
if(d>0){
d--;
}
diff.add(d);
}
Double p = new Double(0);
Collections.sort(diff);
List<Long> ans = new ArrayList<>();
ans.add(k - arr[n-1]);
ans.add(arr[0]-1);
for(int j = diff.size()-1;j>=Math.max(0, diff.size()-2);j--){
ans.add((diff.get(j)+1) / 2);
}
Collections.sort(ans);
p += ans.get(ans.size()-1);
p+=ans.get(ans.size()-2);
str.append(p/(double)k+"\n");
}
out.print(str);
out.flush();
br.close();
}
}
--
-- You received this message because you are subscribed to the Google Groups
Code Jam group. To post to this group, send email to
[email protected]. To unsubscribe from this group, send email to
[email protected]. For more options, visit this group at
https://groups.google.com/d/forum/google-code?hl=en
---
You received this message because you are subscribed to the Google Groups
"Google Code Jam" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/google-code/cba4b486-e06a-4d73-9908-ca61677ae6e3n%40googlegroups.com.