# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
894502 | 2023-12-28T11:18:39 Z | salmon | Palembang Bridges (APIO15_bridge) | C++14 | 1 ms | 504 KB |
#include <bits/stdc++.h> using namespace std; int N; int K; char c,c1; vector<pair<long long int,pair<int,int>>> v; long long int h,h1; long long int sum = 0; long long int ans = 0; long long int big = 1e18; long long int lst[100100]; int main(){ scanf(" %d",&K); scanf(" %d",&N); set<pair<int,int>> s; map<int,int> mep; for(int i = 0; i < N; i++){ scanf(" %c",&c); scanf(" %lld",&h); scanf(" %c",&c1); scanf(" %lld",&h1); if(c == c1){ ans += abs(h - h1); } else{ v.push_back(make_pair(h + h1, make_pair(h,h1))); } } sort(v.begin(),v.end()); auto it = s.begin(); lst[0] = 0; for(int i = 0; i < v.size(); i++){ if(i == 0){ sum = abs(v[i].second.second - v[i].second.first); s.insert(make_pair(v[i].second.second, mep[v[i].second.second])); mep[v[i].second.second]++; s.insert(make_pair(v[i].second.first, mep[v[i].second.first])); mep[v[i].second.first]++; it = s.begin(); } else{ int flag = 0; pair<int,int> ii = make_pair(v[i].second.first, mep[v[i].second.first] ); mep[v[i].second.first]++; pair<int,int> ii1 = make_pair(v[i].second.second, mep[v[i].second.second]); mep[v[i].second.second]++; flag += (*it) < ii; flag += (*it) < ii1; int lum = 0; if(flag == 0){ s.insert(ii); s.insert(ii1); lum += it -> first; advance(it,-1); lum -= it -> first; sum += abs(it -> first - ii.first); sum += abs(it -> first - ii1.first); sum += abs(lum); } if(flag == 1){ s.insert(ii); s.insert(ii1); sum += abs(it -> first - ii.first); sum += abs(it -> first - ii1.first); } if(flag == 2){ s.insert(ii); s.insert(ii1); advance(it,-1); sum += abs(it -> first - ii.first); sum += abs(it -> first - ii1.first); } } lst[i + 1] = sum; } s.clear(); mep.clear(); big = sum + ans; for(int i = v.size() - 1; i >= 0; i--){ if(i == v.size() - 1){ sum = abs(v[i].second.second - v[i].second.first); s.insert(make_pair(v[i].second.second, mep[v[i].second.second])); mep[v[i].second.second]++; s.insert(make_pair(v[i].second.first, mep[v[i].second.first])); mep[v[i].second.first]++; it = s.begin(); } else{ int flag = 0; pair<int,int> ii = make_pair(v[i].second.first, mep[v[i].second.first] ); mep[v[i].second.first]++; pair<int,int> ii1 = make_pair(v[i].second.second, mep[v[i].second.second]); mep[v[i].second.second]++; flag += (*it) < ii; flag += (*it) < ii1; int lum = 0; if(flag == 0){ s.insert(ii); s.insert(ii1); lum += it -> first; advance(it,-1); lum -= it -> first; sum += abs(it -> first - ii.first); sum += abs(it -> first - ii1.first); sum += abs(lum); } if(flag == 1){ s.insert(ii); s.insert(ii1); sum += abs(it -> first - ii.first); sum += abs(it -> first - ii1.first); } if(flag == 2){ s.insert(ii); s.insert(ii1); advance(it,-1); sum += abs(it -> first - ii.first); sum += abs(it -> first - ii1.first); } } big = min(big, lst[i] + sum + ans); } if(K == 2) printf("%lld",big + v.size()); else printf("%lld", lst[v.size()] + ans + v.size()); } /* 1 5 B 0 A 4 B 1 B 3 A 5 B 7 B 2 A 6 B 1 A 7 */
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 344 KB | Output is correct |
2 | Correct | 0 ms | 348 KB | Output is correct |
3 | Incorrect | 1 ms | 344 KB | Output isn't correct |
4 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 344 KB | Output is correct |
2 | Correct | 0 ms | 344 KB | Output is correct |
3 | Incorrect | 1 ms | 348 KB | Output isn't correct |
4 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 504 KB | Output is correct |
2 | Correct | 0 ms | 348 KB | Output is correct |
3 | Incorrect | 1 ms | 348 KB | Output isn't correct |
4 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 348 KB | Output is correct |
2 | Correct | 0 ms | 348 KB | Output is correct |
3 | Incorrect | 1 ms | 348 KB | Output isn't correct |
4 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 348 KB | Output is correct |
2 | Correct | 0 ms | 348 KB | Output is correct |
3 | Incorrect | 0 ms | 348 KB | Output isn't correct |
4 | Halted | 0 ms | 0 KB | - |