列车调度(加了几个注释

https://blog.youkuaiyun.com/u014302425/article/details/79981435

/* #include<stdio.h>
const int maxn=1e5+5;
int main(){
    int n;
    int a[maxn];
    scanf("%d",&n);
    int k, len=0;
    while(n--){
        scanf("%d",&k);
        if(len==0||k>a[len-1]){
            a[len++]=k;//新轨道
        }
        else
        {
            int l=0, r=len-1;
            while(l<r){
                int mid=l+(r-l)/2;
                if(a[mid]>k)
                    r=mid-1;
                else l=mid+1;
            }
            a[l]=k;
        }
    }
    printf("%d",len);
    return 0;
} */
#include<stdio.h>
#include<limits.h>
#include<string.h>
int n;
int che[100001];          //che[]用于储存链尾序号
int N;
int count =1;//总轨道数
int y;
int foun(int n)
{
    //与最后一个相比
    if(n>che[count])
        return 0;
    //下面这个感觉没必要
    if(n<che[count]&&n>che[count-1])
        return count;
    //
    for(y=1;y<=count;y++)
       if(n<che[y])    //检查能否插入
             return y;
 
}
int main()
{
    scanf("%d",&N);
    int i,k,a;
    memset(che,0,sizeof(che));
    che[1]=INT_MAX;
    for(i=1;i<=N;i++)
    {
        scanf("%d",&n);
        if(a=foun(n))
            che[a]=n;   //更新链尾序号
        else            //不可插入链尾,则创建新轨道
        {
            count++;
            che[count]=n;
        }
    }
     printf("%d",count);
 
}
#include<stdio.h>
#include<set>
using namespace std;
int main()
{
	int n,num;
	scanf("%d %d",&n,&num);
	set<int> s;
	s.insert(num);
	for(int i=1;i<n;i++) 
	{
		scanf("%d",&num);
		if(s.lower_bound(num)!=s.end()){
			s.erase(s.lower_bound(num));
		}
		s.insert(num);
	}
	printf("%d",s.size());
	return 0; 
} 

理论上来说,用vector后upper_bound更快一些
g++ -DONLINE_JUDGE -fno-tree-ch -O2 -Wall -std=c++14 -pipe $src -lm -o $exe
开O2优化,PTA

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值