QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#225605 | #4425. Cake | janix4000 | TL | 0ms | 0kb | C++14 | 2.3kb | 2023-10-24 20:49:07 | 2023-10-24 20:49:08 |
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>;
///////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
int getInvCount(vi& arr)
{
multiset<int> set1;
set1.insert(arr[0]);
int invcount = 0; // Initialize result
multiset<int>::iterator itset1; // Iterator for the set
// Traverse all elements starting from second
for (int i=1; i<arr.size(); i++)
{
set1.insert(arr[i]);
itset1 = set1.upper_bound(arr[i]);
invcount += distance(itset1, set1.end());
}
return invcount;
}
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 << getInvCount(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: 0
Time Limit Exceeded
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