HDU5532 almost Sorted array (longest ascending subsequence or blind to make a difference)

Source: Internet
Author: User

Topic Link: Point Me

Test instructions: Given a sequence, ask if you can delete a number to make it a non-descending or non-incrementing sequence.

For example, the deleted sequence is 1 3 3 5 or 5 3 3 1 or 1 3 5 or 5 3 1 can be. As long as the deletion of a certain number, constitute non-decrement or non-increment, the output is yes, if not the output no

Positive solution (Lis to find the longest ascending subsequence):

I'm going to do it again. Note To use Upper_bound:

Code:

#include <bits/stdc++.h>using namespacestd;intMaxLen (intA[],intN) {    intb[100010]; memset (b,0,sizeof(b)); intLen =0;  for(inti =0; I < n; i + +)      {          if(len = =0|| A[i] >= B[len-1]) {B[len]=A[i]; Len++; }          Else          {              intp = upper_bound (B,b + len,a[i])-b; B[P]=A[i]; }      }    returnLen;}intMain () {intT,n;  for(SCANF ("%d", &t); t--;) {scanf ("%d",&N); inta[100010],c[100010];  for(inti =0; I < n; i++) {scanf ("%d",&A[i]); C[n-i-1] =A[i]; }        intLen =MaxLen (a,n); intLen1 =MaxLen (c,n); if(Len >= N-1|| LEN1 >= N-1) puts ("YES"); ElsePuts"NO"); }}

If you want to be blind ...

Because delete a well, first prove to delete a can be non-diminishing (non-incrementing the sequence to the reverse of the line)

First, make a difference to two numbers before and after a sequence

For example, sequence

3 1 4 1 5 after the difference (ie 1-3,4-1,1-4,5-1) is -2,3,-3,4. If you find 2 negative numbers, you can't.

If the sequence is 3 1 1 5. After doing the difference is -2,0,4. Found that there is a negative number that is in the head and can be deleted

If the sequence is 5 6 3, 7 7, the difference is 1,-3,4,0. Found a negative number, and can be added to the left and right side of the sum to become positive, then the 3 can be deleted.

If the sequence is 1 2 3 4, doing bad after is 1,1,1,1 no negative numbers, itself can be non-diminishing.

Can see it:

Do the bad sequence: if there are 2 or more negative numbers, then it is certainly not a non-diminishing.

If there is a negative number, it is in the head or tail, or in the middle and can be combined with the left and right of any of the numbers is positive, can be non-diminishing

If there are no negative numbers, it is non-descending

The time complexity is O (n), and an extra space of O (n) is required to make an array of poor

If you're not incrementing, just rewind the array and do it again.

Code (very messy):

#include <cstdio>#include<cstring>#include<cmath>#include<cstdlib>#include<ctime>#include<iostream>#include<algorithm>#include<sstream>#include<string>#include<vector>#include<queue>#include<stack>#include<map>#include<Set>#include<utility>#include<bitset>using namespacestd; #defineLL Long Long#definePB Push_back#defineMk Make_pair#definePill Pair<int, int>#defineMST (A, B) memset (A, B, sizeof a)#defineREP (i, x, n) for (int i = x; i <= n; ++i)intMain () {intT,n;  for(SCANF ("%d", &t); t--;) {scanf ("%d",&N); inta[100010],b[100010],c[100010];  for(inti =0; I < n; i++) {scanf ("%d",&A[i]); C[n-i-1] =A[i]; }        intF1 =0, ard =-1; intS1 =0, S2 =0;  for(inti =1; I < n; i++) {B[i]= A[i]-a[i-1]; if(B[i] <0) {F1++; ARD=i; }            if(F1 = =2){                 Break; }        }        if(F1 = =0) {S1=1; }        if(F1 = =1){            if(ard = =1|| ard = N1) {S1=1; }            Else if(B[ard] + b[ard-1] >=0|| B[ard] + b[ard+1] >=0) {S1=1; }} F1=0; ARD= -1; //for (int i = 0; i < n; i++) printf ("%d", C[i]);         for(inti =1; I < n; i++) {B[i]= C[i]-c[i-1]; if(B[i] <0) {F1++; ARD=i; }            if(F1 = =2){                 Break; }        }        if(F1 = =0) {s2=1; }        if(F1 = =1){            if(ard = =1|| ard = N1) {s2=1; }            Else if(B[ard] + b[ard-1] >=0|| B[ard] + b[ard+1] >=0) {s2=1; }} S1|| S2?puts ("YES"):p UTS ("NO"//s1,s2, respectively, represent the conditions in non-descending and non-increment can be satisfied}}

HDU5532 almost Sorted array (longest ascending subsequence or blind to make a difference)

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.