QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#578655#9349. Exchanging GiftsYukari#TL 26ms14820kbC++201.4kb2024-09-20 20:39:442024-09-20 20:39:45

Judging History

你现在查看的是最新测评结果

  • [2024-09-20 20:39:45]
  • 评测
  • 测评结果:TL
  • 用时:26ms
  • 内存:14820kb
  • [2024-09-20 20:39:44]
  • 提交

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
*/

詳細信息

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...

output:


result: