zoukankan      html  css  js  c++  java
  • 51nod1294 修改数组

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

    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
    ",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
  • 相关阅读:
    geoserver 文件系统
    geoserver 源码介绍
    geoserver 开发2
    geoserver 开发1
    geoserver笔记
    linux 下安装gult
    LINUX 笔记5
    SQLSTATE[HY000] [2002] 乱码
    微信开发
    javascript记忆
  • 原文地址:https://www.cnblogs.com/fighting-to-the-end/p/5874730.html
Copyright © 2011-2022 走看看