7-2 列車調度 (25 分)


題目:

 

樣例輸入:

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;
const 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;
}
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM