码迷,mamicode.com
首页 > 编程语言 > 详细

51nod1294 修改数组

时间:2016-09-15 15:00:55      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:

看题解的。。。就是将必须要修改的数去掉后求最长的不递减子序列。

upper_bound+lower_bound要理解。有时候-1有时候不用是有原因的。 

#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
using namespace std;
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define clr(x,c) memset(x,c,sizeof(x))
int read(){
	int x=0;char c=getchar();
	while(!isdigit(c)) c=getchar();
	while(isdigit(c)) x=x*10+c-‘0‘,c=getchar();
	return x;
}
const int nmax=1e5+5;
const int inf=0x7f7f7f7f;
int dp[nmax],a[nmax];
int main(){
	int n=read(),u,v,d,cnt=0,ans=0;
	rep(i,1,n){
		u=read();
		if(u-i<0) ans++;else a[++cnt]=u-i;
	}
	fill(dp+1,dp+cnt+1,inf);
	rep(i,1,cnt){
		v=upper_bound(dp+1,dp+cnt+1,a[i])-dp;dp[v]=a[i];
	}
	printf("%d\n",ans+cnt-(lower_bound(dp+1,dp+cnt+1,inf)-dp-1));
	return 0;
}

  

题目来源: HackerRank
基准时间限制:1 秒 空间限制:131072 KB 分值: 160 难度:6级算法题
技术分享 收藏
技术分享 关注
给出一个整数数组A,你可以将任何一个数修改为任意一个正整数,最终使得整个数组是严格递增的且均为正整数。问最少需要修改几个数?
Input
第1行:一个数N表示序列的长度(1 <= N <= 100000)。
第2 - N + 1行:每行1个数,对应数组元素。(0 <= A[i] <= 10^9)
Output
输出最少需要修改几个数使得整个数组是严格递增的。
Input示例
5
1
2
2
3
4
Output示例
3

51nod1294 修改数组

标签:

原文地址:http://www.cnblogs.com/fighting-to-the-end/p/5874730.html

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