地址:http://codeforces.com/contest/876/problem/D
题目:
Recently, Dima met with Sasha in a philatelic store, and since then they are collecting coins together. Their favorite occupation is to sort collections of coins. Sasha likes having things in order, that is why he wants his coins to be arranged in a row in such a way that firstly come coins out of circulation, and then come coins still in circulation.
For arranging coins Dima uses the following algorithm. One step of his algorithm looks like the following:
- He looks through all the coins from left to right;
- If he sees that the i-th coin is still in circulation, and (i + 1)-th coin is already out of circulation, he exchanges these two coins and continues watching coins from (i + 1)-th.
Dima repeats the procedure above until it happens that no two coins were exchanged during this procedure. Dima calls hardness of ordering the number of steps required for him according to the algorithm above to sort the sequence, e.g. the number of times he looks through the coins from the very beginning. For example, for the ordered sequence hardness of ordering equals one.
Today Sasha invited Dima and proposed him a game. First he puts n coins in a row, all of them are out of circulation. Then Sasha chooses one of the coins out of circulation and replaces it with a coin in circulation for n times. During this process Sasha constantly asks Dima what is the hardness of ordering of the sequence.
The task is more complicated because Dima should not touch the coins and he should determine hardness of ordering in his mind. Help Dima with this task.
The first line contains single integer n (1 ≤ n ≤ 300 000) — number of coins that Sasha puts behind Dima.
Second line contains n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) — positions that Sasha puts coins in circulation to. At first Sasha replaces coin located at position p1, then coin located at position p2 and so on. Coins are numbered from left to right.
Print n + 1 numbers a0, a1, ..., an, where a0 is a hardness of ordering at the beginning, a1 is a hardness of ordering after the first replacement and so on.
4
1 3 4 2
1 2 3 2 1
8
6 8 3 4 7 2 1 5
1 2 2 3 4 3 4 5 1
Let's denote as O coin out of circulation, and as X — coin is circulation.
At the first sample, initially in row there are coins that are not in circulation, so Dima will look through them from left to right and won't make any exchanges.
After replacement of the first coin with a coin in circulation, Dima will exchange this coin with next three times and after that he will finally look through the coins and finish the process.
XOOO → OOOX
After replacement of the third coin, Dima's actions look this way:
XOXO → OXOX → OOXX
After replacement of the fourth coin, Dima's actions look this way:
XOXX → OXXX
Finally, after replacement of the second coin, row becomes consisting of coins that are in circulation and Dima will look through coins from left to right without any exchanges.
思路:
观察样例可以发现,次数=x的数量-末尾有几个连续的x+1。
维护末尾有几个连续的x可以用并查集。
然后就没有然后了
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define MP make_pair 6 #define PB push_back 7 typedef long long LL; 8 typedef pair<int,int> PII; 9 const double eps=1e-8; 10 const double pi=acos(-1.0); 11 const int K=1e6+7; 12 const int mod=1e9+7; 13 14 int n,a[K],f[K],cnt[K],b[K]; 15 int fd(int x) 16 { 17 return f[x]==x?x:f[x]=fd(f[x]); 18 } 19 void join(int x,int y) 20 { 21 int fx=fd(x),fy=fd(y); 22 if(fx!=fy) 23 { 24 cnt[fy]+=cnt[fx]; 25 f[fx]=fy; 26 } 27 } 28 int main(void) 29 { 30 cin>>n; 31 for(int i=1;i<=n;i++) 32 scanf("%d",a+i),f[i]=i,cnt[i]=1; 33 printf("1 "); 34 for(int i=1,ans;i<=n;i++) 35 { 36 b[a[i]]=1; 37 if(a[i]-1>0&&b[a[i]-1]) join(a[i],a[i]-1); 38 if(a[i]+1<=n&&b[a[i]+1]) join(a[i],a[i]+1); 39 if(b[n]==1) ans=i-cnt[fd(n)]; 40 else ans=i; 41 printf("%d ",ans+1); 42 } 43 return 0; 44 }