QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#279601 | #6962. Far Away from Home | IsaacMoris# | AC ✓ | 2239ms | 104660kb | C++14 | 2.5kb | 2023-12-08 21:54:48 | 2023-12-08 21:54:49 |
Judging History
answer
#include<iostream>
#include <bits/stdc++.h>
#define ld long double
#define ll long long
#define IO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
const int N = 30, inf = 2e9;
void doWork() {
int n, c;
cin >> n >> c;
map<int, vector<int> > pos;
while (n--) {
int x, t;
cin >> x >> t;
while (t--) {
int a;
cin >> a;
pos[a].push_back(x);
}
}
vector<pair<int, int> > seg;
for (auto i: pos) {
vector<int> &v = i.second;
sort(v.begin(), v.end());
int prv = -inf;
for (auto i: v) {
seg.push_back({prv, i});
prv = i;
}
seg.push_back({prv, inf});
}
sort(seg.rbegin(), seg.rend());
vector<int> good_points;
for (auto i: seg) {
good_points.push_back(i.first);
good_points.push_back(i.second);
}
sort(good_points.begin(), good_points.end());
good_points.resize(unique(good_points.begin(), good_points.end()) - good_points.begin());
good_points.pop_back();
good_points.erase(good_points.begin());
ll sum_LR = 0, sum_L = 0, cnt = 0;
multiset<ll> q1; // for L+R >= 2I
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> q2; // curSegments R , L
auto add_seg = [&](ll l, ll r) {
sum_L += l;
q1.insert(l + r);
q2.push({r, l});
};
auto del_seg = [&](ll i) {
ll l = q2.top().second, r = q2.top().first;
q2.pop();
sum_L -= l;
if (l + r < 2 * i) {
sum_LR -= l + r;
cnt--;
} else {
q1.erase(q1.find(l + r));
}
};
ll ans = 1e18;
for (auto i: good_points) {
while (!seg.empty() && seg.back().first < i) {
add_seg(seg.back().first, seg.back().second);
seg.pop_back();
}
while (!q1.empty() && *q1.begin() < 2 * i) {
sum_LR += *q1.begin();
cnt++;
q1.erase(q1.begin());
}
while (!q2.empty() && q2.top().first <= i) {
del_seg(i);
}
int n = q2.size();
ans = min(ans, sum_LR + (n - 2 * cnt) * i - sum_L);
}
cout << ans << "\n";
}
int main() {
IO
int t = 1;
cin >> t;
for (int i = 1; i <= t; i++) {
// cout << "Case #" << i << ": ";
doWork();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2239ms
memory: 104660kb
input:
5 99674 20 763240852 6 13 12 1 19 14 15 907986528 9 9 13 2 3 10 5 8 11 20 405330475 5 8 20 18 19 7 350664157 3 14 7 11 866108800 7 5 15 19 17 7 1 16 243752093 3 5 6 16 957293963 7 6 2 7 12 3 8 11 87866078 8 8 15 2 10 16 4 5 18 599501459 4 15 20 10 13 75898433 3 15 1 13 634860118 3 4 10 12 931882462 ...
output:
8461 225014484653807 4191726 32192409196439 2242332818199
result:
ok 5 lines