2016年团体程序设计天梯赛-决赛 L2-2 列车调度

以为PAT数据弱,结果这题就吃了大亏。乱搞一发O(nk)复杂度的不让过,想了好久总觉得不得劲;感觉可以根据列车编号直接用某种规律直接推出来。

后来百度一发:一个数组的最少下降序列的个数正好等于其最大上升子序列的长度。这个规律不是很好发现啊。

我用的是下面的方法:

n<10^5,其实nlogn算法很轻松的就可以过了,但是我一直没往这方面想……只要把查找last序列中第一个大于X的数用logn实现就行了,而last正好是升序的,这不二分还等什么。百度发现有人用了一个学了很多遍从来不记得用的函数:upper_bound,作用在这里和二分差不多,正好可以找到第一个大于key值的元素,upper_bound应该是用红黑树实现的,速度也非常快,logn。

/* ***********************************************
Author        :angon
************************************************ */
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define REP(i,k,n) for(int i=k;i<n;i++)
#define REPP(i,k,n) for(int i=k;i<=n;i++)
#define scan(d) scanf("%d",&d)
#define scann(n,m) scanf("%d%d",&n,&m)
#define mst(a,k)  memset(a,k,sizeof(a));
#define LL long long
#define maxn 100005
#define mod 100000007
/*
inline int read()
{
    int s=0;
    char ch=getchar();
    for(; ch<'0'||ch>'9'; ch=getchar());
    for(; ch>='0'&&ch<='9'; ch=getchar())s=s*10+ch-'0';
    return s;
}
inline void print(int x)
{
    if(!x)return;
    print(x/10);
    putchar(x%10+'0');
}
*/


int last[maxn];
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int n,x,k;
    scan(n);
    scan(x);
    last[0]=x; k=1;
    REP(i,1,n)
    {
        scan(x);
        if(x>last[k-1])
            last[k++]=x;
        else
        {
            int l=0,r=k-1;
            while(l<r)
            {
                int mid=(l+r)>>1;
                if(last[mid]<x)
                    l=mid+1;
                else
                    r=mid-1;
            }
            //int l = upper_bound(last,last+k,x)-last; 找到[a,b)区间第一个大于x的位置
            last[l]=x;
        }
    }
    printf("%d\n",k);


    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值