Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Input
- 5 4
- 4 3 2 5 1
- 1 2 1
- 3 3 2
- 2 2 3
- 1 1 2
- Output
- 1
- 2
- 3
- 5
- as:
- After Q1, A = {2, 5, 1}, B = {4, 3}. Therefore, inversion count in B is 1.
- After Q2, A = {2, 5}, B = {4, 1, 3}. Therefore, inversion count in B is 2.
- After Q3, A = {2}, B = {4, 1, 5, 3}. Therefore, inversion count in B is 3.
- After Q4, A = {}, B = {4, 2, 1, 5, 3}. Therefore, inversion count in B is 5.
- import java.util.*;
- class nick {
- public static void main(String args[])
- {
- int i,t=0;
- Scanner b = new Scanner(System.in);
- int n=b.nextInt();
- int q=b.nextInt();
- String x="";
- b.nextLine();
- String s=b.nextLine();
- s=s.replace(" ","");
- int l1=0;
- StringBuffer sb = new StringBuffer(" ");
- while(q>0)
- {
- t=0;
- s=s+"";
- int l=b.nextInt();
- int r=b.nextInt();
- int k=b.nextInt();
- x=s.substring(l-1,r);
- l1=l1+x.length();
- sb.insert(k-1,x);
- s=s.replace(x,"");
- int j=0;
- i=0;
- for(i=0;i<l1-1;i++)
- {
- for(j=i+1;j<l1;j++)
- {
- if(sb.charAt(i)>sb.charAt(j))
- t++;
- }
- }
- System.out.println(t);
- q--;
- }
- b.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement