codeforces round #441 D

来源:互联网 发布:python 交易策略 模拟 编辑:程序博客网 时间:2024/06/05 19:23
D. 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:

  1. He looks through all the coins from left to right;
  2. 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.

给你一列数字a[i]为第i个硬币入列位置

让你求按冒泡排序方法下使序列有序的次数(指标从头到尾算一次)

注意每次硬币入列前的初始状态是没有冒泡过的

我们发现每次冒泡必然会有一个硬币被移到最后面

所以要求的即不在最后面的硬币的个数

由于第i+1次最后面的硬币数不会少于i次

可以通过打tag转移从而避免超时

#include<cstdio>using namespace std;int n,a[300001],f[300001];int main(){scanf("%d",&n);int t=0,x,bj=0;for (int i=1;i<=n;i++){scanf("%d",&x);a[x]=1;int sum=i;if (a[n-bj]==1)while (a[n-bj]==1){bj++;}f[i]=sum+1-bj;}printf("1 ");f[n]=1;for (int i=1;i<=n;i++)printf("%d ",f[i]);return 0;}



原创粉丝点击