B. Sorting the Coins
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output
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:
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.
Input
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 p_1, _p_2, …, _p__n (1 ≤ p__i ≤ n) — positions that Sasha puts coins in circulation to. At first Sasha replaces coin located at position _p_1, then coin located at position _p_2 and so on. Coins are numbered from left to right.
Output
Print n + 1 numbers a_0, _a_1, …, _a__n, where _a_0 is a hardness of ordering at the beginning, _a_1 is a hardness of ordering after the first replacement and so on.
Examples
Input
4
1 3 4 2
Output
1 2 3 2 1
Input
8
6 8 3 4 7 2 1 5
Output
1 2 2 3 4 3 4 5 1
Note
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.
这一题真难读啊啊啊 读了好久都不明所以
题意:我跟着note的理解是:遍历数组,遇到x后面是o,就将二者变换位置,直到x在最后或者x后面是x为止,输出要遍历多少次。
解题思路:这样只需要每次遍历都记录下最后一个o前面有多少个x就行啦,代码交上去ac了,看来这种思路是对的。
ac代码:
1 #include
2 #include
3 #include
4 #include
5 using namespace std;
6 const int maxn = 3*1e5+5;
7 int nu[maxn];
8 int mp[maxn];
9 int main() {
10 ios::sync_with_stdio(false);
11 int n;
12 cin>>n;
13 for(int i=1;i<=n;++i) {
14 cin>>nu[i];
15 }
16 int len=n,ans=1;
17 cout<<1;
18 for(int i=1;i<=n;++i) {
19 mp[nu[i]]=1;
20 ans++;
21 while(mp[len]) {
22 len--;
23 ans--;
24 }
25 cout<<" "<<ans;
26 }
27 return 0;
28 }
手机扫一扫
移动阅读更方便
你可能感兴趣的文章