2104 Deleting items
2013 Provincial team audition in Jilin
time limit: 1 sspace limit: 128000 KBtopic rank: Master Master SolvingView Run ResultsTitle Description
Description
The case redistribution problem needs to address the following issues:
(1) A total of n items, piled into m heap.
(2) All items are the same, but they have different priorities.
(3) You can only move items at the top of a pile.
(4) You can move any item in the top of any pile to the top of another heap. If the item is the highest priority in all current items, it can be deleted without moving it directly.
(5) Find out the minimum number of steps required to remove all items. The delete operation does not count toward the number of steps.
(6) Just a more difficult problem to solve, here you just need to solve a relatively simple version:
No two items have the same priority, and m=2
Enter a description
Input Description
The first line consists of two integer n1,n2 that represent the number of items in the two stacks.
Next, N1 line integers give priority to the first pile of items according to the order from top to bottom, and the higher the number, the greater the priority.
The next N2 line gives priority to the second pile of items in the same format.
Output description
Output Description
For each data, output an integer, which is the minimum number of steps to move.
Sample input
Sample Input
7 ·
1
4
5
2
7
3
Sample output
Sample Output
6
Data range and Tips
Data Size & Hint
1<=n1+n2<=100 for 20% of data
1<=n1+n2<=1000 for 40% of data
For all data, there are 1<=n1+n2<=100000
Category labels
Tags Click here to expandSegment tree tree structure Jilin Provincial Team Tryouts 2013
The following:Segment Tree + Simulation
AC Code:
#include <cstdio>#include<iostream>#include<algorithm>using namespacestd;#defineN 100010structnode{intx, y;} A[n];intN,n1,n2,pos,c[n];inlineBOOLcmpConstNode &p,ConstNode &q) { returnP.y>q.y;}intLowbit (intx) { returnx&-x;} InlinevoidUpdata (intp) { for(intI=p;i<=n;i+=lowbit (i)) c[i]+=1;} InlineintQueryintp) { intAns0); for(intI=p;i;i-=lowbit (i)) ans+=C[i]; returnans;}intMain () {scanf ("%d%d", &n1,&n2); n=n1+n2;pos=N1; for(inti=n1;i;i--) scanf ("%d",&a[i].y); for(inti=n1+1; i<=n;i++) scanf ("%d",&a[i].y); for(intI=1; i<=n;i++) a[i].x=i; Sort (a+1, a+n+1, CMP); Long LongAns0); for(intI=1; i<=n;i++){ if(A[i].x<=pos) Ans+=pos-a[i].x-query (POS) +query (a[i].x), pos=a[i].x; Elseans+=a[i].x-1-pos-query (a[i].x-1) +query (POS), pos=a[i].x-1; Updata (a[i].x); } printf ("%lld\n", ans); return 0;}
2104 Deleting items