#include <bits/stdc++.h>
#define FAST_IO ios_base::sync_with_stdio(false); cin.tie(nullptr)
#define FOR(i, begin, end) for(int i=(begin); i<(end); i++)
#define pb push_back
#define s second
#define f first
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
const int N=1e5+10;
int k, n;
ll ans, p[N];
vector<ll> a;
vector<pii> inf;
map<int, int> in;
bool cmp(pii a, pii b) {
return (a.f+a.s)<(b.f+b.s);
}
ll s[4];
void fix(int in, multiset<int> &a, multiset<int> &b) {
int tot=a.size()+b.size();
while(a.size()>(tot+1)/2) {
int val=*a.rbegin();
a.erase(a.find(val));
s[in]-=val;
b.insert(val);
s[in+1]+=val;
}
while(b.size()>tot/2) {
int val=*b.begin();
b.erase(b.find(val));
s[in+1]-=val;
a.insert(val);
s[in]+=val;
}
}
ll calc(multiset<int> &a, multiset<int> &b, multiset<int> &c, multiset<int> &d) {
ll ret=0;
if(!a.empty()) {
ll md=*a.rbegin();
ret+=md*a.size()-s[0] + s[1]-md*b.size();
}
if(!c.empty()) {
ll md=*c.rbegin();
ret+=md*c.size()-s[2] + s[3]-md*d.size();
}
// cout << "calc: " << ret << endl;
return ret;
}
int main() {
FAST_IO;
cin >> k >> n;
FOR(i, 0, n) {
char c1, c2; int x1, x2;
cin >> c1 >> x1 >> c2 >> x2;
if(c1==c2) {
ans+=abs(x1-x2);
}
else {
inf.pb({x1,x2});
a.pb(x1);
a.pb(x2);
}
}
sort(a.begin(), a.end());
n=a.size();
ans+=n/2;
if(n==0) {
cout << ans;
return 0;
}
// if(k==1) {
// ll sm=0, cn=0;
// FORp(i, 0, n/2) sm+=a[i], cn++;
// ans+=a[n/2]*cn-sm;
//
// sm=0; cn=0;
// FOR(i, n/2+1, n) sm+=a[i], cn++;
// ans+=sm-a[n/2]*cn;
// cout << ans;
// return 0;
// }
multiset<int> l, r, le, ri;
sort(inf.begin(), inf.end(), cmp);
FOR(i, 0, inf.size()) {
le.insert(inf[i].f);
le.insert(inf[i].s);
s[2]+=inf[i].f+inf[i].s;
}
while(le.size()>(le.size()+ri.size()+1)/2) {
int val=*le.rbegin();
le.erase(le.find(val));
ri.insert(val);
s[2]-=val; s[3]+=val;
}
ll md=*ri.begin(), l_sz=le.size(), r_sz=ri.size(), now=md*l_sz-s[2]+s[3]-md*r_sz, ret=ans+now;
if(k==1) {
cout << ret;
return 0;
}
FOR(i, 0, inf.size()) {
int val=inf[i].f;
if(l.empty()) l.insert(val), s[0]+=val;
else {
int md=*l.rbegin();
if(val>=md) r.insert(val), s[1]+=val;
else l.insert(val), s[0]+=val;
}
if(le.count(val)) le.erase(le.find(val)), s[2]-=val;
else ri.erase(ri.find(val)), s[3]-=val;
val=inf[i].s;
if(l.empty()) l.insert(val), s[0]+=val;
else {
int md=*l.rbegin();
if(val>=md) r.insert(val), s[1]+=val;
else l.insert(val), s[0]+=val;
}
if(le.count(val)) le.erase(le.find(val)), s[2]-=val;
else ri.erase(ri.find(val)), s[3]-=val;
fix(0, l, r);
fix(2, le, ri);
ret=min(ret, ans+calc(l, r, le, ri));
// if(ret==22) {
// cout << "l: "; for(auto it : l) cout << it << " "; cout << endl;
// cout << "r: "; for(auto it : r) cout << it << " "; cout << endl;
// cout << "le: "; for(auto it : le) cout << it << " "; cout << endl;
// cout << "ri: "; for(auto it : ri) cout << it << " "; cout << endl;
// }
}
cout << ret;
}
Compilation message
bridge.cpp: In function 'void fix(int, std::multiset<int>&, std::multiset<int>&)':
bridge.cpp:27:19: warning: comparison of integer expressions of different signedness: 'std::multiset<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
27 | while(a.size()>(tot+1)/2) {
| ~~~~~~~~^~~~~~~~~~
bridge.cpp:34:19: warning: comparison of integer expressions of different signedness: 'std::multiset<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
34 | while(b.size()>tot/2) {
| ~~~~~~~~^~~~~~
bridge.cpp: In function 'int main()':
bridge.cpp:3:48: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
3 | #define FOR(i, begin, end) for(int i=(begin); i<(end); i++)
| ^
bridge.cpp:92:5: note: in expansion of macro 'FOR'
92 | FOR(i, 0, inf.size()) {
| ^~~
bridge.cpp:3:48: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
3 | #define FOR(i, begin, end) for(int i=(begin); i<(end); i++)
| ^
bridge.cpp:110:5: note: in expansion of macro 'FOR'
110 | FOR(i, 0, inf.size()) {
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
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 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
56 ms |
12196 KB |
Output is correct |
13 |
Correct |
83 ms |
12196 KB |
Output is correct |
14 |
Correct |
72 ms |
11000 KB |
Output is correct |
15 |
Correct |
45 ms |
7356 KB |
Output is correct |
16 |
Correct |
63 ms |
12044 KB |
Output is correct |
17 |
Correct |
55 ms |
12192 KB |
Output is correct |
18 |
Correct |
59 ms |
11964 KB |
Output is correct |
19 |
Correct |
63 ms |
11960 KB |
Output is correct |
20 |
Correct |
62 ms |
11960 KB |
Output is correct |
21 |
Correct |
69 ms |
11956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
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 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
3 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
2 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
2 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
3 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
2 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
2 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Execution timed out |
2090 ms |
12932 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |