Submission #807421

#TimeUsernameProblemLanguageResultExecution timeMemory
807421hugo_pmReconstruction Project (JOI22_reconstruction)C++17
3 / 100
5060 ms1048576 KiB
#include <bits/stdc++.h> #define int long long using namespace std; #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() #define rep(i, a, b) for(int i = (a); i < (b); i++) #define sz(v) ((int)((v).size())) template<typename T> void chmax(T &x, const T &v) { if (x < v) x = v; } template<typename T> void chmin(T &x, const T &v) { if (x > v) x = v; } using pii = pair<int, int>; using vi = vector<int>; string to_string(string s) { return s; } template <typename T> string to_string(T v) { bool first = true; string res = "["; for (const auto &x : v) { if (!first) res += ", "; first = false; res += to_string(x); } res += "]"; return res; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } void dbg_out() { cout << endl; } template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << to_string(H); dbg_out(T...); } #ifdef DEBUG #define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__) #else #define dbg(...) #endif struct uf { int N; vector<int> repr, taille; uf(int _N) : N(_N), repr(_N), taille(_N, 1) { iota(repr.begin(), repr.end(), 0); } int find(int u) { if (repr[u] == u) return u; return repr[u] = find(repr[u]); } bool unite(int u, int v) { u = find(u), v = find(v); if (u == v) return false; if (taille[u] < taille[v]) swap(u, v); repr[v] = u; taille[u] += taille[v]; return true; } }; struct Edge { int u, v, p; }; int nbNode, nbEdge, nbReq; vector<Edge> edges; vector<pii> reqs; signed main() { ios::sync_with_stdio(false); cin.tie(0); cin >> nbNode >> nbEdge; edges.resize(nbEdge); for (auto &[u,v,p] : edges) { cin >> u >> v >> p; --u, --v; p *= 2; } sort(all(edges), [&] (Edge a, Edge b) { return a.p < b.p; }); vector<int> times {0, (int)1e18}; rep(i, 0, nbEdge) rep(j, i+1, nbEdge) { // |a - x| = |b - x|, a < b // il faut a < x < b // x - a = b - x <-> 2x = a+b <-> x = (a+b)/2 times.push_back((edges[i].p + edges[j].p)/2); } sort(all(times)); times.resize(unique(all(times)) - begin(times)); auto getMst = [&] (int X) { sort(all(edges), [&] (Edge a, Edge b) { return abs(a.p-X) < abs(b.p-X); }); vector<int> taken; uf mst(nbNode); for (auto [u, v, p] : edges) { if (mst.unite(u,v)) taken.push_back(p); } return taken; }; cin >> nbReq; reqs.resize(nbReq); rep(iReq, 0, nbReq) { cin >> reqs[iReq].first; reqs[iReq].first *= 2; reqs[iReq].second = iReq; } sort(all(reqs)); vector<int> ans(nbReq); int ptr = 0; auto v1 = getMst(times[ptr]), v2 = getMst(times[ptr+1]); for (auto [X, iReq] : reqs) { while (X >= times[ptr+1]) { ++ptr; v1 = getMst(times[ptr]), v2 = getMst(times[ptr+1]); } int r1 = 0, r2 = 0; for (int p : v1) r1 += abs(p-X); for (int p : v2) r2 += abs(p-X); ans[iReq] = min(r1, r2); assert(ans[iReq] % 2 == 0); } rep(i, 0, nbReq) cout << ans[i]/2 << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...