C. Element Extermination (greedy, simulation, conclusion)

C. Element Extermination

Question: Given an array, two adjacent numbers satisfy A i <A i − 1 A_i <A_{i-1}Ai<Ai1, You can delete one of the numbers. Ask if there is a deletion scheme that leaves only one element in the array.

Ideas:

Stack simulation

Use the stack to simulate the sequence of numbers in the array (successively on the stack) and greedy decision-making (whether to stack or not)

Suppose you want to push the element a on the stack and the top element b on the
stack 1. If the stack is empty, push it directly.
2. If a <ba <ba<b , directly into the stack.
3. Ifa> ba> ba>b , the condition of deletion is satisfied, and the problem of greedy decision-making is reached.
(1). If there is only one element in the stack at this time, we prefer the smallerbbb . :
Inter-partition discussion, if there is[a, n] [a, n]on the right side ofa[a,n ] element, that leavesa, ba, ba and b are both OK, if inaaTo the right of a is[b, a] [b, a][b,a ] element, onlybbb , if inaaa right with a[1, b] [1,[1,b ] , then leavea, ba, ba and b will not work,bbb has a higher priority.
(2). If there are multiple elements in the stack, we have to delete as many elements as possible.
Then we can also discuss between partitions, where the elements in the stack are located, and leavea, ba, ba , b , which may delete more elements. The result of the discussion is to leaveaaa , delete the elements in the stack circularly, until there is only one element left in the stack or the deletion condition is not met.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e5+10;
const int inf = 0x7ffffff;
int f[][2] = {
    
    1, 0, 0, 1, -1, 0, 0, -1}, n, m;
int s[N];
int read() {
    
    
    int x = 0; int f = 1; char s = getchar();
    while(s < '0' || s > '9') {
    
    if(s == '-') f = -1; s = getchar();}
    while(s >= '0' && s <= '9') {
    
    x = (x << 3) + (x << 1) + s - 48; s = getchar();}
    return x * f;
}
int main() {
    
    
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    int t;
    t = read();
    while(t--) {
    
    
        stack<int> T;
        int n, f, mx = 0, mi = 0;
        n = read();
        for(int i=1; i<=n; i++) {
    
    
            s[i] = read();
            if(s[i] == 1) f = i;
        }
        for(int i=1; i<=n; i++) {
    
    //一个一个元素进行决策。
            if(T.empty()) T.push(s[i]);//空
            else if(s[i] < T.top()) T.push(s[i]);//不满足删除条件
            else {
    
    //满足删除条件
                if(T.size() == 1) continue;//只有一个元素
                else T.push(s[i]);
                while(1) {
    
    //贪心决策
                    int a = T.top(); T.pop();
                    int b = T.top(); T.pop();
                    if(a > b) {
    
    //留a还是留b
                        if(T.size() > 0) T.push(a);
                        else T.push(b);
                    }
                    else {
    
    //不满足删除条件了
                        T.push(b); T.push(a);
                        break;
                    }
                    if(T.size() == 1) break;//只剩下一个元素
                }
            }
        }
        if(T.size() == 1) puts("YES");
        else puts("NO");
    }
    return 0;
}

According to the above simulation, we can think about whether the first element has not been deleted. When making a decision on the last element, if the last element will meet the first element and meet the deletion conditions ( A 1 <A n A_1 <A_nA1<An), the first element will be left. If there is a situation that does not meet the deletion conditions in the middle, we will sort them in this order.
The first element A 1, the last element A i that does not meet the deletion condition, the last element A n The first element A_1, the last element A_i that does not meet the deletion condition, the last element A_nFirst a th membered element A1, For most after a th element element is not full enough to delete the other bar member of the metadata element Ai, Most after a th membered element An
Can deduce A i> A n A_i> A_nAi>An, And because of the A i A_iAiThis element was left when making decisions, indicating that A 1> A i A_1> A_iA1>Ai. There is already a shadow of the conclusion here, and it proves that I can't figure it out.

in conclusion

Just compare the size of the first and last digits.
S 1> S n, N o S 1 <S n, YES S_1> S_n,~~~No~~~~~~~~~~S_1 <S_n,~~~YESS1>Sn,   N o S           1<Sn,   YES

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e5+10;
const int inf = 0x7ffffff;
int f[][2] = {
    
    1, 0, 0, 1, -1, 0, 0, -1}, n, m;
int s[N];
int read() {
    
    
    int x = 0; int f = 1; char s = getchar();
    while(s < '0' || s > '9') {
    
    if(s == '-') f = -1; s = getchar();}
    while(s >= '0' && s <= '9') {
    
    x = (x << 3) + (x << 1) + s - 48; s = getchar();}
    return x * f;
}
int main() {
    
    
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    int t;
    t = read();
    while(t--) {
    
    
        int n, f, mx = 0, mi = 0;
        n = read();
        for(int i=1; i<=n; i++) {
    
    
            s[i] = read();
        }
        if(s[n] < s[1]) printf("NO\n");
        else printf("YES\n");
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45363113/article/details/107250935