QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#554051 | #4425. Cake | wisniewskij | WA | 4709ms | 155912kb | C++20 | 2.9kb | 2024-09-09 03:13:31 | 2024-09-09 03:13:32 |
Judging History
answer
#include <bits/stdc++.h>
#define ndl '\n'
#define ll long long
#define INF 1000000007
#define st first
#define nd second
#define debug(x) cout << #x << ": " << x << ndl
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
#define all(x) (x).begin(), (x).end()
using namespace std;
ll P{ static_cast<ll>(1e9 + 7) };
void rob() {
int n; cin >> n;
vector<vector<int>> a(n, vector<int>(2)), b(n, vector<int>(2));
for (int i = 0;i < n;i++) cin >> a[i][0];
for (int i = 0;i < n;i++) cin >> a[i][1];
for (int i = 0;i < n;i++) cin >> b[i][0];
for (int i = 0;i < n;i++) cin >> b[i][1];
for (int i = 1;i < n;i += 2) {
swap(a[i][0], a[i][1]);
swap(b[i][0], b[i][1]);
}
vector<ll> ah(n), bh(n);
for (int i = 0;i < n;i++) {
ah[i] = a[i][0] + P * a[i][1];
bh[i] = b[i][0] + P * b[i][1];
}
map<ll, vector<int>> hidb;
map<ll, int> idx_b;
vector<int> perm(n);
for (int i = 0;i < n;i++) {
if (hidb.find(bh[i]) == hidb.end()) {
hidb.insert({ bh[i], vector<int>(1, i) });
idx_b[bh[i]] = 0;
}
else {
hidb[bh[i]].push_back(i);
}
}
bool ok = true;
for (int i = 0;i < n;i++) {
if (hidb.find(ah[i]) == hidb.end() || hidb[ah[i]].size() <= idx_b[ah[i]]) {
ok = false;
break;
}
else {
perm[i] = hidb[ah[i]][idx_b[ah[i]]++];
}
}
if (!ok) {
cout << -1 << '\n';
return;
}
int ans = 0;
vector<int> tmp(n);
function<void(int, int)> bubble_sort = [&](int a, int b) {
if (a == b) return;
if (a + 1 == b) {
if (perm[a] > perm[b]) {
ans++;
swap(perm[a], perm[b]);
}
return;
}
int mid_point = (a + b) / 2;
bubble_sort(a, mid_point);
bubble_sort(mid_point + 1, b);
int i1 = a, i2 = mid_point + 1;
for (int i = a; i <= b;i++) {
if (i1 == mid_point + 1) {
// ans += i2 - i1;
tmp[i] = perm[i2++];
}
else if (i2 == b + 1) {
tmp[i] = perm[i1++];
}
else {
if (perm[i1] < perm[i2]) {
tmp[i] = perm[i1++];
}
else {
ans += i2 - i;
tmp[i] = perm[i2++];
}
}
}
for (int i = a; i <= b;i++)
perm[i] = tmp[i];
};
bubble_sort(0, n - 1);
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int t;
cin >> t;
for (int i = 0;i < t;i++)
rob();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 4709ms
memory: 155912kb
input:
4 500000 471518156 319758862 812815356 520822448 129241996 461169933 796713727 608641317 281180101 953966756 749634941 274104949 996181952 88142916 998544672 125597509 991731126 974767231 338911715 674197249 167602044 682799026 226927279 703198907 216742488 8185420 94921423 690039818 859329736 45428...
output:
0 -1 445698416 -1855464059
result:
wrong answer 3rd lines differ - expected: '124999750000', found: '445698416'