Submission #922185

#TimeUsernameProblemLanguageResultExecution timeMemory
922185asdasdqwerPalembang Bridges (APIO15_bridge)C++14
63 / 100
2070 ms7376 KiB
#include <bits/stdc++.h> using namespace std; #define int int64_t #define pii array<int,2> struct cmp { bool operator()(const pii &x, const pii &y) { return (x[0] + x[1]) < (y[0] + y[1]); } }; signed main() { int k, n;cin>>k>>n; vector<pii> dif; int dis = 0; for (int i=0;i<n;i++) { char c1,c2; int p1,p2; cin>>c1>>p1>>c2>>p2; if (c1 == c2) { dis += abs(p1-p2); } else { dif.push_back({p1,p2}); } } if (dif.size() == 0) { cout<<dis<<"\n"; return 0; } if (k == 1 || dif.size() == 1) { vector<int> al; for (auto x:dif) { al.push_back(x[0]); al.push_back(x[1]); } sort(al.begin(),al.end()); int med = al[al.size() / 2]; for (int x:al) { dis += abs(x - med); } dis += dif.size(); cout<<dis<<"\n"; return 0; } sort(dif.begin(),dif.end(), cmp()); multiset<int> m1, m2; int s1 = 0, s2 = 0; function<void(int)> insert=[&](int num) { m1.insert(num); s1 += num; while ((m1.size() > m2.size() && ((m1.size() + m2.size()) % 2 == 0)) || (m1.size() - 1 > m2.size() && ((m1.size() + m2.size()) % 2 == 1))) { num = *m1.rbegin(); m1.erase(m1.find(num)); m2.insert(num); s1 -= num; s2 += num; } }; vector<int> cst1; // for (auto x:dif) { // insert(x[0]); // insert(x[1]); // int med = *m1.rbegin(); // cst1.push_back((((int)m1.size() * med) - s1) + (s2 - (((int)m2.size()) * med))); // } vector<int> al; for (auto x:dif) { al.push_back(x[0]); al.push_back(x[1]); sort(al.begin(),al.end()); int med = al[al.size()/2]; int tmp=0; for (int y:al) { tmp += abs(y-med); } cst1.push_back(tmp); } reverse(dif.begin(),dif.end()); vector<int> cst2; m1.clear();m2.clear(); s1=s2=0; al.clear(); // for (auto x:dif) { // insert(x[0]); // insert(x[1]); // int med = *m1.rbegin(); // // tmp2.push_back(med); // cst2.push_back((((int)m1.size() * med) - s1) + (s2 - (((int)m2.size()) * med))); // } for (auto x:dif) { al.push_back(x[0]); al.push_back(x[1]); sort(al.begin(),al.end()); int med = al[al.size()/2]; int tmp=0; for (int y:al) { tmp += abs(y-med); } cst2.push_back(tmp); } reverse(cst2.begin(),cst2.end()); // reverse(tmp2.begin(), tmp2.end()); int res = 1e17; for (int i=1;i<(int)cst1.size();i++) { res = min(res, cst1[i-1] + cst2[i]); } res += dif.size(); cout<<res+dis<<"\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...