Two Merged Sequences
Strictly increasing sequence is a sequence of integers [x1<x2<⋯<xk][x1<x2<⋯<xk]. And strictly decreasing sequence is a sequence of integers [y1>y2>⋯>yl][y1>y2>⋯>yl]. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
Elements of increasing sequence were inserted between elements of the decreasing one (and, possibly, before its first element and after its last element) without changing the order. For example, sequences [1,3,4][1,3,4] and [10,4,2][10,4,2] can produce the following resulting sequences: [10,1,3,4,2,4][10,1,3,4,2,4], [1,3,4,10,4,2][1,3,4,10,4,2]. The following sequence cannot be the result of these insertions: [1,10,4,4,3,2][1,10,4,4,3,2]because the order of elements in the increasing sequence was changed.
Let the obtained sequence be aa. This sequence aa is given in the input. Your task is to find any two suitable initial sequences. One of them should be strictly increasing, and another one — strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
If there is a contradiction in the input and it is impossible to split the given sequence aa into one increasing sequence and one decreasing sequence, print "NO".
The first line of the input contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of elements in aa.
The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤2⋅1050≤ai≤2⋅105), where aiai is the ii-th element of aa.
If there is a contradiction in the input and it is impossible to split the given sequence aa into one increasing sequence and one decreasing sequence, print "NO" in the first line.
Otherwise print "YES" in the first line. In the second line, print a sequence of nn integers res1,res2,…,resnres1,res2,…,resn, where resiresi should be either 00 or 11 for each ii from 11 to nn. The ii-th element of this sequence should be 00 if the ii-th element of aa belongs to the increasing sequence, and 11 otherwise. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
大意就是给你一个数组,问能否让每个元素进入递增或递减序列,最后形成一个严格递减和递增的序列。
网上也有dp解法
但是感觉贪心更容易理解
首先对于一个元素,如果这个元素在两个序列都放不进去,那么肯定输出no;
其次对于一个元素只能进入其中一个序列,那么就把他放进去
最后是对于两个序列都能放进去的元素,我们怎么放,我们应该看下一个元素(没有的话放到哪一个都可以)
如果下一个元素比他大,那么就把他放到递增序列里面,如果比他小就放到递减序列里面,(如果相等都可以)
为什么会这样呢?假设下一个元素比它大,我们可以想如果下一个元素进入递增数组,那么这个元素相当于没用,这样对于后面的元素进入是最优的;
如果这个元素是递减的,那这个元素肯定在递增里面(因为这个元素比下一个小,不可能和下一个元素同时放到递减里面)
因此这样贪心一定是对的
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5+100; int a[maxn],ans[maxn]; int flag; int main() { int n; cin>>n; for(int i=0;i<n;i++) { scanf("%d",&a[i]); } int up=-1;//递增数组的上限 int down=9999999;//递减数组的下限 for(int i=0;i<n;i++) { if(a[i]>up&&a[i]<down) { if(i==n-1)//最后一个元素 { ans[i]=0; } else { if(a[i]>a[i+1]) { ans[i]=1; down=a[i]; } else { up=a[i]; ans[i]=0; } } } else if(a[i]>up&&a[i]>=down) { ans[i]=0; up=a[i]; } else if(a[i]<down&&a[i]<=up) { ans[i]=1; down=a[i]; } else { flag=1; break; } } if(flag==1) { printf("NO\n"); return 0; } else { printf("YES\n"); for(int i=0;i<n-1;i++) { printf("%d ",ans[i]); } printf("%d\n",ans[n-1]); } return 0; }