QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#578655 | #9349. Exchanging Gifts | Yukari# | TL | 26ms | 14820kb | C++20 | 1.4kb | 2024-09-20 20:39:44 | 2024-09-20 20:39:45 |
Judging History
answer
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int read() {
int x = 0;
char c = getchar();
for (; !isdigit(c); c = getchar());
for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x;
}
int main() {
int t = read();
while (t--) {
int n = read(), tot = 0;
vector<array<int, 1000000>> vec(n);
unordered_map<int, int> id;
for (int i = 0; i < n; i++) {
if (read() == 1) {
int k = read();
for (int j = 1; j <= k; j++) {
int x = read();
if (!id.count(x)) id[x] = tot++;
vec[i][id[x]]++;
}
} else {
int x = read(), y = read();
for (int j = 0; j < 1e6; j++)
vec[i][j] = vec[x - 1][j] + vec[y - 1][j];
}
}
auto& g = vec[n - 1];
sort(g.begin(), g.end(), greater<int>());
ll last = 0, ans = 0;
for (auto& v : g) {
if (!v) continue;
if (v >= last)
ans += last * 2, last = v - last;
else
ans += v * 2, last -= v;
}
printf("%lld\n", ans);
}
return 0;
}
/*
2
1
1 5 3 3 2 1 3
3
1 3 3 3 2
1 4 2 2 3 3
2 1 2
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 26ms
memory: 14820kb
input:
2 1 1 5 3 3 2 1 3 3 1 3 3 3 2 1 4 2 2 3 3 2 1 2
output:
4 6
result:
ok 2 lines
Test #2:
score: -100
Time Limit Exceeded
input:
10000 100 1 30 371028678 371028678 371028678 716418076 398221499 591504380 398221499 398221499 591504380 777141390 398221499 591504380 591504380 777141390 287847807 716418076 777141390 716418076 777141390 287847807 287847807 287847807 371028678 371028678 398221499 777141390 371028678 6827702 6827702...