Advertisement
Guest User

Untitled

a guest
Aug 30th, 2015
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. Input
  2.  
  3. 5 4
  4.  
  5. 4 3 2 5 1
  6.  
  7. 1 2 1
  8.  
  9. 3 3 2
  10.  
  11. 2 2 3
  12.  
  13. 1 1 2
  14.  
  15. Output
  16.  
  17. 1
  18.  
  19. 2
  20.  
  21. 3
  22.  
  23. 5
  24.  
  25. as:
  26. After Q1, A = {2, 5, 1}, B = {4, 3}. Therefore, inversion count in B is 1.
  27. After Q2, A = {2, 5}, B = {4, 1, 3}. Therefore, inversion count in B is 2.
  28. After Q3, A = {2}, B = {4, 1, 5, 3}. Therefore, inversion count in B is 3.
  29. After Q4, A = {}, B = {4, 2, 1, 5, 3}. Therefore, inversion count in B is 5.
  30.  
  31. import java.util.*;
  32. class nick {
  33. public static void main(String args[])
  34. {
  35. int i,t=0;
  36. Scanner b = new Scanner(System.in);
  37. int n=b.nextInt();
  38. int q=b.nextInt();
  39. String x="";
  40. b.nextLine();
  41. String s=b.nextLine();
  42. s=s.replace(" ","");
  43. int l1=0;
  44. StringBuffer sb = new StringBuffer(" ");
  45. while(q>0)
  46. {
  47. t=0;
  48. s=s+"";
  49. int l=b.nextInt();
  50. int r=b.nextInt();
  51. int k=b.nextInt();
  52. x=s.substring(l-1,r);
  53. l1=l1+x.length();
  54. sb.insert(k-1,x);
  55. s=s.replace(x,"");
  56. int j=0;
  57. i=0;
  58. for(i=0;i<l1-1;i++)
  59. {
  60.  
  61. for(j=i+1;j<l1;j++)
  62. {
  63.  
  64. if(sb.charAt(i)>sb.charAt(j))
  65. t++;
  66. }
  67. }
  68. System.out.println(t);
  69. q--;
  70. }
  71. b.close();
  72. }
  73.  
  74.  
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement