#575. 列车调度

【题目描述】:

列车由东面进站只有一个车道,进站后站内共分有K个平行的车道,出站前汇聚为一个车道,由西面出站。

有N辆列车,标记为1,2,3,…,N。它们按照一定的次序进站,轨道遵从先进先出的原则。列车进入站台内的轨道后可以等待任意时间后出站,且所有列车不可后退。现在要使出站的顺序变为N,N-1,N-2,…,1,询问K的最小值是多少。

【输入描述】:

输入共2行。

第 1 行包含1个正整数N,表示N辆列车。

第 2 行包含N个正整数,为1至N的一个排列,表示进站次序。

【输出描述】:

输出共1行,包含1个整数,表示站台内轨道数K的最小值。

【样例输入1】:

3
1 2 3

【样例输出1】:

3

【样例输入2】:

9
1 3 2 4 8 6 9 5 7

【样例输出2】:

5

【时间限制、数据范围及描述】:

时间:1s 空间:128M

对于30%的数据,N≤10;

对于70%的数据,N≤2000;

对于100%的数据,N≤100000。

扫描二维码关注公众号,回复: 7252523 查看本文章
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
int n,now,ans=1;
bool ok;
int a[100000];
int read(){
	int a=0,b=1;
	char ch=getchar();
	while((ch<48||ch>57)&&ch!='-'){
		ch=getchar();
	}
	if(ch=='-'){
		b=-1;
		ch=getchar();
	}
	while(ch<48||ch>57){
		ch=getchar();
	}
	while(ch>47&&ch<58){
		a=a*10+ch-48;
		ch=getchar();
	}
	return a*b;
}
int main(){
	n=read();
	now=read();
	a[1]=now;
	for(int i=2;i<=n;i++){
		now=read();
		ok=1;
		for(int i=1;i<=ans;i++){
			if(a[i]>now){
				a[i]=now;
				ok=0;
				break;
			}
		}
		if(ok){
			ans++;
			a[ans]=now;
		}
	}
	printf("%d",ans);
	return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/xiongchongwen/p/11503809.html