#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
const ll maxn = 1e6 + 10, smalln = 2010;
struct work
{
ll x, y;
} w[maxn];
ll k, n;
ll pref[maxn][2];
bool cmp(work w1, work w2)
{
return w1.x + w1.y < w2.x + w2.y;
}
struct median
{
multiset < ll > lf, rf;
ll lf_sum, rf_sum;
median()
{
lf_sum = 0;
rf_sum = 0;
}
void add(ll x)
{
lf.insert(x);
if (!lf.empty() && !rf.empty())
{
if (*lf.rbegin() > *rf.begin())
{
ll s1 = *lf.rbegin(), s2 = *rf.begin();
lf_sum = lf_sum + s2 - s1;
rf_sum = rf_sum + s1 - s2;
lf.erase(lf.find(s1));
rf.erase(rf.find(s2));
lf.insert(s2);
rf.insert(s1);
}
}
lf_sum += x;
if (lf.size() - 1 > rf.size())
{
lf_sum -= *lf.rbegin();
rf_sum += *lf.rbegin();
rf.insert(*lf.rbegin());
lf.erase(lf.find(*lf.rbegin()));
///cout << lf_sum << " : " << rf_sum << " : " << x << endl;
}
}
ll calc()
{
return rf_sum - lf_sum;
}
};
void solve()
{
cin >> k >> n;
ll ans = 0;
vector < ll > v, sx;
for (ll i = 1; i <= n; i ++)
{
char c1, c2;
cin >> c1 >> w[i].x >> c2 >> w[i].y;
if (w[i].x > w[i].y)
swap(w[i].x, w[i].y);
if (c1 == c2)
{
ans = ans + w[i].y - w[i].x;
i --;
n --;
}
else
{
v.push_back(w[i].y);
v.push_back(w[i].x);
sx.push_back(w[i].x + w[i].y);
ans ++;
}
}
//for (ll i = 1; i <= n; i ++)
// cout << w[i].x << " - " << w[i].y << endl;
if (k == 1)
{
sort(v.begin(), v.end());
for (ll i = 0; i < v.size() / 2; i ++)
ans = ans - (v[i]);
for (ll i = v.size() / 2; i < v.size(); i ++)
ans = ans + v[i];
cout << ans << endl;
}
else
{
ll best = 1e18;
if (v.empty())
best = 0;
sort(w + 1, w + n + 1, cmp);
sort(v.begin(), v.end());
median go_right, go_left;
for (ll i = 1; i <= n; i ++)
{
go_right.add(w[i].x);
go_right.add(w[i].y);
pref[i][0] = go_right.calc();
}
for (ll i = n; i > 0; i --)
{
go_left.add(w[i].x);
go_left.add(w[i].y);
pref[i][1] = go_left.calc();
}
for (ll i = 0; i <= n; i ++)
{
best = min(best, pref[i][0] + pref[i + 1][1]);
}
cout << ans + best << endl;
}
}
int main()
{
solve();
return 0;
}
/**
2 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
*/
Compilation message
bridge.cpp: In function 'void solve()':
bridge.cpp:106:26: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
106 | for (ll i = 0; i < v.size() / 2; i ++)
| ~~^~~~~~~~~~~~~~
bridge.cpp:108:37: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
108 | for (ll i = v.size() / 2; i < v.size(); i ++)
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
2 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 |
2 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
2 ms |
360 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 |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
45 ms |
5176 KB |
Output is correct |
13 |
Correct |
99 ms |
5176 KB |
Output is correct |
14 |
Correct |
65 ms |
4800 KB |
Output is correct |
15 |
Correct |
60 ms |
3012 KB |
Output is correct |
16 |
Correct |
70 ms |
5204 KB |
Output is correct |
17 |
Correct |
87 ms |
5176 KB |
Output is correct |
18 |
Correct |
81 ms |
5176 KB |
Output is correct |
19 |
Correct |
97 ms |
5300 KB |
Output is correct |
20 |
Correct |
81 ms |
5412 KB |
Output is correct |
21 |
Correct |
91 ms |
5180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 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 |
1 ms |
340 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 |
340 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 |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 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 |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 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 |
468 KB |
Output is correct |
22 |
Correct |
3 ms |
468 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
468 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 |
2 ms |
468 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 |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
2 ms |
468 KB |
Output is correct |
25 |
Correct |
234 ms |
24500 KB |
Output is correct |
26 |
Correct |
305 ms |
25508 KB |
Output is correct |
27 |
Correct |
325 ms |
26248 KB |
Output is correct |
28 |
Correct |
345 ms |
26844 KB |
Output is correct |
29 |
Correct |
417 ms |
26876 KB |
Output is correct |
30 |
Correct |
156 ms |
14272 KB |
Output is correct |
31 |
Correct |
216 ms |
26120 KB |
Output is correct |
32 |
Correct |
313 ms |
26896 KB |
Output is correct |
33 |
Correct |
209 ms |
26488 KB |
Output is correct |
34 |
Correct |
331 ms |
26916 KB |
Output is correct |
35 |
Correct |
281 ms |
26456 KB |
Output is correct |
36 |
Correct |
329 ms |
26644 KB |
Output is correct |
37 |
Correct |
173 ms |
25240 KB |
Output is correct |