QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#295708 | #4831. Eager Sorting | ucup-team2307# | 0 | 0ms | 0kb | C++20 | 1.7kb | 2023-12-31 19:38:27 | 2023-12-31 19:38:28 |
answer
#include<bits/stdc++.h>
#define all(x) begin(x), end(x)
using ll = long long;
using namespace std;
const int local = 0;
vector<int> hidden;
int ask(int l, int r) {
cout << "? " << l << " " << r << endl;
if(!local) {
cin >> l;
if(l == -1) exit(0);
} else {
l--, r--;
if(l > r) swap(l, r);
if(hidden[l] > hidden[r]) {
swap(hidden[l], hidden[r]);
return 1;
}
return 0;
}
return l;
}
vector<int> merge_sort(int l, int r) {
if(l == r) return { l };
int m = (l + r) / 2;
auto L = merge_sort(l, m);
auto R = merge_sort(m + 1, r);
vector<int> ord;
int x = 0, y = 0;
while(x < L.size() && y < R.size()) {
int t = ask(L[x], R[y]);
ord.push_back(min(L[x], R[y]));
if(t) swap(L[x], R[y]);
if(L[x] < R[y]) x++;
else y++;
}
ord.insert(ord.end(), x + all(L));
ord.insert(ord.end(), y + all(R));
return ord;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int n;
cin >> n;
if(local) {
mt19937 rng(123);
hidden.resize(n);
iota(all(hidden), 1);
shuffle(all(hidden), rng);
}
auto ord = merge_sort(1, n);
vector<int> a(n + 1);
// for(auto i : ord) cout << i << " "; cout << endl;
for(int i = 0; i < n; i++)
a[ord[i]] = i + 1;
for(int i = 1; i <= n; i++) {
int p = find(all(a), i) - a.begin();
if(p != i) {
ask(i, p);
}
swap(a[i], a[p]);
}
// for(auto i : a) cout << i << " "; cout << endl;
cout << "-1 -1" << endl;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Instance #1 Time Limit Exceeded
Interactor to First Run
5
First Run to Interactor
? 1 2
Interactor to Second Run
Second Run to Interactor
Manager to Checker
WA Invalid Operation 778886360 22085