码迷,mamicode.com
首页 > 其他好文 > 详细

7-2 列车调度 (25 分)

时间:2018-10-22 16:45:57      阅读:332      评论:0      收藏:0      [点我收藏+]

标签:i++   就是   png   bit   typedef   printf   std   .com   输出   

题目:

技术分享图片

 

样例输入:

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

 

7-2 列车调度 (25 分)

标签:i++   就是   png   bit   typedef   printf   std   .com   输出   

原文地址:https://www.cnblogs.com/sykline/p/9830505.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!