#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define pb push_back
#define faster ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define pii pair<int, int>
const int MAX_N = 2e5+25;
ll n, k;
vector<pair<ll, pair<ll, ll>>> v;
vector<pair<ll, ll>> vtx;
ll sum = 0;
multiset<ll> a, b;
multiset<ll> c, d;
ll suma, sumb, sumc, sumd;
ll mini = 1e18;
void print() {
ll res = 0;
ll p1 = 0;
ll p2 = 0;
ll p3 = 0;
ll p4 = 0;
if(a.size() > 0)
p1 = ((*a.rbegin()) * a.size()) - suma;
if(b.size() > 0)
p2 = sumb - ((*a.rbegin()) * b.size());
if(c.size() > 0)
p3 += ((*c.rbegin()) * c.size()) - sumc;
if(d.size() > 0)
p4 += sumd - ((*c.rbegin()) * d.size());
res = p1 + p2 + p3 + p4;
mini = min(mini, res);
}
void handle(multiset<ll> & s, multiset<ll> & f, ll & sum1, ll & sum2) {
while(s.size() > f.size() && s.size() - f.size() >= 2) {
ll x = *s.rbegin();
sum1 -= x;
sum2 += x;
s.erase(s.find(x));
f.insert(x);
}
while(f.size() > s.size()) {
ll x = *f.begin();
sum1 += x;
sum2 -= x;
f.erase(f.begin());
s.insert(x);
}
}
void add(int y) {
if(a.size() == 0) {
a.insert(y);
suma += y;
}
else {
if(y > *a.rbegin()) {
sumb += y;
b.insert(y);
}
else {
suma += y;
a.insert(y);
}
}
if(y > *c.rbegin()) {
d.erase(d.find(y));
sumd -= y;
}
else {
sumc -= y;
c.erase(c.find(y));
}
handle(a, b, suma, sumb);
handle(c, d, sumc, sumd);
}
void solve() {
sort(v.begin(), v.end());
for(auto i : v) {
vtx.pb({i.second.first, i.second.second});
}
for(auto i : vtx) {
c.insert(i.first);
c.insert(i.second);
sumc += i.first;
sumc += i.second;
}
handle(c, d, sumc, sumd);
print();
for(int i = 0; i < vtx.size(); i++) {
add(vtx[i].first);
add(vtx[i].second);
print();
}
}
void input() {
cin >> k >> n;
if(k == 1) {
vector<int> cnt;
for(int i = 1; i <= n; i++) {
char a, b;
ll x, y;
cin >> a >> x >> b >> y;
if(a != b) {
sum++;
cnt.pb(x);
cnt.pb(y);
}
else {
sum += abs(x - y);
}
}
sort(cnt.begin(), cnt.end());
ll mid = cnt[(cnt.size() - 1) / 2];
for(auto i : cnt) {
sum += abs(i - mid);
}
cout << sum;
}
else {
for(int i = 1; i <= n; i++) {
char a, b;
ll x, y;
cin >> a >> x >> b >> y;
if(a != b)
v.pb({x + y, {x, y}});
else
sum += abs(x - y);
}
if(v.size() == 0)
cout << sum;
else {
solve();
cout << sum + mini + v.size();
}
}
}
int main() {
faster;
input();
}
Compilation message
bridge.cpp: In function 'void solve()':
bridge.cpp:98:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
98 | for(int i = 0; i < vtx.size(); i++) {
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
352 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
352 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
20 ms |
2132 KB |
Output is correct |
13 |
Correct |
40 ms |
3444 KB |
Output is correct |
14 |
Correct |
29 ms |
2520 KB |
Output is correct |
15 |
Correct |
27 ms |
2252 KB |
Output is correct |
16 |
Correct |
28 ms |
2844 KB |
Output is correct |
17 |
Correct |
27 ms |
3500 KB |
Output is correct |
18 |
Correct |
36 ms |
3088 KB |
Output is correct |
19 |
Correct |
39 ms |
3544 KB |
Output is correct |
20 |
Correct |
29 ms |
3020 KB |
Output is correct |
21 |
Correct |
35 ms |
3256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
2 ms |
452 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
3 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
456 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
2 ms |
468 KB |
Output is correct |
22 |
Correct |
2 ms |
468 KB |
Output is correct |
23 |
Correct |
3 ms |
360 KB |
Output is correct |
24 |
Correct |
3 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
316 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
324 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
460 KB |
Output is correct |
14 |
Correct |
2 ms |
460 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
316 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
324 KB |
Output is correct |
19 |
Correct |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
2 ms |
464 KB |
Output is correct |
22 |
Correct |
2 ms |
468 KB |
Output is correct |
23 |
Correct |
2 ms |
472 KB |
Output is correct |
24 |
Correct |
2 ms |
468 KB |
Output is correct |
25 |
Correct |
194 ms |
14360 KB |
Output is correct |
26 |
Correct |
225 ms |
14512 KB |
Output is correct |
27 |
Correct |
373 ms |
15256 KB |
Output is correct |
28 |
Correct |
443 ms |
15916 KB |
Output is correct |
29 |
Correct |
415 ms |
15928 KB |
Output is correct |
30 |
Correct |
128 ms |
8500 KB |
Output is correct |
31 |
Correct |
214 ms |
15184 KB |
Output is correct |
32 |
Correct |
219 ms |
15928 KB |
Output is correct |
33 |
Correct |
189 ms |
15496 KB |
Output is correct |
34 |
Correct |
199 ms |
15900 KB |
Output is correct |
35 |
Correct |
200 ms |
15400 KB |
Output is correct |
36 |
Correct |
195 ms |
15700 KB |
Output is correct |
37 |
Correct |
198 ms |
14368 KB |
Output is correct |