답안 #62167

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
62167 2018-07-27T16:05:09 Z Benq DEL13 (info1cup18_del13) C++11
100 / 100
30 ms 4780 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;
 
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()

const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 100001;

int T;
bitset<MX> good;

void solve() {
    int N,Q; cin >> N >> Q;
    FOR(i,1,N+1) good[i] = 0;
    F0R(i,Q) {
        int x; cin >> x;
        good[x] = 1;
    }
    vector<vi> v;
    vi cur, V, vsz;
    FOR(i,1,N+1) {
        if (good[i] == 1) {
            v.pb(cur); vsz.pb(sz(cur)); cur.clear();
            V.pb(i);
        } else cur.pb(i);
    }
    v.pb(cur); vsz.pb(sz(cur));
    F0R(i,sz(vsz)) {
        if (vsz[i] < 0) {
            cout << "-1\n";
            return;
        }
        if (vsz[i]&1) {
            if (i == sz(vsz)-1) {
                cout << "-1\n";
                return;
            }
            vsz[i] --, vsz[i+1] --;
        }
    }
    /*for (auto a: vsz) cout << a << " ";
    cout << "\n";*/
    for (int i = 0; i < sz(vsz); i ++) {
        if (vsz[i] == sz(v[i]) && vsz[i] > 0) {
            int I = i;
            while (i < sz(vsz) && vsz[i] == sz(v[i]) && vsz[i] > 0) i ++;
            i --;
            // cout << "OOPS " << I << " " << i << "\n";
            int bes = -1;
            FOR(i1,I+1,i) if (vsz[i1] > 2) bes = i1; 
            if ((i-I+1) % 2 == 0) {
                FOR(i1,I,i+1) vsz[i1] -= 2;
            } else if (I && vsz[I-1]) {
                FOR(i1,I-1,i+1) vsz[i1] -= 2;
            } else if (I != i && bes != -1) {
                FOR(i1,I,i+1) vsz[i1] -= 2;
                vsz[bes] -= 2;
            } else if (i != sz(vsz)-1 && vsz[i+1]) {
                FOR(i1,I,i+2) vsz[i1] -= 2;
            } else {
                cout << "-1\n";
                return;
            }
        } 
    }
    cout << (N-Q)/2 << "\n";
    vi dif(sz(vsz));
    F0R(i,sz(vsz)) dif[i] = sz(v[i])-vsz[i];
    /*for (auto a: v) cout << sz(a) << " ";
    cout << "\n";
    for (int i: dif) cout << i << " ";
    cout << "\n";
    for (int i: vsz) cout << i << " ";
    cout << "\n";*/
    F0R(i,sz(vsz)) {
        int nes = vsz[i]/2;
        if (sz(v) != sz(vsz)) exit(5);
        F0R(j,nes) {
            if (sz(v[i]) < 3) exit(5);
            v[i].pop_back();
            int x = v[i].back(); v[i].pop_back(); cout << x << " ";
            v[i].pop_back(); v[i].pb(x);
        }
    }
    // cout << "HI " << sz(V) <<  " " << sz(dif) << "\n";
    // exit(0);
    F0R(i,sz(vsz)-1) while (dif[i] && dif[i+1]) {
        cout << V[i] << " ";
        dif[i] --, dif[i+1] --;
    }
    cout << "\n";
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> T;
    F0R(i,T) solve();
}

/* Look for:
* the exact constraints (multiple sets are too slow for n=10^6 :( ) 
* special cases (n=1?)
* overflow (ll vs int?)
* array bounds
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 376 KB Output is correct
2 Correct 4 ms 496 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 376 KB Output is correct
2 Correct 4 ms 496 KB Output is correct
3 Correct 15 ms 632 KB Output is correct
4 Correct 14 ms 764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 1160 KB Output is correct
2 Correct 9 ms 1672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 376 KB Output is correct
2 Correct 4 ms 496 KB Output is correct
3 Correct 15 ms 632 KB Output is correct
4 Correct 14 ms 764 KB Output is correct
5 Correct 5 ms 1672 KB Output is correct
6 Correct 4 ms 1672 KB Output is correct
7 Correct 4 ms 1672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 376 KB Output is correct
2 Correct 4 ms 496 KB Output is correct
3 Correct 15 ms 632 KB Output is correct
4 Correct 14 ms 764 KB Output is correct
5 Correct 5 ms 1672 KB Output is correct
6 Correct 4 ms 1672 KB Output is correct
7 Correct 4 ms 1672 KB Output is correct
8 Correct 21 ms 1672 KB Output is correct
9 Correct 28 ms 2804 KB Output is correct
10 Correct 21 ms 3128 KB Output is correct
11 Correct 30 ms 4780 KB Output is correct