JZOJ100041. 【NOIP2017提高A组模拟7.12】列车调度

这里写图片描述

分析

我们可以知道,如果最后的顺序是n、n-1、…、1
那么没有条轨道的序号一定是大到小的。
很自然地可以想到用权值线段树来维护,
每次加入一个新的列车,就在线段树里面找出第一个比它大的
然后就将这个列车加入到这条轨道。
如果找不到比它大的,就新开一条轨道。

通过观察发现,每条轨道的第一个组成了原序列的最长上升子序列。
也就是说我们最需要求出最长上升子序列就可以得出答案了。

code

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string.h>
#include <cmath>
#include <math.h>
#define N  100003
#define ll long long
using namespace std;
int n,m,a[N],tr[4*N],x;
char ch;
void read(int &n)
{
    n=0;
    ch=getchar();
    while((ch<'0' || ch>'9') && ch!='-')ch=getchar();
    int w=1;
    if(ch=='-')w=-1,ch=getchar();
    while('0'<=ch && ch<='9')n=n*10+ch-'0',ch=getchar();
    n*=w;
}
void change(int x,int y,int z,int l,int r)
{
    if(l==r)
    {
        tr[x]+=z;
        return;
    }
    int m=(l+r)/2;
    if(y<=m)change(x+x,y,z,l,m);
        else change(x+x+1,y,z,m+1,r);
    tr[x]=tr[x+x]+tr[x+x+1];
}
int find(int x,int y,int l,int r)
{
    if(!tr[x])return 0;
    if(l==r)return l; 
    int m=(l+r)/2,ans=0;
    if(y<=m)
    {
        if(tr[x+x])ans=find(x+x,y,l,m);
    } 
    if(ans==0)ans=find(x+x+1,y,m+1,r);
    return ans;
}
int main()
{
    read(n);
    for(int i=1;i<=n;i++)
        read(a[i]);
    for(int i=1;i<=n;i++)
    {
        x=find(1,a[i]+1,1,n);
        if(x==0)
        {
            m++;
            change(1,a[i],1,1,n);
        }
        else
        {
            change(1,x,-1,1,n);
            change(1,a[i],1,1,n);
        }
    }
    printf("%d",m);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值