fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10.  
  11. public static long merge(long[] a,long[] left,long[] right){
  12. int i =0,j=0;
  13. long count=0;
  14. while(i < left.length || j < right.length){
  15. if(i == left.length){
  16. a[i+j] = right[j++];
  17. }
  18. else if(j == right.length){
  19. a[i+j] = left[i++];
  20. }
  21. else if (left[i] <= right[j]){
  22. a[i+j] = left[i++];
  23. }
  24. else{
  25. a[i+j] = right[j++];
  26. count+= left.length-i;
  27. }
  28. }
  29. return count;
  30.  
  31. }
  32.  
  33. public static long iCount(long[] a){
  34. if(a.length < 2)
  35. return 0;
  36. else{
  37. int m = (a.length+1)/2;
  38. long[] left = Arrays.copyOfRange(a,0,m);
  39. long[] right = Arrays.copyOfRange(a,m,a.length);
  40. return iCount(left) + iCount(right) + merge(a,left,right);
  41. }
  42. }
  43.  
  44. public static void main (String[] args) throws java.lang.Exception
  45. {
  46. // your code goes here
  47. PrintWriter out = new PrintWriter(System.out,true);
  48. int t = Integer.parseInt(br.readLine());
  49. long c = 0;
  50. while(t-->0){
  51. c++;
  52. StringTokenizer st = new StringTokenizer(br.readLine());
  53. int n = Integer.parseInt(st.nextToken());
  54. long k = Long.parseLong(st.nextToken());
  55. st = new StringTokenizer(br.readLine());
  56. long[] a = new long[n];
  57. for(int i =0;i<n;i++){
  58. a[i] = Integer.parseInt(st.nextToken());
  59. }
  60. long I = iCount(a);
  61. if(k <= I){
  62. out.println("Case "+c+": "+(I-k));
  63. }
  64. else{
  65. if((k&1) == 1)out.println("Case "+c+": "+1);
  66. else out.println("Case "+c+": "+0);
  67. }
  68. }
  69. }
  70. }
Success #stdin #stdout 0.1s 320512KB
stdin
3
3 3
3 2 1
3 1
1 2 3
3 2
1 2 3
stdout
Case 1: 0
Case 2: 1
Case 3: 0