제출 #135900

#제출 시각아이디문제언어결과실행 시간메모리
135900KastandaRoller Coaster Railroad (IOI16_railroad)C++11
100 / 100
231 ms25200 KiB
// ItnoE #include<bits/stdc++.h> #define x first #define y second using namespace std; typedef long long ll; const int N = 200005; int P[N]; int Find(int v) { return (P[v] < 0 ? v : (P[v] = Find(P[v]))); } inline bool Merge(int v, int u) { v = Find(v); u = Find(u); if (v == u) return 0; P[v] = u; return 1; } int64_t plan_roller_coaster(vector < int > S, vector < int > T) { int n = (int)S.size(); vector < pair < int , pair < int , int > > > A, E; for (int i = 0; i < n; i ++) { A.push_back({S[i], {1, i}}); A.push_back({T[i], {-1, i}}); } A.push_back({(int)1e9, {1, n}}); A.push_back({1, {-1, n ++}}); sort(A.begin(), A.end()); int SM = 0; ll tot = 0; memset(P, -1, sizeof(P)); for (int i = 0; i + 1 < (int)A.size(); i ++) { SM += A[i].y.x; tot += max(SM, 0) * (ll)(A[i + 1].x - A[i].x); E.push_back({A[i + 1].x - A[i].x, {A[i].y.y, A[i + 1].y.y}}); if (A[i].x == A[i + 1].x || SM) Merge(A[i].y.y, A[i + 1].y.y); } sort(E.begin(), E.end()); for (auto e : E) if (Merge(e.y.x, e.y.y)) tot += e.x; return (tot); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...