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

【HDU】5256 序列变换(最长上升子序列变形)

时间:2015-06-03 23:33:13      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:

如果a[i]和a[j]想不变,需要满足的条件就是

a[j] - a[i] > j - i

也就是a[i] - i < a[j] - j

比如1 4 2 就不满足,所以1和2之间一定有一个需要改变

所以我们对所有a[i] - i求其最长上升子序列就可以了

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 100005;
const int  INF = 5000000;
int n;
int arr[maxn],g[maxn];
int main(){
    int T,Case = 1;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        for(int i = 1; i <= n; i ++) g[i] = INF;
        for(int i = 1; i <= n; i++){
            scanf("%d",&arr[i]);
            arr[i] -= i;
        }
        int ans = 0;
        for(int i = 1; i <= n; i++){
            int pos = upper_bound(g + 1,g + n + 1,arr[i]) - g;
            ans = max(ans,pos);
            g[pos] = arr[i];
        }
        printf("Case #%d:\n",Case++);
        printf("%d\n",n - ans);
    }
    return 0;
}


【HDU】5256 序列变换(最长上升子序列变形)

标签:

原文地址:http://blog.csdn.net/u013451221/article/details/46352457

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