Submission #922197

#TimeUsernameProblemLanguageResultExecution timeMemory
922197asdasdqwerPalembang Bridges (APIO15_bridge)C++14
22 / 100
77 ms7364 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,int)> insert=[&](int num1, int num2) { m1.insert(num1); m1.insert(num2); s1 += num1; s1 += num2; num1 = *m1.rbegin(); s1 -= num1; s2 += num1; m1.erase(m1.find(num1)); m2.insert(num1); }; vector<int> cst1; for (auto x:dif) { insert(x[0], 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; for (auto x:dif) { insert(x[0], 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...