QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#225614 | #4425. Cake | janix4000 | AC ✓ | 2818ms | 69684kb | C++17 | 3.1kb | 2023-10-24 21:02:01 | 2023-10-24 21:02:02 |
Judging History
answer
#include <bits/stdc++.h>
#include <vector>
template <typename... Ts>
void __print(Ts &&...ts) {}
#ifdef DEBUG
#include "print.hpp"
#endif // DEBUG
using namespace std;
#define all(x) x.begin(), x.end()
#define sz(x) ((int)(x).size())
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vs = vector<string>;
using vvs = vector<vs>;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vpi = vector<pi>;
///////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
ll _mergeSort(vi& arr, vi& temp, int left, int right);
ll merge(vi& arr, vi& temp, int left, int mid, int right);
// This function sorts the
// input array and returns the
// number of inversions in the array
ll mergeSort(vi& arr)
{
vi temp(arr.size());
return _mergeSort(arr, temp, 0, arr.size() - 1);
}
ll _mergeSort(vi& arr, vi& temp, int left, int right)
{
ll mid, inv_count = 0;
if (right > left) {
mid = (right + left) / 2;
inv_count += _mergeSort(arr, temp, left, mid);
inv_count += _mergeSort(arr, temp, mid + 1, right);
inv_count += merge(arr, temp, left, mid + 1, right);
}
return inv_count;
}
ll merge(vi& arr, vi& temp, int left, int mid, int right)
{
int i, j, k;
ll inv_count = 0;
i = left;
j = mid;
k = left;
while ((i <= mid - 1) && (j <= right)) {
if (arr[i] <= arr[j]) {
temp[k++] = arr[i++];
}
else {
temp[k++] = arr[j++];
inv_count = inv_count + (mid - i);
}
}
while (i <= mid - 1)
temp[k++] = arr[i++];
while (j <= right)
temp[k++] = arr[j++];
for (i = left; i <= right; i++)
arr[i] = temp[i];
return inv_count;
}
void solution() {
int n; cin >> n;
vvi in(2, vi(n));
vvi tg(2, vi(n));
for (size_t k = 0; k < 2; k++)
{
for (size_t i = 0; i < n; i++)
{
cin >> in[k][i];
}
}
for (size_t k = 0; k < 2; k++)
{
for (size_t i = 0; i < n; i++)
{
cin >> tg[k][i];
}
}
map<pi, vi> ids;
for (size_t i = 0; i < n; i++)
{
int a = tg[0][i];
int b = tg[1][i];
if(i % 2) swap(a, b);
ids[{a, b}].push_back(i);
}
vi res(n);
for (int i = n - 1; i >= 0; i--)
{
int a = in[0][i];
int b = in[1][i];
if(i % 2) swap(a, b);
auto it = ids.find({a, b});
if(it == ids.end() || it->second.empty()) {
res.clear();
break;
}
int idx = it->second.back();
it->second.pop_back();
res[i] = idx;
}
if(res.empty()) {
cout << -1 << endl;
} else {
cout << mergeSort(res) << endl;
}
}
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int qs = 1;
cin >> qs;
while (qs--) {
solution();
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 2380ms
memory: 69684kb
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 124999750000 62569045381
result:
ok 4 lines
Test #2:
score: 0
Accepted
time: 2818ms
memory: 69668kb
input:
4 500000 413978977 447850293 802546055 289227062 321812178 97254226 380555063 167654142 874843339 349071516 10868808 93093321 996630037 33169052 99312773 335065051 278187197 94810404 643530967 947474510 63047182 573006260 8470509 116477506 251504796 899920047 806158195 452300674 859769748 489596554 ...
output:
62623368052 62486676013 62520737719 62506121292
result:
ok 4 lines
Test #3:
score: 0
Accepted
time: 664ms
memory: 4716kb
input:
9082 7 714942297 703160828 709363739 23094554 461254804 159017923 666590460 196100940 70571239 755082029 321718821 55663261 378527017 335969190 321718821 335969190 70571239 755082029 378527017 55663261 714942297 23094554 666590460 703160828 709363739 159017923 461254804 196100940 5 2 1 1 2 1 2 1 2 2...
output:
13 5 0 -1 0 2 10 2 0 5 0 10 15 1 2336 -1 1 -1 -1 4 -1 15 0 9 45 15 3 4 0 11 -1 -1 10 -1 18 10 0 3 1 -1 0 1 0 5 -1 16 -1 -1 -1 15 -1 307294 -1 7 0 0 -1 -1 -1 0 2210 3 1 16 -1 -1 0 -1 0 0 5 5 10 3 0 -1 21 0 7 3 0 32308741 -1 20 -1 0 1 -1 9 6 2249 0 -1 0 55236 2265 1 12 -1 -1 29 0 -1 20 0 2 0 0 1 -1 0 ...
result:
ok 9082 lines
Extra Test:
score: 0
Extra Test Passed