Title Meaning: 51nod1294
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1294
Given an integer array A, you can change any number to any positive integer , and eventually the entire array is strictly incremented and all are 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
Topic Analysis:
For this kind of problem, the character in the original position of the order, our processing idea is, first of each number minus its location, and then use LIS, but need to pay attention to the problem is a positive integer, so we must be greater than or equal to 0 of the value of LIS, the number of modifications is n-longest lis.
AC Code (java/c++):
C++:
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include < vector> #include <iterator> #define MAX 100005using namespace Std;int a[max],b[max];int main () {int n; while (scanf ("%d", &n)!=eof) {int cnt=0; Vector<int> v; Vector<int>::iterator it; for (int i=0;i<n;i++) {scanf ("%d", &a[i]); a[i]=a[i]-(i+1);//each number minus the ordinal of its position if (a[i]>=0) b[cnt++]=a[i]; } if (cnt==0) {cout<<n<<endl; continue; } v.push_back (B[0]); for (int i=1;i<cnt;i++) {if (b[i]>=* (V.end ()-1)) {V.push_back (b[i]); } else{It=upper_bound (V.begin (), V.end (), b[i]); * (IT) =b[i]; }}//printf ("%d\n", V.size ()); printf ("%d\n", N-v.size ()); } return 0;}
Java code:
Import Java.lang.reflect.array;import java.util.arrays;import java.util.scanner;import javax.naming.BinaryRefAddr; Import Javax.xml.soap.saajresult;public class Xuigai_array {static int binarysearch (int s[],int x,int Low,int high) { while (low<=high) {int middle= (high+low)/2; if (X<s[middle]) high=middle-1; else if (X>s[middle]) low=middle+1; else return middle;} return high+1;} public static void Main (string[] args) {int a[]=new int[1000005];int b[]=new int[1000005];int c[]=new int[1000005];int n,c nt Scanner sc=new Scanner (system.in); while (Sc.hasnext ()) {n=sc.nextint (); cnt=0;for (int i=0;i<n;i++) {a[i]= Sc.nextint (); a[i]=a[i]-(i+1); if (a[i]>=0) b[cnt++]=a[i];} if (cnt==0) {System.out.println (n); continue;} int len=1,pos;c[len-1]=b[0];for (int i=1;i<cnt;i++) {//system.out.println (B[i]), if (B[i]>=c[len-1]) {c[len++]= B[i];} Else{pos=binarysearch (c, B[i], 0, len-1);//system.out.println (POS); if (pos>=0&&pos<=len-1) for (int j= pos;j<=len;j++) {if (C[j]>b[i]) {pos=j; break;}} C[pos]=b[i];}} System.out.println (N-len);}}}
CSDN China Telecom Wing Payment 2014 programming Contest rematch Revision series (LIS)