QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#312789#4831. Eager Sortingtuanlinh1230 0ms0kbC++202.1kb2024-01-24 12:07:552024-01-24 12:07:56

Judging History

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

  • [2024-01-24 12:07:56]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:0kb
  • [2024-01-24 12:07:55]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ld long double
using namespace std;

vector <pll> ran;
ll ask(ll i, ll j)
{
    if (i==j) return 0;
    cout << i << " " << j << endl;
    ll x; cin >> x; 
    if (x==-1) exit(0);
    return x;
}

void merge(ll i)
{
    ll L=ran[i].fi, M=ran[i].se, R=ran[i+1].se;
    vector <ll> pos(R+5, 0), a(R+5, 0);
    for (ll i=1; i<=R; i++) pos[i]=a[i]=i;
    auto Swap=[&](ll i, ll j)
    {
        if (i==j) return 0;
        if (!ask(i, j)) return 0;
        swap(pos[a[i]], pos[a[j]]);
        swap(a[i], a[j]);
        return 1;
    };
    if (!Swap(M, M+1)) return;
    for (ll lptr=L, rptr=M+1; lptr<=M || rptr<=R;)
    {
        ll p=L+(lptr-L)+(rptr-M-1);
        if (lptr>M) Swap(pos[rptr], p), rptr++;
        else if (rptr>R) Swap(pos[lptr], p), lptr++;
        else if (pos[lptr]<pos[rptr])
        {
            if (Swap(pos[lptr], pos[rptr]))
                Swap(pos[rptr], p), rptr++;
            else Swap(pos[lptr], p), lptr++;
        }
        else
        {
            if (Swap(pos[lptr], pos[rptr]))
                Swap(pos[lptr], p), lptr++;
            else Swap(pos[rptr], p), rptr++;
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    ll n, lo=1; cin >> n;
    for (ll i=2; i<=n; i++)
        if (ask(i-1, i))
        {
            if (lo<=i-2) ran.pb({lo, i-2});
            lo=i-1;
        }
    ran.pb({lo, n-1});
    // for (pll i:ran) cout << i.fi << " " << i.se << "\n";
    while (1)
    {
        vector <pll> rann;
        for (ll i=ran.size()%2; i<ran.size(); i+=2)
        {
            if (i+1==ran.size()) rann.pb(ran[i]);
            else merge(i), rann.pb({ran[i].fi, ran[i+1].se});
        }
        if (rann.size()==1)
        {
            cout << "-1 -1" << endl;
            exit(0);
        }
        // cout << "new\n";
        // for (pll i:rann) cout << i.fi << " " << i.se << "\n";
        swap(ran, rann);
    }
}

详细

Test #1:

score: 0
Instance #1 Time Limit Exceeded

Interactor to First Run

5
0
1
0
1
1
0
0

First Run to Interactor

1 2
2 3
3 4
4 5
3 4
2 3
4 3
-1 -1

Interactor to Second Run

5
1
0
0
0

Second Run to Interactor

1 2
2 3
3 4
4 5

Manager to Checker

OK
good job!

result: