1. 程式人生 > >1294 修改數組

1294 修改數組

urn namespace img input long gif main getchar() getc

1294 修改數組 基準時間限制:1 秒 空間限制:131072 KB 給出一個整數數組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
位置的考慮也很重要。
技術分享
 1 #include<iostream>
 2
#include<algorithm> 3 #include<cstdio> 4 #include<cstring> 5 #include<cmath> 6 #include<cstdlib> 7 #include<vector> 8 using namespace std; 9 typedef long long ll; 10 typedef long double ld; 11 typedef pair<int,int> pr; 12 const double pi=acos(-1); 13 #define
rep(i,a,n) for(int i=a;i<=n;i++) 14 #define per(i,n,a) for(int i=n;i>=a;i--) 15 #define Rep(i,u) for(int i=head[u];i;i=Next[i]) 16 #define clr(a) memset(a,0,sizeof(a)) 17 #define pb push_back 18 #define mp make_pair 19 #define fi first 20 #define sc second 21 #define pq priority_queue 22 #define
pqb priority_queue <int, vector<int>, less<int> > 23 #define pqs priority_queue <int, vector<int>, greater<int> > 24 #define vec vector 25 ld eps=1e-9; 26 ll pp=1000000007; 27 ll mo(ll a,ll pp){if(a>=0 && a<pp)return a;a%=pp;if(a<0)a+=pp;return a;} 28 ll powmod(ll a,ll b,ll pp){ll ans=1;for(;b;b>>=1,a=mo(a*a,pp))if(b&1)ans=mo(ans*a,pp);return ans;} 29 void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); } 30 //void add(int x,int y,int z){ v[++e]=y; next[e]=head[x]; head[x]=e; cost[e]=z; } 31 int dx[5]={0,-1,1,0,0},dy[5]={0,0,0,-1,1}; 32 ll read(){ ll ans=0; char last= ,ch=getchar(); 33 while(ch<0 || ch>9)last=ch,ch=getchar(); 34 while(ch>=0 && ch<=9)ans=ans*10+ch-0,ch=getchar(); 35 if(last==-)ans=-ans; return ans; 36 } 37 const int N=100005; 38 int a[N],b[N]; 39 int main(){ 40 int n=read(),nu=0; 41 for (int i=1;i<=n;i++) a[i]=read(),a[i]-=i; 42 for (int i=1;i<=n;i++) 43 if (a[i]>=0){ 44 int p=upper_bound(b+1,b+nu+1,a[i])-b; 45 if (p>nu) b[++nu]=a[i]; 46 else b[p]=a[i]; 47 } 48 printf("%d",n-nu); 49 return 0; 50 }
View Code

1294 修改數組