Sequence Transformation (LIS variant)

Source: Internet
Author: User

Sequence transformations

Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others) total submission (s): Accepted Submission (s): 448

Problem description We have a series of a1,a2 ... An, you now want to modify the least number of elements, so that the sequence is strictly incremented. Each element must be an integer, either before or after the modification. Please output the minimum number of elements you need to modify.

Input first line Enter aT(1≤t≤) That indicates how many sets of data
Each group of data:
The first line enters an(1≤n≤5) , which indicates the length of the sequence
Enter n number in the second linea1,a2,.. . ,An 。
Each element in the sequence is a positive integer and does not exceed Ten6.

Output for each set of data, first a row
Case #i:
Then output the minimum number of elements that need to be modified.

Sample Input221 1032 5 4

Sample outputcase #1:0Case #2:1

SOURCE2015 year Baidu Star Program Design Competition-Preliminary (2)

Exercises

Changing series makes the monotone increment sub-sequence; then it just needs to be dp[i]-dp[j]>=i-j, and the method of monotone increment subsequence is added;

Dp[i]-dp[j]>=i-j is the dp[i]-i>=dp[j]-j;

Code:

#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<vector>using namespacestd;Const intMAXN = 1e5 + -;intNUM[MAXN];intDp[maxn];vector<int>Vec;/*int main () {int T, N, Kase = 0;    scanf ("%d", &t);        while (t--) {vec.clear ();        scanf ("%d", &n);            for (int i = 0; i < N; i++) {scanf ("%d", num + i);            Num[i]-= i;            if (Upper_bound (Vec.begin (), Vec.end (), num[i]) = = Vec.end ()) {Vec.push_back (num[i]);                } else{int p = upper_bound (Vec.begin (), Vec.end (), num[i])-Vec.begin ();            VEC[P] = Num[i];    }} printf ("Case #%d:\n%d\n", ++kase, N-vec.size ()); } return 0;}*/intMain () {intT, N, Kase =0; scanf ("%d", &T);  while(t--) {scanf ("%d", &N); Memset (DP,0,sizeof(DP)); intAns =0;  for(inti =0; i < N; i++) {scanf ("%d", Num +i);  for(intj =0; J < I; J + +){                if(Num[i]-num[j] >= i-j) {Dp[i]= Dp[j] +1; Ans=Max (ans, dp[i]); }}} printf ("Case #%d:\n%d\n", ++kase, N-ans-1); }    return 0;}

Sequence Transformation (LIS variant)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.