7-2 列車調度 (25 分)
阿新 • • 發佈:2018-10-22
alt 問題 opened ons max esp spa lose code
題目:
樣例輸入:
9
8 4 2 5 3 9 1 6 7
樣例輸出:
4
思路:
要想得到最少的調度序列,那就要找出最少的下降序列的個數。拿上邊的例子來說:有如下四個下降序列
8 4 2 1
5 3
9 6
7
所以只需要四個調度隊列就可以了。
又根據定理:最小的下降序列的個數等於最長上升子序列的長度。(這個定理證明沒看懂,直接懵逼,菜是原罪啊!!)剩下的就是一個裸的最長上升子序列問題了。
代碼:
#include <bits/stdc++.h> #define inf 0x3f3f3f3f using namespace std; typedef long long ll;View Codeconst int maxn = 1e5+10; int a[maxn],dp[maxn]; int main() { int n; scanf("%d",&n); for(int i = 0; i<n; i++) { scanf("%d",&a[i]); dp[i] = inf; } int mmax = -1; for(int i = 0; i<n; i++) { int k = lower_bound(dp,dp+n,a[i])-dp; dp[k]= a[i]; mmax = max(mmax, k+1); } printf("%d\n",mmax); return 0; }
7-2 列車調度 (25 分)