QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#706187#9538. Closest Derangementucup-team1134#AC ✓440ms13472kbC++2318.8kb2024-11-03 09:04:132024-11-03 09:04:13

Judging History

你现在查看的是最新测评结果

  • [2024-11-03 09:04:13]
  • 评测
  • 测评结果:AC
  • 用时:440ms
  • 内存:13472kb
  • [2024-11-03 09:04:13]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pair<int,int>>
#define vll vector<pair<ll,ll>>
#define vvi vector<vector<int>>
#define vvl vector<vector<ll>>
#define vvii vector<vector<pair<int,int>>>
#define vvll vector<vector<pair<ll,ll>>>
#define vst vector<string>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define mkunique(x) sort(all(x));(x).erase(unique(all(x)),(x).end())
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=998244353,MAX=300005,INF=15<<26;

// BIT セグ木 遅延セグ木 のみ

// from: https://gist.github.com/yosupo06/ddd51afb727600fd95d9d8ad6c3c80c9
// (based on AtCoder STL)

#include <algorithm>
#include <array>
#ifdef _MSC_VER
#include <intrin.h>
#endif
namespace atcoder {
namespace internal {
int ceil_pow2(int n) {
    int x = 0;
    while ((1U << x) < (unsigned int)(n)) x++;
    return x;
}
int bsf(unsigned int n) {
#ifdef _MSC_VER
    unsigned long index;
    _BitScanForward(&index, n);
    return index;
#else
    return __builtin_ctz(n);
#endif
}
}  // namespace internal

}  // namespace atcoder

#include <cassert>
#include <numeric>
#include <type_traits>

namespace atcoder {

namespace internal {

#ifndef _MSC_VER
template <class T>
using is_signed_int128 =
typename std::conditional<std::is_same<T, __int128_t>::value ||
std::is_same<T, __int128>::value,
std::true_type,
std::false_type>::type;

template <class T>
using is_unsigned_int128 =
typename std::conditional<std::is_same<T, __uint128_t>::value ||
std::is_same<T, unsigned __int128>::value,
std::true_type,
std::false_type>::type;

template <class T>
using make_unsigned_int128 =
typename std::conditional<std::is_same<T, __int128_t>::value,
__uint128_t,
unsigned __int128>;

template <class T>
using is_integral = typename std::conditional<std::is_integral<T>::value ||
is_signed_int128<T>::value ||
is_unsigned_int128<T>::value,
std::true_type,
std::false_type>::type;

template <class T>
using is_signed_int = typename std::conditional<(is_integral<T>::value &&
                                                 std::is_signed<T>::value) ||
is_signed_int128<T>::value,
std::true_type,
std::false_type>::type;

template <class T>
using is_unsigned_int =
typename std::conditional<(is_integral<T>::value &&
                           std::is_unsigned<T>::value) ||
is_unsigned_int128<T>::value,
std::true_type,
std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<
is_signed_int128<T>::value,
make_unsigned_int128<T>,
typename std::conditional<std::is_signed<T>::value,
std::make_unsigned<T>,
std::common_type<T>>::type>::type;

#else

template <class T> using is_integral = typename std::is_integral<T>;

template <class T>
using is_signed_int =
typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,
std::true_type,
std::false_type>::type;

template <class T>
using is_unsigned_int =
typename std::conditional<is_integral<T>::value &&
std::is_unsigned<T>::value,
std::true_type,
std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<is_signed_int<T>::value,
std::make_unsigned<T>,
std::common_type<T>>::type;

#endif

template <class T>
using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;

template <class T>
using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;

template <class T> using to_unsigned_t = typename to_unsigned<T>::type;

}  // namespace internal

}  // namespace atcoder

#include <cassert>
#include <vector>

namespace atcoder {

template <class T> struct fenwick_tree {
    using U = internal::to_unsigned_t<T>;
    
public:
    fenwick_tree() : _n(0) {}
    fenwick_tree(int n) : _n(n), data(n) {}
    
    void add(int p, T x) {
        assert(0 <= p && p < _n);
        p++;
        while (p <= _n) {
            data[p - 1] += U(x);
            p += p & -p;
        }
    }
    
    T sum(int l, int r) {
        assert(0 <= l && l <= r && r <= _n);
        return sum(r) - sum(l);
    }
    
private:
    int _n;
    std::vector<U> data;
    
    U sum(int r) {
        U s = 0;
        while (r > 0) {
            s += data[r - 1];
            r -= r & -r;
        }
        return s;
    }
};

}  // namespace atcoder


#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>
namespace atcoder {

template <class S,
S (*op)(S, S),
S (*e)(),
class F,
S (*mapping)(F, S),
F (*composition)(F, F),
F (*id)()>
struct lazy_segtree {
public:
    lazy_segtree() : lazy_segtree(0) {}
    lazy_segtree(int n) : lazy_segtree(std::vector<S>(n, e())) {}
    lazy_segtree(const std::vector<S>& v) : _n(int(v.size())) {
        log = internal::ceil_pow2(_n);
        size = 1 << log;
        d = std::vector<S>(2 * size, e());
        lz = std::vector<F>(size, id());
        for (int i = 0; i < _n; i++) d[size + i] = v[i];
        for (int i = size - 1; i >= 1; i--) {
            update(i);
        }
    }
    
    void set(int p, S x) {
        assert(0 <= p && p < _n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        d[p] = x;
        for (int i = 1; i <= log; i++) update(p >> i);
    }
    
    S get(int p) {
        assert(0 <= p && p < _n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        return d[p];
    }
    
    S prod(int l, int r) {
        assert(0 <= l && l <= r && r <= _n);
        if (l == r) return e();
        
        l += size;
        r += size;
        
        for (int i = log; i >= 1; i--) {
            if (((l >> i) << i) != l) push(l >> i);
            if (((r >> i) << i) != r) push(r >> i);
        }
        
        S sml = e(), smr = e();
        while (l < r) {
            if (l & 1) sml = op(sml, d[l++]);
            if (r & 1) smr = op(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }
        
        return op(sml, smr);
    }
    
    S all_prod() { return d[1]; }
    
    void apply(int p, F f) {
        assert(0 <= p && p < _n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        d[p] = mapping(f, d[p]);
        for (int i = 1; i <= log; i++) update(p >> i);
    }
    void apply(int l, int r, F f) {
        assert(0 <= l && l <= r && r <= _n);
        if (l == r) return;
        
        l += size;
        r += size;
        
        for (int i = log; i >= 1; i--) {
            if (((l >> i) << i) != l) push(l >> i);
            if (((r >> i) << i) != r) push((r - 1) >> i);
        }
        
        {
            int l2 = l, r2 = r;
            while (l < r) {
                if (l & 1) all_apply(l++, f);
                if (r & 1) all_apply(--r, f);
                l >>= 1;
                r >>= 1;
            }
            l = l2;
            r = r2;
        }
        
        for (int i = 1; i <= log; i++) {
            if (((l >> i) << i) != l) update(l >> i);
            if (((r >> i) << i) != r) update((r - 1) >> i);
        }
    }
    
    template <bool (*g)(S)> int max_right(int l) {
        return max_right(l, [](S x) { return g(x); });
    }
    template <class G> int max_right(int l, G g) {
        assert(0 <= l && l <= _n);
        assert(g(e()));
        if (l == _n) return _n;
        l += size;
        for (int i = log; i >= 1; i--) push(l >> i);
        S sm = e();
        do {
            while (l % 2 == 0) l >>= 1;
            if (!g(op(sm, d[l]))) {
                while (l < size) {
                    push(l);
                    l = (2 * l);
                    if (g(op(sm, d[l]))) {
                        sm = op(sm, d[l]);
                        l++;
                    }
                }
                return l - size;
            }
            sm = op(sm, d[l]);
            l++;
        } while ((l & -l) != l);
        return _n;
    }
    
    template <bool (*g)(S)> int min_left(int r) {
        return min_left(r, [](S x) { return g(x); });
    }
    template <class G> int min_left(int r, G g) {
        assert(0 <= r && r <= _n);
        assert(g(e()));
        if (r == 0) return 0;
        r += size;
        for (int i = log; i >= 1; i--) push((r - 1) >> i);
        S sm = e();
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!g(op(d[r], sm))) {
                while (r < size) {
                    push(r);
                    r = (2 * r + 1);
                    if (g(op(d[r], sm))) {
                        sm = op(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = op(d[r], sm);
        } while ((r & -r) != r);
        return 0;
    }
    
private:
    int _n, size, log;
    std::vector<S> d;
    std::vector<F> lz;
    
    void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
    void all_apply(int k, F f) {
        d[k] = mapping(f, d[k]);
        if (k < size) lz[k] = composition(f, lz[k]);
    }
    void push(int k) {
        all_apply(2 * k, lz[k]);
        all_apply(2 * k + 1, lz[k]);
        lz[k] = id();
    }
};

}  // namespace atcoder

#include <algorithm>
#include <cassert>
#include <vector>

namespace atcoder {

template <class S, S (*op)(S, S), S (*e)()> struct segtree {
public:
    segtree() : segtree(0) {}
    segtree(int n) : segtree(std::vector<S>(n, e())) {}
    segtree(const std::vector<S>& v) : _n(int(v.size())) {
        log = internal::ceil_pow2(_n);
        size = 1 << log;
        d = std::vector<S>(2 * size, e());
        for (int i = 0; i < _n; i++) d[size + i] = v[i];
        for (int i = size - 1; i >= 1; i--) {
            update(i);
        }
    }
    
    void set(int p, S x) {
        assert(0 <= p && p < _n);
        p += size;
        d[p] = x;
        for (int i = 1; i <= log; i++) update(p >> i);
    }
    
    S get(int p) {
        assert(0 <= p && p < _n);
        return d[p + size];
    }
    
    S prod(int l, int r) {
        assert(0 <= l && l <= r && r <= _n);
        S sml = e(), smr = e();
        l += size;
        r += size;
        
        while (l < r) {
            if (l & 1) sml = op(sml, d[l++]);
            if (r & 1) smr = op(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }
        return op(sml, smr);
    }
    
    S all_prod() { return d[1]; }
    
    template <bool (*f)(S)> int max_right(int l) {
        return max_right(l, [](S x) { return f(x); });
    }
    template <class F> int max_right(int l, F f) {
        assert(0 <= l && l <= _n);
        assert(f(e()));
        if (l == _n) return _n;
        l += size;
        S sm = e();
        do {
            while (l % 2 == 0) l >>= 1;
            if (!f(op(sm, d[l]))) {
                while (l < size) {
                    l = (2 * l);
                    if (f(op(sm, d[l]))) {
                        sm = op(sm, d[l]);
                        l++;
                    }
                }
                return l - size;
            }
            sm = op(sm, d[l]);
            l++;
        } while ((l & -l) != l);
        return _n;
    }
    
    template <bool (*f)(S)> int min_left(int r) {
        return min_left(r, [](S x) { return f(x); });
    }
    template <class F> int min_left(int r, F f) {
        assert(0 <= r && r <= _n);
        assert(f(e()));
        if (r == 0) return 0;
        r += size;
        S sm = e();
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!f(op(d[r], sm))) {
                while (r < size) {
                    r = (2 * r + 1);
                    if (f(op(d[r], sm))) {
                        sm = op(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = op(d[r], sm);
        } while ((r & -r) != r);
        return 0;
    }
    
private:
    int _n, size, log;
    std::vector<S> d;
    
    void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
};

}  // namespace atcoder

using T=pair<int,int>;

T f(T a,T b){
    return min(a,b);
}

T ti(){
    return mp(INF,INF);
}

vi gutyoku(vi Q,int K){
    int N=si(Q);
    if(N%2==0){
        if(K>1) return {-1};
        for(int i=0;i<N;i++){
            Q[i]^=1;
        }
        return Q;
    }else{
        if(K>N-1) return {-1};
        
    }
    vvi ans;
    vi P(N);iota(all(P),0);
    do{
        int d=0;
        for(int i=0;i<N;i++){
            if(P[i]==Q[i]) d=INF;
            else d+=abs(Q[i]-P[i]);
        }
        if(d==N+1){
            ans.pb(P);
        }
    }while(next_permutation(all(P)));
    
    return ans[K-1];
}
vi solve(vi P,int K){
    int N=si(P);
    if(N%2==0){
        if(K>1) return {-1};
        for(int i=0;i<N;i++){
            P[i]^=1;
        }
        return P;
    }
    
    if(K>N-1) return {-1};
    K--;
    
    vii S;
    for(int i=0;i<N-2;i+=2){
        S.pb(mp(i,1));
        S.pb(mp(i,2));
    }
    
    vi Q(N);
    for(int i=0;i<N;i++){
        Q[P[i]]=i;
    }
    
    vector<T> def(N);
    for(int i=0;i<N;i++){
        def[i]=mp(Q[i],i);
    }
    atcoder::segtree<T,f,ti> seg(def);
    
    sort(all(S),[&](auto x,auto y){
        if(x.fi==y.fi){
            int po=min({Q[x.fi],Q[x.fi+1],Q[x.fi+2]});
            if(P[po]==x.fi) return x.se<y.se;
            else if(P[po]==x.fi+1) return x.se>y.se;
            else return x.se<y.se;
        }else{
            int ss,tt;
            int l=min(x.fi,y.fi),r=max(x.fi,y.fi)+2;
            vi chan;
            while(1){
                auto [a,b]=seg.prod(l,r+1);
                
                int s,t;
                
                if(b<x.fi){
                    s=b^1;
                }else if(b<=x.fi+2){
                    if(x.se==1){
                        if(b==x.fi) s=x.fi+1;
                        else if(b==x.fi+1) s=x.fi+2;
                        else s=x.fi;
                    }else{
                        if(b==x.fi) s=x.fi+2;
                        else if(b==x.fi+1) s=x.fi;
                        else s=x.fi+1;
                    }
                }else{
                    if(b%2==0) s=b-1;
                    else s=b+1;
                }
                
                swap(x,y);
                swap(s,t);
                if(b<x.fi){
                    s=b^1;
                }else if(b<=x.fi+2){
                    if(x.se==1){
                        if(b==x.fi) s=x.fi+1;
                        else if(b==x.fi+1) s=x.fi+2;
                        else s=x.fi;
                    }else{
                        if(b==x.fi) s=x.fi+2;
                        else if(b==x.fi+1) s=x.fi;
                        else s=x.fi+1;
                    }
                }else{
                    if(b%2==0) s=b-1;
                    else s=b+1;
                }
                swap(x,y);
                swap(s,t);
                
                if(s!=t){
                    ss=s;
                    tt=t;
                    break;
                }
                
                seg.set(b,mp(INF,b));
                chan.pb(b);
            }
            
            
            for(int x:chan){
                seg.set(x,mp(Q[x],x));
            }
            return ss<tt;
        }
    });
    
    auto x=S[K];
    
    vi ans(N);
    for(int i=0;i<N;i++){
        int b=P[i];
        int s;
        if(b<x.fi){
            s=b^1;
        }else if(b<=x.fi+2){
            if(x.se==1){
                if(b==x.fi) s=x.fi+1;
                else if(b==x.fi+1) s=x.fi+2;
                else s=x.fi;
            }else{
                if(b==x.fi) s=x.fi+2;
                else if(b==x.fi+1) s=x.fi;
                else s=x.fi+1;
            }
        }else{
            if(b%2==0) s=b-1;
            else s=b+1;
        }
        ans[i]=s;
    }
    
    return ans;
    /*
    K--;
    
    int L=0,R=N-1;
    int st=-1;
    vi ans=P;
    
    for(int i=0;i<N;i++){
        if(P[i]<L||R<P[i]) continue;
        vector<array<int,3>> cand;
        for(int x=P[i]-2;x<=P[i];x++){
            if(L<=x&&(x-L)%2==0&&x+2<=R){
                if(x!=P[i]) cand.pb({x,x,1});
                if(x+1!=P[i]) cand.pb({x+1,x,1});
                if(x+2!=P[i]) cand.pb({x+2,x,1});
            }
        }
        for(int x=P[i]-1;x<=P[i];x++){
            if(L<=x&&x+1<=R){
                if((x-L)%2==0){
                    if(R-(x+1)>=3){
                        if(x==P[i]-1) cand.pb({x,-1,(R-(x+1))/2*2});
                        else cand.pb({x+1,-1,(R-(x+1))/2*2});
                    }
                }else{
                    if(x-L>=3){
                        if(x==P[i]-1) cand.pb({x,-2,(x-L)/2*2});
                        else cand.pb({x+1,-2,(x-L)/2*2});
                    }
                }
            }
        }
        
        sort(all(cand));
    }
     */
}
int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    /*
    for(int N=3;N<=9;N+=2){
        vi P(N);iota(all(P),0);
        do{
            for(int K=1;K<=N-1;K++){
                auto a=solve(P,K),b=gutyoku(P,K);
                assert(a==b);
            }
        }while(next_permutation(all(P)));
        cout<<N<<endl;
    }
    
    for(int N=3;N<=9;N+=2){
        vi P(N);iota(all(P),0);
        vi dis(N*N+4);
        for(int x:P) cout<<x<<" ";
        cout<<endl;
        vvi r;
        do{
            int d=0;
            for(int i=0;i<N;i++){
                if(P[i]==i) d=INF;
                else d+=abs(i-P[i]);
            }
            if(d==N+1){
                r.pb(P);
            }
        }while(next_permutation(all(P)));
        reverse(all(r));
        for(int i=0;i+1<si(r);i+=2) swap(r[i],r[i+1]);
        for(auto q:r){
            for(int x:q) cout<<x<<" ";
            cout<<endl;
        }
        cout<<endl;
    }
     */
    
    int Q;cin>>Q;
    while(Q--){
        int N,K;cin>>N>>K;
        vi P(N);
        for(int i=0;i<N;i++){
            cin>>P[i];P[i]--;
        }
        auto Q=solve(P,K);
        if(Q[0]==-1) cout<<-1<<"\n";
        else{
            for(int x:Q) cout<<x+1<<" ";
            cout<<"\n";
        }
    }
}



这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3588kb

input:

2
2 2
2 1
3 2
1 2 3

output:

-1
3 1 2 

result:

ok 2 lines

Test #2:

score: 0
Accepted
time: 0ms
memory: 3584kb

input:

50
6 1
4 2 6 3 5 1
8 1
6 2 8 7 3 4 1 5
3 298728530
2 3 1
4 610615077
2 4 3 1
4 2
4 2 3 1
7 152065238
4 1 3 5 7 6 2
6 844435075
2 6 4 5 1 3
4 544008000
3 2 4 1
9 379783780
5 9 3 8 4 2 1 7 6
5 139426002
2 4 5 3 1
2 1
1 2
2 1
1 2
3 1
3 1 2
4 2
2 4 1 3
4 1
3 2 4 1
5 4
2 4 1 5 3
3 1
1 2 3
6 805720317
5 6...

output:

3 1 5 4 6 2 
5 1 7 8 4 3 2 6 
-1
-1
-1
-1
-1
-1
-1
-1
2 1 
2 1 
1 2 3 
-1
4 1 3 2 
3 5 2 4 1 
2 3 1 
-1
-1
-1
2 1 
-1
-1
2 4 3 1 
-1
-1
-1
-1
-1
5 3 7 8 6 4 1 2 
1 3 2 
-1
3 7 9 5 8 1 4 2 6 
-1
1 2 
-1
-1
-1
8 5 3 2 4 7 1 6 
-1
-1
5 1 6 7 4 3 2 
-1
5 2 4 1 6 3 7 
1 2 
-1
2 1 
-1
4 1 3 2 5 
-1

result:

ok 50 lines

Test #3:

score: 0
Accepted
time: 0ms
memory: 3584kb

input:

100
7 3
6 4 3 5 1 2 7
7 2
7 6 5 1 3 4 2
3 579339385
3 1 2
3 244980876
1 2 3
2 1
2 1
6 184906010
1 4 2 6 5 3
6 908625780
3 2 6 4 5 1
4 223847761
2 3 1 4
7 4078777
1 3 4 2 6 5 7
5 955859204
1 3 5 4 2
5 3
4 5 3 2 1
7 6
1 7 4 3 6 2 5
6 2
4 2 3 1 6 5
2 1
1 2
7 6
5 4 6 7 2 1 3
2 2
2 1
3 2
2 3 1
6 38861221...

output:

7 3 5 4 2 1 6 
6 5 7 2 4 3 1 
-1
-1
1 2 
-1
-1
-1
-1
-1
5 4 1 3 2 
3 6 5 2 7 1 4 
-1
2 1 
7 3 5 6 1 2 4 
-1
3 1 2 
-1
5 2 4 3 1 6 
-1
2 3 1 
-1
-1
4 1 2 3 
-1
-1
2 1 3 
-1
-1
-1
-1
-1
1 2 
-1
-1
-1
2 5 3 4 1 
1 2 3 
6 5 4 1 3 2 
-1
5 7 4 3 1 2 6 
2 4 1 5 3 
-1
-1
2 4 3 6 1 5 
-1
2 4 6 1 3 5 
1 5 4 2...

result:

ok 100 lines

Test #4:

score: 0
Accepted
time: 0ms
memory: 3880kb

input:

100
8 2
8 6 4 1 7 5 3 2
3 1
2 1 3
4 408738161
1 3 4 2
9 392326366
4 3 2 1 8 7 6 5 9
7 6
2 5 1 3 7 4 6
3 662976110
1 2 3
4 584704458
3 1 2 4
2 170402059
2 1
8 195525462
8 4 2 1 7 6 5 3
5 241372504
4 5 2 3 1
2 1
1 2
7 2
6 3 7 4 1 2 5
6 1
6 3 5 1 2 4
7 5
3 2 7 4 1 6 5
8 2
5 3 4 6 1 2 7 8
4 1
2 4 3 1
2 ...

output:

-1
1 3 2 
-1
-1
3 4 2 1 6 5 7 
-1
-1
-1
-1
-1
2 1 
7 1 6 5 2 3 4 
5 4 6 2 1 3 
4 1 6 5 2 7 3 
-1
1 3 4 2 
-1
-1
2 1 3 
-1
1 3 6 5 7 4 2 
-1
-1
5 3 1 4 2 
-1
-1
-1
-1
-1
3 5 2 4 6 1 
3 1 2 
-1
5 4 9 3 7 1 2 8 6 
-1
-1
-1
-1
-1
-1
-1
2 1 3 
2 1 
-1
-1
1 6 5 2 4 3 7 8 
-1
8 2 3 5 6 9 4 7 1 
2 3 1 
-1
-...

result:

ok 100 lines

Test #5:

score: 0
Accepted
time: 1ms
memory: 3892kb

input:

1000
4 2
2 1 4 3
3 1
3 2 1
7 110856137
6 5 1 7 4 2 3
7 253418683
2 6 7 4 3 1 5
2 2
1 2
7 763879058
7 5 3 1 6 4 2
8 518300421
1 8 4 2 3 5 7 6
3 241647870
2 1 3
4 400296427
2 1 4 3
3 477979797
1 3 2
6 2
4 1 3 6 5 2
8 2
3 7 5 4 2 8 1 6
8 1
3 2 1 5 7 8 6 4
3 2
2 3 1
9 3
1 6 7 2 5 4 3 8 9
5 1
3 2 5 1 4
4...

output:

-1
1 3 2 
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
4 1 2 6 8 7 5 3 
3 1 2 
2 5 9 1 6 3 4 7 8 
1 3 4 2 5 
-1
-1
4 1 2 5 3 
-1
3 8 2 1 5 9 7 6 4 
-1
-1
4 3 2 7 1 5 6 
-1
-1
-1
-1
-1
2 1 3 4 6 5 
2 1 
-1
-1
-1
2 1 3 
-1
2 1 
1 5 4 6 8 7 3 2 
-1
-1
3 2 4 5 1 
3 2 4 1 
-1
3 1 2 
3 2 1 4 
-1
8 1 3 2 9 7 5 4 6 
7 4 6...

result:

ok 1000 lines

Test #6:

score: 0
Accepted
time: 1ms
memory: 3556kb

input:

1000
6 2
6 4 2 3 1 5
9 7
6 5 3 9 1 7 2 8 4
8 290062773
5 2 6 7 8 3 4 1
6 764373478
6 2 1 5 4 3
2 2
2 1
4 280972480
1 4 2 3
3 931925743
2 3 1
5 698025972
2 5 3 1 4
3 260868128
3 2 1
4 15623583
3 4 2 1
5 2
2 5 1 4 3
7 5
3 1 5 4 2 6 7
10 2
1 5 4 3 8 2 9 7 10 6
7 3
5 2 4 6 7 1 3
10 1
5 9 2 8 7 1 4 6 10 ...

output:

-1
7 4 5 8 2 6 1 9 3 
-1
-1
-1
-1
-1
-1
-1
-1
1 4 2 3 5 
4 2 7 3 1 5 6 
-1
4 1 5 7 6 3 2 
6 10 1 7 8 2 3 5 9 4 
4 5 1 2 3 
-1
-1
4 2 6 1 9 7 3 5 8 
-1
-1
-1
-1
-1
-1
-1
5 2 1 3 4 6 
-1
-1
-1
9 5 2 4 7 3 1 6 8 
-1
3 1 4 5 2 
-1
5 7 1 6 2 4 8 9 3 
-1
3 4 5 1 2 
2 1 3 
2 1 4 3 7 6 5 
-1
-1
3 9 5 4 8 6 ...

result:

ok 1000 lines

Test #7:

score: 0
Accepted
time: 198ms
memory: 3948kb

input:

100
3412 1
258 1465 305 1663 2883 340 3112 1078 1464 1315 1418 2018 141 2901 1121 1382 938 1208 3289 3016 2358 1270 1795 3335 1566 1284 2091 1582 3137 3276 2873 1838 2049 3274 438 266 2827 1337 440 1063 2895 2953 1433 84 253 1547 3360 3125 530 2708 2985 181 2559 3308 1660 1609 287 2737 1894 871 3048...

output:

257 1466 306 1664 2884 339 3111 1077 1463 1316 1417 2017 142 2902 1122 1381 937 1207 3290 3015 2357 1269 1796 3336 1565 1283 2092 1581 3138 3275 2874 1837 2050 3273 437 265 2828 1338 439 1064 2896 2954 1434 83 254 1548 3359 3126 529 2707 2986 182 2560 3307 1659 1610 288 2738 1893 872 3047 403 3361 8...

result:

ok 100 lines

Test #8:

score: 0
Accepted
time: 40ms
memory: 4716kb

input:

131
20 1
15 20 9 10 2 3 19 5 12 11 18 17 16 4 1 14 6 13 7 8
5 2
5 3 4 2 1
6 1
3 5 4 1 6 2
41 19
6 5 1 38 41 20 10 4 21 30 22 29 24 36 16 2 8 12 27 23 3 39 33 26 25 32 18 19 37 40 15 31 11 34 14 9 28 35 7 17 13
9 6
5 8 4 7 6 1 9 3 2
4 1
1 2 4 3
41 5
33 4 8 13 7 9 23 11 36 26 25 5 39 41 2 15 31 28 30 ...

output:

16 19 10 9 1 4 20 6 11 12 17 18 15 3 2 13 5 14 8 7 
4 1 5 3 2 
4 6 3 2 5 1 
5 6 2 39 40 19 9 3 22 31 21 30 23 37 15 1 7 11 28 24 4 38 32 25 26 33 17 20 36 41 16 29 12 35 13 10 27 34 8 18 14 
6 9 3 5 7 2 8 4 1 
2 1 3 4 
32 3 7 12 8 11 22 10 37 27 24 6 38 40 1 14 30 29 31 41 5 2 23 18 20 33 26 4 16 13...

result:

ok 131 lines

Test #9:

score: 0
Accepted
time: 2ms
memory: 3828kb

input:

1000
33 13
2 18 17 8 25 30 1 29 19 6 11 13 9 14 10 22 4 26 23 31 27 3 28 15 7 24 20 21 33 32 16 5 12
40 43
24 27 31 4 26 8 33 3 25 34 28 30 7 22 38 17 14 39 11 23 20 13 10 37 15 40 5 21 32 1 29 36 2 9 35 18 19 6 16 12
41 81
2 14 3 11 1 27 33 16 40 32 31 18 39 38 26 29 20 34 30 25 12 6 8 19 17 21 4 2...

output:

1 17 18 7 26 31 2 30 20 5 12 14 10 13 9 21 3 25 24 29 28 4 27 16 8 23 19 22 32 33 15 6 11 
-1
-1
-1
19 9 7 6 12 5 13 15 8 3 18 14 1 4 11 10 17 16 2 
-1
39 19 40 13 12 49 10 43 42 22 32 14 3 5 26 28 9 23 2 7 11 16 46 24 18 1 27 30 25 29 6 41 48 20 47 37 8 44 35 31 45 17 33 15 34 21 38 4 36 
-1
-1
-1
...

result:

ok 1000 lines

Test #10:

score: 0
Accepted
time: 12ms
memory: 3696kb

input:

1000
47 88
38 47 17 13 27 3 1 25 11 23 9 5 41 40 31 20 16 46 6 2 30 19 28 32 10 26 43 36 15 45 29 33 21 24 39 34 7 44 22 42 8 14 18 12 35 37 4
30 33
5 29 3 21 2 27 25 4 19 23 17 6 7 8 14 18 20 1 26 24 28 12 13 22 16 15 9 11 30 10
62 85
17 50 62 16 15 28 20 27 57 31 11 53 58 61 14 39 24 3 33 34 38 49...

output:

-1
-1
-1
2 17 65 8 80 6 81 15 26 16 83 59 68 91 33 40 61 78 74 12 92 75 70 64 7 21 24 23 38 62 29 45 84 34 20 39 85 9 86 30 41 57 56 27 13 52 79 88 82 72 55 47 42 89 90 11 5 31 73 1 77 51 67 76 19 60 18 25 32 93 35 4 49 28 48 69 87 22 46 43 71 66 37 63 50 10 54 53 58 36 44 3 14 
-1
2 4 3 1 
-1
-1
-1...

result:

ok 1000 lines

Test #11:

score: 0
Accepted
time: 104ms
memory: 3636kb

input:

10000
15 15
14 2 5 7 9 15 4 12 10 6 1 3 13 11 8
84 4
56 16 12 74 14 71 40 21 13 52 78 2 4 46 29 3 36 70 32 22 75 58 63 27 45 6 26 47 64 33 44 31 15 28 24 1 67 30 83 68 76 73 82 39 54 38 35 41 42 61 53 11 66 34 51 37 81 25 62 17 18 5 57 60 10 84 8 48 9 19 69 80 79 7 23 59 49 43 20 55 72 77 65 50
9 17...

output:

-1
-1
-1
-1
52 55 34 79 54 22 91 82 94 16 86 41 9 38 31 78 75 72 15 46 6 45 17 35 56 70 58 73 3 40 62 57 39 84 26 65 59 53 13 87 80 92 69 88 76 19 21 90 89 4 11 42 95 44 93 51 30 48 32 36 37 5 7 63 33 50 1 49 2 28 8 24 74 77 27 47 83 43 12 14 29 20 68 85 67 25 81 10 61 60 18 71 64 66 23 
-1
67 80 22...

result:

ok 10000 lines

Test #12:

score: 0
Accepted
time: 149ms
memory: 3808kb

input:

1000
999 1602
532 982 788 569 395 942 891 814 393 246 562 806 668 631 296 787 663 782 641 448 971 98 170 14 361 249 46 133 484 402 41 608 885 825 790 724 614 304 411 644 779 567 290 931 449 47 242 121 708 433 16 358 536 933 784 773 180 851 80 459 896 254 956 604 970 540 215 541 537 639 669 939 943 3...

output:

-1
338 79 456 38 709 479 273 451 252 617 233 464 503 511 64 342 42 541 627 679 241 275 299 25 160 75 494 182 67 172 18 316 231 680 558 578 690 715 112 107 449 741 434 188 610 422 448 559 81 223 203 676 63 333 163 705 637 232 144 698 518 26 362 738 493 153 589 548 122 602 80 263 381 329 83 565 177 58...

result:

ok 1000 lines

Test #13:

score: 0
Accepted
time: 146ms
memory: 3788kb

input:

100
186 134
128 34 170 56 136 10 62 50 48 144 1 66 91 99 153 149 77 87 156 8 29 36 57 183 69 90 28 51 115 76 21 101 49 65 71 173 73 25 154 70 116 89 24 103 22 167 138 68 88 119 31 46 166 125 126 181 78 178 104 9 130 60 11 177 127 44 47 4 30 45 171 185 3 38 92 162 17 63 114 118 23 106 81 184 37 27 14...

output:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
1732 762 2848 1365 107 1567 1202 2362 970 573 1051 2142 1037 1479 1003 24 439 2026 2352 304 2689 2579 500 1658 1232 6 1402 2824 2841 967 1044 411 2343 1322 1254 3015 1135 780 3125 1937 2494 232 1175 1807 1454 2582 853 3104 1475 1843 2059 2005 383 2929 ...

result:

ok 100 lines

Test #14:

score: 0
Accepted
time: 238ms
memory: 8084kb

input:

10
32077 42391
17986 10802 3107 8912 23279 16553 26516 8313 5924 9342 3829 29290 420 18508 29472 24529 26449 5893 28485 983 8073 2722 21478 23941 22613 19864 12364 25856 27509 7951 21218 17202 13121 17591 11609 13300 15719 31876 26033 24795 25899 6433 15759 2051 29655 12031 17813 16091 28725 13894 1...

output:

-1
-1
14839 5132 2966 10074 468 13948 15989 2478 9665 9212 14639 13705 2031 13708 11921 1614 3620 20187 5082 6026 15300 2595 9727 11867 4001 10527 1418 3619 5085 3980 19407 3520 18446 11589 15261 2454 10763 9889 18895 19896 11432 2793 5 3503 19524 17879 7552 12847 12146 15744 15301 16217 18281 20746...

result:

ok 10 lines

Test #15:

score: 0
Accepted
time: 440ms
memory: 13472kb

input:

5
200000 357261
49805 123255 26648 123787 16881 172698 147556 44746 40569 173176 122155 103751 174194 42928 65635 130130 149126 189179 107060 107100 24399 154028 105771 158652 109927 78981 3086 159721 126652 130391 194285 50663 143730 191701 193671 180950 28088 11584 174201 191920 25968 31954 116771...

output:

-1
143838 18024 41263 111462 34584 179044 118745 153971 5814 99626 107956 133071 46127 136547 100260 54744 167298 121071 166176 199011 132209 109000 47621 171257 168076 199027 190697 127624 194291 169706 63927 104217 38796 198715 49650 136501 132599 122404 169308 8320 55450 132159 83408 184311 18244...

result:

ok 5 lines

Extra Test:

score: 0
Extra Test Passed