【PAT L2-014】列车调度(Dilworth定理)

【PAT L2-014】列车调度(Dilworth定理)


L2-014. 列车调度

时间限制
300 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
陈越

火车站的列车调度铁轨的结构如下图所示。


Figure

两端分别是一条入口(Entrance)轨道和一条出口(Exit)轨道,它们之间有N条平行的轨道。每趟列车从入口可以选择任意一条轨道进入,最后从出口离开。在图中有9趟列车,在入口处按照{8,4,2,5,3,9,1,6,7}的顺序排队等待进入。如果要求它们必须按序号递减的顺序从出口离开,则至少需要多少条平行铁轨用于调度?

输入格式:

输入第一行给出一个整数N (2 <= N <= 105),下一行给出从1到N的整数序号的一个重排列。数字间以空格分隔。

输出格式:

在一行中输出可以将输入的列车按序号递减的顺序调离所需要的最少的铁轨条数。

输入样例:
9
8 4 2 5 3 9 1 6 7
输出样例:
4

由Dilworth定理(最小反链划分 == 最长链)可知最少的下降序列个数就等于整个序列最长上升子序列的长度

这样求最长上升子序列长度即可(顺便复习了下nlogn算法

比赛时候用的树状数组+二分。。。

set也可过


代码如下

#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#define LL long long
#define Pr pair<int,int>
#define fread(ch) freopen(ch,"r",stdin)
#define fwrite(ch) freopen(ch,"w",stdout)

using namespace std;
const int INF = 0x3f3f3f3f;
const int msz = 10000;
const int mod = 1e9+7;
const double eps = 1e-8;

int dp[111111];

int main()
{
	//fread("");
	//fwrite("");

	int n,x;

	scanf("%d",&n);
	int len = 0;

	while(n--)
	{
		scanf("%d",&x);
		if(len == 0 || dp[len-1] <= x) dp[len++] = x;
		else
		{
			int l = 0,r = len-1;

			while(l <= r)
			{
				int mid = (l+r)/2;
				if(dp[mid] <= x) l = mid+1;
				else r = mid-1;
			}

			dp[l] = min(dp[l],x);
		}
	}

	printf("%d\n",len);

	return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值