deque 8pts 求助
查看原帖
deque 8pts 求助
206814
封禁用户楼主2022/9/16 18:53

照着题解第一篇打的

#include <bits/stdc++.h>
using namespace std;
int arr[500005];
deque<int> c, d;
char output[500005];
int main(){
    int T;
    scanf("%d", &T);
    while(T--){
        int n;
        scanf("%d", &n);
        for(int i = 1; i <= 2 * n; i++){
            scanf("%d", &arr[i]);
        }
        for(char first = 'L'; ; first = 'R'){
            int x, now;
            output[1] = first;
            output[2 * n] = first;
            if(first == 'L'){
                x = find(arr + 2, arr + 2 * n + 1, arr[1]) - arr, now = 2;
            }else{
                x = find(arr + 1, arr + 2 * n, arr[2 * n]) - arr, now = 2;
            }
            for(int i = 2; i <= x - 1; i++){
                c.push_back(arr[i]);
            }
            for(int i = n * 2; i >= x + 1; i--){
                d.push_back(arr[i]);
            }
            bool failed = false;
            while(!c.empty() || !d.empty()){
                if(c.size() == 1 && d.size() == 1 && (*c.begin()) == (*d.begin())){
                    c.clear();
                    d.clear();
                    output[now] = 'L';
                    output[2 * n - now + 1] = 'R';
                    break;
                }
                if(!c.empty() && c.front() == c.back()){
                    c.pop_front();
                    c.pop_back();
                    output[now] = output[2 * n - now + 1] = 'L';
                }else if(!c.empty() && !d.empty() && c.front() == d.back()){    
                    c.pop_front();
                    d.pop_back();
                    output[now] = 'L';
                    output[2 * n - now + 1] = 'R';
                }else if(!d.empty() && d.front() == d.back()){
                    d.pop_front();
                    d.pop_back();
                    output[now] = output[2 * n - now + 1] = 'R';
                }else if(!c.empty() && !d.empty() && d.front() == c.back()){
                    d.pop_front();
                    c.pop_back();
                    output[now] = 'R';
                    output[2 * n - now + 1] = 'L';
                }else{
                    failed = true;
                    break;
                }
                now++;
            }
            if(!failed) break;
            if(failed && first == 'R'){
                output[1] = '-';
                output[2] = '1';
                output[3] = '\0';
                break;
            }
        }
        for(int i = 1; i <= 2 * n; i++){
            if(output[i] == '\0') break;
            putchar(output[i]);
        }
        putchar('\n');
    }
    return 0;
}
2022/9/16 18:53
加载中...