Two Merged Sequences greedy

Topic link: http: //codeforces.com/problemset/problem/1144/G

This question and another question is very similar, but simpler than many of the other questions that are interested can do a do together. There are topics and ideas: https: //www.cnblogs.com/xlbfxx/p/11255419.html

Meaning of the questions: a sequence order is divided into two sequences, a strictly increasing, a strictly decreasing. Empty sequence a sequence or element can be used as increment or decrement.

 Ideas:

Providing two sequences, an increment, a decrement, traversing the input sequence, and comparing the count at the end of the sequence

1. If this number can only be placed in increasing sequence, it is placed in increasing sequence.

2. If this number can only be placed in descending sequence, it is placed in descending sequence.

3. If the number can be placed in two sequences, and this number will compare the number of behind it.

(1) If the number is greater than the latter, it is placed in a descending sequence.

(2) If the number is smaller than the latter, it is placed in increasing sequence.

(3) if equal, respectively, into the two sequences.

 

Why is it? When this number can be placed in two sequences, the number of which is smaller than the latter, if the incremented sequence number is later, then there are this number does not matter, if this number into a descending sequence, it is possible later and is greater than its want to put a descending sequence number, but the limited number, such as 5384, for the number 3, which can be placed in two sequences, it is less than 8, if it is placed in a descending sequence, the latter limits the 4.

I do this problem, we want the wrong case, it is the time when the sequence number 1, I think it can not be divided into two sequences, in fact, can be divided into two sequences, a sequence of numbers, a sequence empty, can be incremented or decremented.

AC Code:

#include <the iostream>
 the using  namespace STD;
 const  int MAX + 2E5 = 10 ;
 int A [MAX], B [MAX];
 int main () 
{ 
    int n-; 
    CIN >> n-;
     for ( int I = 0 ; I <n- ; I ++) CIN >> a [I];
     int PRE1 = - . 1 , pre2 = MAX;     // PRE1 is the last value is incremented sequence, pre2 is a decreasing sequence of the last value 
    for ( int I = 0 ; I <n-; I ++ ) 
    { 
        IF (A [I]> PRE1 && A [I] < PRE2) 
        { 
            IF(i==n-1)
            {
                b[i]=0;
            }else{
                if(a[i]<a[i+1]){
                    b[i]=0;
                    pre1=a[i];
                }else if(a[i]>a[i+1]){
                    b[i]=1;
                    pre2=a[i];
                }else{
                    b[i]=0;
                    b[i+1]=1;
                    pre1=a[i];
                    pre2=a[i];
                    i++;
                }
            }
        }else if(a[i]>pre1){
            pre1=a[i];
            b[i]=0;
        }else if(a[i]<pre2){
            pre2=a[i];
            b[i]=1;
        }else{
            cout<<"NO"<<endl;
            return 0;
        }
    }
    cout<<"YES"<<endl;
    for(int i=0;i<n;i++)cout<<b[i]<<" ";
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/xlbfxx/p/11270050.html