ASDFZ 3648 - Train schedule

Description

There are N trains, labeled 1,2,3, ..., N. They follow a certain sequence pit stop, a total of K tracks, track compliance within the platform FIFO principles. After the train enters the station may track the waiting time in any station, and all trains can not be retracted. Order now resorted station becomes N, N-1, N- 2, ..., 1, K is the number of minimum interrogation.
For example, a sequential order of the figure above the stop 1,3,2,4,8,6,9,5,7, then the station becomes 9,8,7,6,5,4,3,2, 1.

Input

Enter the total of the two lines.
The first line contains a positive integer N , it represents N trains.
The second line contains N positive integers and . 1 to a permutation of N, it represents a stop order.

Output

The output common line, contains an integer representing the number of tracks within the station K minimum.

Sample Input

#1
3
1 2 3

#2
9
1 3 2 4 8 6 9 5 7

Sample Output

#1
3

#2
5

Hint

对于 30% 的数据,N≤10;
对于 70% 的数据,N≤2000;
对于 100% 的数据,N≤10^5。

Source

2019NOIP前训练

思路

求最长上升子序列(LIS上升子序列指的是对于任意的i<j都满足ai<aj的子序列)

代码(70分—超时)

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int N=100010;

int n,ans=-1;
int a[N],dp[N];

int main () {
	scanf("%d",&n);
	for(int i=1; i<=n; i++)
		scanf("%d",&a[i]);
	for(int i=1; i<=n; i++) {
		dp[i]=1;
		for(int j=1; j<i; j++)
			if(a[j]<a[i])
				dp[i]=max(dp[i],dp[j]+1);
		ans=max(dp[i],ans);
	}
	printf("%d\n",ans);
	return 0;
}

 AC代码—优化

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int N=100010;

int n,ans,a[N],v[N],s[N];

int main() {
	scanf("%d",&n);
	for(int i=1; i<=n; i++)
		scanf("%d",&a[i]);
	for(int i=1; i<=n; i++) {
		if(v[i]==1)
			continue;
		bool flag=false;
		for(int j=1; j<=ans; j++)
			if(a[i]<s[j]) {
				s[j]=a[i];
				v[i]=1;
				flag=true;
				break;
			}
		if(!flag)
			s[++ans]=a[i];
	}
	printf("%d\n",ans);
	return 0;
}

 

Guess you like

Origin www.cnblogs.com/mysh/p/11789477.html