Look at the ... is to remove the number that must be modified to find the longest non-descending subsequence.
Upper_bound+lower_bound to understand. Sometimes-1 sometimes not for a reason.
#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;}
1294 modifying an array title Source: Hackerrank Base time limit: 1 seconds space limit: 131072 KB score: 160 Difficulty: 6 level algorithm topic collection focus on giving an integer array A, you can change any number to any positive integer, The result is that the entire array is strictly incremented and is a positive integer. 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
51nod1294 Modifying an array