Submission #1174476

#TimeUsernameProblemLanguageResultExecution timeMemory
1174476PacybwoahDEL13 (info1cup18_del13)C++20
30 / 100
66 ms1860 KiB
#include<iostream>
#include<vector>
#include<algorithm>
#include<utility>
#include<cmath>
using namespace std;
typedef long long ll;
const ll inf = 1e18;
int dp[(1 << 18) + 3], lst[(1 << 18) + 3], lstans[(1 << 18) + 3];
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    bool flag = 0;
    while(t--){
        int n, q;
        cin >> n >> q;
        if(!flag){
            flag = 1;
            dp[(1 << n) - 1] = 1;
            for(int i = (1 << n) - 2; i >= 0; i--){
                vector<int> pre(n + 1);
                for(int j = 0; j < n; j++){
                    pre[j + 1] = pre[j];
                    if(i & (1 << j)){
                        pre[j + 1]++;
                    }
                }
                bool flag2 = 0;
                for(int j = 0; j < n; j++){
                    if(flag2) break;
                    for(int k = j + 1; k < n; k++){
                        if((i & (1 << j)) || (i & (1 << k))) continue;
                        if(pre[k + 1] - pre[j] == 1 && dp[i ^ (1 << j) ^ (1 << k)]){
                            dp[i] = 1;
                            lst[i] = (i ^ (1 << j) ^ (1 << k));
                            for(int l = j + 1; l < k; l++){
                                if(i & (1 << l)){
                                    lstans[i] = l;
                                    break;
                                }
                            }
                            flag2 = 1;
                            break;
                        }
                    }
                }
            }
        }
        int now = 0;
        while(q--){
            int tmp;
            cin >> tmp;
            now += (1 << (tmp - 1));
        }
        if(dp[now] == 0){
            cout << "-1\n";
        }
        else{
            vector<int> ans;
            while(now < (1 << n) - 1){
                ans.push_back(lstans[now]);
                now = lst[now];
            }
            reverse(ans.begin(), ans.end());
            cout << (int)ans.size() << "\n";
            for(auto &x: ans) cout << x + 1 << ' ';
            cout << "\n";
        }
    }
}
// g++ -std=c++17 -Wall -Wextra -Wshadow -fsanitize=undefined -fsanitize=address -o run pBbrute.cpp -g
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...