Given a sequence (length >=2), ask if an element can be removed to become a monotone non-descending sequence or a monotone non-increment sequence.
For any sequence, first assume that it can be transformed into a monotone non-descending sequence, if it is set to output Yes, not set up and then assume that it can be transformed into a monotone non-increment sequence, if the set is output Yes, does not set output No.
As the flat does not affect the overall monotonicity, in order to be intuitive, I am in the following "Do not Drop" is called "increment/Ascending", the "no increase" is called "decrement/Descending."
Increment and decrement are symmetric, here first consider increment, decrement change a symbol and the most value is good.
We call the point of the "bad point" that is removed for the maintenance of monotonicity. By the requirements of the topic, "can be modified" can be equivalent to "there is only one bad point."
For the "Bad point" judgment, we can first find out whether there is only a set of "reverse".
For the "almosted sorted" increment sequence, there is only one set of four cases in reverse order (here the reverse is not considered at the boundary).
Now consider the case of reverse order at the boundary. Because a[] array element subscript is 1~n, and this problem 1<=ai<=100000, then for the increment sequence, you can set the a[0] to 1, the a[n+1] is set to 100000 as the Sentinel node, will not destroy the overall monotonicity; descending sequence to do symmetric processing. Such boundaries can also be handled in the same way as in the middle.
Since three cases satisfy one, and the second can be regarded as the intersection of the third and fourth, it is only possible to go through the a[] array one at a time in the third and fourth cases, and satisfy one can output yes.
For the processing of bad points, we adopt the "when it does not exist" strategy, so the first encounter the bad point, ignore it, again encounter the bad point, then this situation is not established.
As for how to "reverse" the "bad Point", and to achieve a number of cases of judgment, we traverse I:0~n, for the first pair of reverse order a[i]>a[i+1], we can:
First Take "left" (third), that is, a[i] as the bad point, Judge A[i-1] and a[i+1] whether ascending (if not ascending is equivalent to constitute the second pair of reverse order, the second bad point);
If the left is not established, then take the "right" (fourth), that is, a[i+1] when the bad point, the same judgment a[i] and a[i+2] whether ascending.
The code is as follows:
1#include <cstdio>2 using namespacestd;3 4 Const intmax_n=100005;5 Const intMin_a=1;6 Const intMax_a=100000;7 intT;8 intN;9 int inch[Max_n],de[max_n];Ten intFlag; One intfix_cnt; A - intMain () - { theFreopen ("5532.txt","R", stdin); -scanf"%d",&T); - while(t--) - { +scanf"%d",&n); - for(intI=1; i<=n;i++) + { Ascanf"%d",&inch[i]); atde[i]=inch[i]; - } - - //the situation in ascending order - inch[0]=min_a; - inch[n+1]=max_a; inflag=1;//Assuming that you remove up to one element, the overall ascending -Fix_cnt=0; to for(intI=1; i<=n-1; i++) + { - if(inch[i]<=inch[i+1])Continue; thefix_cnt++;//the case of left return * if(fix_cnt<=1&&inch[I1]<=inch[i+1])Continue; $flag=0;Panax Notoginseng Break; - } the if(flag) + { Aprintf"yes\n"); the Continue; + } -flag=1; $Fix_cnt=0; $ for(intI=1; i<=n-1; i++) - { - if(inch[i]<=inch[i+1])Continue; thefix_cnt++;//right-of-return situation - if(fix_cnt<=1&&inch[i]<=inch[i+2])Continue;Wuyiflag=0; the Break; - } Wu if(flag) - { Aboutprintf"yes\n"); $ Continue; - } - //Descending Condition -de[0]=max_a; Ade[n+1]=min_a; +flag=1;//Assuming that you remove up to one element, the overall descending theFix_cnt=0; - for(intI=1; i<=n-1; i++) $ { the if(de[i]>=de[i+1])Continue; thefix_cnt++;//the case of left return the if(fix_cnt<=1&&de[i-1]>=de[i+1])Continue; theflag=0; - Break; in } the if(flag) the { Aboutprintf"yes\n"); the Continue; the } theflag=1; +Fix_cnt=0; - for(intI=1; i<=n-1; i++) the {Bayi if(de[i]>=de[i+1])Continue; thefix_cnt++;//right-of-return situation the if(fix_cnt<=1&&de[i]>=de[i+2])Continue; -flag=0; - Break; the } the if(flag) the { theprintf"yes\n"); - Continue; the } theprintf"no\n"); the }94 return 0; the}
"HDU 5532 almost Sorted Array" water problem, simulation