Source: Hackerrank Base time limit: 1 seconds space limit: 131072 KB score: 160 Difficulty: 6-level algorithm problem collection focus on giving an integer array A, you can change any number to any positive integer, eventually making the entire array strictly incremented and all positive integers. What are the minimum number of changes to ask? Input
Line 1th: A number N indicates the length of the sequence (1 <= n <= 100000). 2-n + 1 lines: 1 numbers per line, corresponding to array elements. (0 <= A[i] <= 10^9)
Output
The minimum number of outputs required to be modified makes the entire array strictly incremented.
Input example
512234
Output example
3
1 //2018-07-16 17:15:292#include <iostream>3#include <cstdio>4#include <algorithm>5 using namespacestd;6 7 Const intN =1000005;8 9 intN;Ten intA[n], d[n], b[n], ans; One A intMain () { -scanf"%d", &n); - intHead =0; the for(intI=1; i<=n; i++){ -scanf"%d", &b[i]); - if(b[i]==0&& i==1) A[++head] = B[i];//a lazy place. - if(B[i]-i >=0) A[++head] = b[i]-i; + Elseans++; - } +d[1] = a[1]; A intLen =1; at for(intI=2; i<=head; i++){ - if(A[i] >= D[len]) D[++len] =A[i]; - Else{ - intj = Upper_bound (d+1, d+len+1, A[i])-D; -D[J] =A[i]; - } in } -printf"%d\n", head-len+ans); to + return 0; -}
51Nod 1294 Modifying an array