Codeforces Round #441 D. Sorting the Coins(线段树)

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.

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 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.

Output
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.

题意还是比较简单的,用类似冒泡排序的方法给流通中和非流通的硬币排序,使得排在前面的都是非流通的硬币,现在n个硬币都是非流通,然后每一次将某个位置的硬币替换成流通的,问你当前序列需要进行几次排序过程(每扫一遍为一次)。

1.硬币如果是有序的则只需要1次(来确定已经有序了)。
2.经过简单的观察,每次排序会让所有非流通币的顺序往前移动一个位置(如果能往前,即前面是流通币).
所以,每个已知硬币序列,从左往右可以对每个非流通币编号(1,2,3…)(这里的编号其实就是它排序完成后的位置),那么这个序列所需要的排序的次数就是max{非流通币的位置减去其的编号}+1。

所以我们将一个位置的非流通币换成流通币时,右边所有的非流通币(如果存在)的编号都会-1,所以其(位置-编号)的值就会加1,我们可以通过这样的区间维护来完成这道题。

我才用了维护最大值线段树,每次p位置替换硬币就让p位置的值置为-1,[p+1,n]位置的值都+1,然后查询就就是查[1,n]的最大值。

注意区间更新的时候不能去改那些已经是-1的结点了,详见代码
(然而我看别人用的别人方法好像都很好实现诶)

#include<bits/stdc++.h>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<string.h>
typedef long long ll;
using namespace std;
const int maxn=300005;
const int maxm=400005;
const int inf=0x3f3f3f3f;
int n,m,k;
int tr[4*maxn],lazy[4*maxn];

void pushup(int cnt){
    tr[cnt]=max(tr[cnt<<1],tr[cnt<<1|1]);
}

void pushdown(int cnt){
    if(lazy[cnt]){
        if(tr[cnt<<1]!=-1){
            tr[cnt<<1]+=lazy[cnt];
            lazy[cnt<<1]+=lazy[cnt];
        }
        if(tr[cnt<<1|1]!=-1){
            tr[cnt<<1|1]+=lazy[cnt];
            lazy[cnt<<1|1]+=lazy[cnt];
        }
        lazy[cnt]=0;
    }
}

void update(int cnt,int l,int r,int p){
    if(l==r){tr[cnt]=-1;return;}
    int mid=(l+r)>>1;
    pushdown(cnt);
    if(p<=mid)update(cnt<<1,l,mid,p);
    else update(cnt<<1|1,mid+1,r,p);
    pushup(cnt);
}

void qupdate(int cnt,int l,int r,int lef,int rig){
    if(lef<=l&&r<=rig){
        if(tr[cnt]==-1)return;
        tr[cnt]++;
        lazy[cnt]++;
        return;
    }
    int mid=(l+r)>>1;
    pushdown(cnt);
    if(lef<=mid)qupdate(cnt<<1,l,mid,lef,rig);
    if(rig>mid)qupdate(cnt<<1|1,mid+1,r,lef,rig);
    pushup(cnt);
}

int query(int cnt,int l,int r,int lef,int rig){
    if(lef<=l&&r<=rig){return tr[cnt];}
    int mid=(l+r)>>1;
    pushdown(cnt);
    int ans=0;
    if(l<=mid)ans=max(ans,query(cnt>>1,l,mid,lef,rig));
    if(r>mid)ans=max(ans,query(cnt>>1|1,mid+1,r,lef,rig));
    return ans;
}

int main(){
    int a;
    scanf("%d",&n);
    printf("1 ");
    for(int i=0;i<n;i++){
        scanf("%d",&a);
        update(1,1,n,a);
        if(a+1<=n)
        qupdate(1,1,n,a+1,n);
        int ans=1+query(1,n,1,n,1);
        if(ans==0)ans++;
        printf("%d ",ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值