#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
#define med q1.top()
struct Median{
priority_queue<ll, vector<ll>, less<ll>> q1;
priority_queue<ll, vector<ll>, greater<ll>> q2;
ll sum1, sum2;
Median() : sum1(0), sum2(0) {};
void insert(ll x) {
if (!q1.empty() && x >= med) q2.push(x), sum2 += x;
else q1.push(x), sum1 += x;
if (q1.size() > q2.size()+1) {
q2.push(q1.top());
sum2 += q1.top();
sum1 -= q1.top();
q1.pop();
} else if (q1.size() < q2.size()) {
q1.push(q2.top());
sum1 += q2.top();
sum2 -= q2.top();
q2.pop();
}
}
ll get() {
return med * ll(q1.size()) - sum1 +
sum2 - med * q2.size();
}
};
int main()
{
int M, N;
scanf("%d %d", &M, &N);
char a,c;
ll b,d, ans0 = 0;
vector<pll> v;
for (int i = 0; i < N; i++) {
scanf(" %c %lld %c %lld",&a, &b,&c, &d);
if (d < b) swap(b,d);
if (a == c) ans0 += d - b, N--, i--;
else v.push_back(pll(b,d)), ans0++;
}
if (v.empty()) {
printf("%lld\n", ans0);
return 0;
}
if (M == 1) {
vector<ll> all;
for (pll p : v)
all.push_back(p.first), all.push_back(p.second);
sort(all.begin(), all.end());
ll x = all[all.size()/2 - 1];
for (ll y : all)
ans0 += abs(y - x);
printf("%lld\n", ans0);
return 0;
} else {
sort(v.begin(), v.end(), [](pll a, pll b){return a.first+a.second<b.first+b.second;});
vector<ll> ans(N, ans0);
Median *mm = new Median();
for (int i = 0; i < N; i++) {
mm->insert(v[i].first);
mm->insert(v[i].second);
ans[i] += mm->get();
}
mm = new Median();
for (int i = N-1; i > 0; i--) {
mm->insert(v[i].first);
mm->insert(v[i].second);
ans[i-1] += mm->get();
}
ll ans0 = ans[0];
for (int i = 1; i < N; i++) ans0 = min(ans0, ans[i]);
printf("%lld\n", ans0);
}
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:41:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &M, &N);
^
bridge.cpp:47:42: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf(" %c %lld %c %lld",&a, &b,&c, &d);
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2028 KB |
Output is correct |
2 |
Correct |
0 ms |
2028 KB |
Output is correct |
3 |
Correct |
0 ms |
2028 KB |
Output is correct |
4 |
Correct |
0 ms |
2028 KB |
Output is correct |
5 |
Correct |
0 ms |
2028 KB |
Output is correct |
6 |
Correct |
0 ms |
2028 KB |
Output is correct |
7 |
Correct |
0 ms |
2028 KB |
Output is correct |
8 |
Correct |
0 ms |
2028 KB |
Output is correct |
9 |
Correct |
0 ms |
2028 KB |
Output is correct |
10 |
Correct |
0 ms |
2028 KB |
Output is correct |
11 |
Correct |
0 ms |
2028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2028 KB |
Output is correct |
2 |
Correct |
0 ms |
2028 KB |
Output is correct |
3 |
Correct |
0 ms |
2028 KB |
Output is correct |
4 |
Correct |
0 ms |
2028 KB |
Output is correct |
5 |
Correct |
0 ms |
2028 KB |
Output is correct |
6 |
Correct |
0 ms |
2028 KB |
Output is correct |
7 |
Correct |
0 ms |
2028 KB |
Output is correct |
8 |
Correct |
0 ms |
2028 KB |
Output is correct |
9 |
Correct |
0 ms |
2028 KB |
Output is correct |
10 |
Correct |
0 ms |
2028 KB |
Output is correct |
11 |
Correct |
0 ms |
2028 KB |
Output is correct |
12 |
Correct |
29 ms |
8256 KB |
Output is correct |
13 |
Correct |
59 ms |
8256 KB |
Output is correct |
14 |
Correct |
49 ms |
8256 KB |
Output is correct |
15 |
Correct |
46 ms |
5184 KB |
Output is correct |
16 |
Correct |
66 ms |
8256 KB |
Output is correct |
17 |
Correct |
49 ms |
8256 KB |
Output is correct |
18 |
Correct |
46 ms |
8256 KB |
Output is correct |
19 |
Correct |
56 ms |
8256 KB |
Output is correct |
20 |
Correct |
36 ms |
8256 KB |
Output is correct |
21 |
Correct |
63 ms |
8256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2028 KB |
Output is correct |
2 |
Correct |
0 ms |
2028 KB |
Output is correct |
3 |
Correct |
0 ms |
2028 KB |
Output is correct |
4 |
Correct |
0 ms |
2028 KB |
Output is correct |
5 |
Correct |
0 ms |
2028 KB |
Output is correct |
6 |
Correct |
0 ms |
2028 KB |
Output is correct |
7 |
Correct |
0 ms |
2028 KB |
Output is correct |
8 |
Correct |
0 ms |
2028 KB |
Output is correct |
9 |
Correct |
0 ms |
2028 KB |
Output is correct |
10 |
Correct |
0 ms |
2028 KB |
Output is correct |
11 |
Correct |
0 ms |
2028 KB |
Output is correct |
12 |
Correct |
0 ms |
2028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2028 KB |
Output is correct |
2 |
Correct |
0 ms |
2028 KB |
Output is correct |
3 |
Correct |
0 ms |
2028 KB |
Output is correct |
4 |
Correct |
0 ms |
2028 KB |
Output is correct |
5 |
Correct |
0 ms |
2028 KB |
Output is correct |
6 |
Correct |
0 ms |
2028 KB |
Output is correct |
7 |
Correct |
0 ms |
2028 KB |
Output is correct |
8 |
Correct |
0 ms |
2028 KB |
Output is correct |
9 |
Correct |
0 ms |
2028 KB |
Output is correct |
10 |
Correct |
0 ms |
2028 KB |
Output is correct |
11 |
Correct |
0 ms |
2028 KB |
Output is correct |
12 |
Correct |
0 ms |
2028 KB |
Output is correct |
13 |
Correct |
0 ms |
2160 KB |
Output is correct |
14 |
Correct |
0 ms |
2160 KB |
Output is correct |
15 |
Correct |
0 ms |
2164 KB |
Output is correct |
16 |
Correct |
0 ms |
2028 KB |
Output is correct |
17 |
Correct |
0 ms |
2164 KB |
Output is correct |
18 |
Correct |
0 ms |
2028 KB |
Output is correct |
19 |
Correct |
0 ms |
2160 KB |
Output is correct |
20 |
Correct |
0 ms |
2160 KB |
Output is correct |
21 |
Correct |
0 ms |
2160 KB |
Output is correct |
22 |
Correct |
0 ms |
2160 KB |
Output is correct |
23 |
Correct |
0 ms |
2160 KB |
Output is correct |
24 |
Correct |
0 ms |
2160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2028 KB |
Output is correct |
2 |
Correct |
0 ms |
2028 KB |
Output is correct |
3 |
Correct |
0 ms |
2028 KB |
Output is correct |
4 |
Correct |
0 ms |
2028 KB |
Output is correct |
5 |
Correct |
0 ms |
2028 KB |
Output is correct |
6 |
Correct |
0 ms |
2028 KB |
Output is correct |
7 |
Correct |
0 ms |
2028 KB |
Output is correct |
8 |
Correct |
0 ms |
2028 KB |
Output is correct |
9 |
Correct |
0 ms |
2028 KB |
Output is correct |
10 |
Correct |
0 ms |
2028 KB |
Output is correct |
11 |
Correct |
0 ms |
2028 KB |
Output is correct |
12 |
Correct |
0 ms |
2028 KB |
Output is correct |
13 |
Correct |
0 ms |
2160 KB |
Output is correct |
14 |
Correct |
0 ms |
2160 KB |
Output is correct |
15 |
Correct |
0 ms |
2164 KB |
Output is correct |
16 |
Correct |
0 ms |
2028 KB |
Output is correct |
17 |
Correct |
0 ms |
2164 KB |
Output is correct |
18 |
Correct |
0 ms |
2028 KB |
Output is correct |
19 |
Correct |
0 ms |
2160 KB |
Output is correct |
20 |
Correct |
0 ms |
2160 KB |
Output is correct |
21 |
Correct |
0 ms |
2160 KB |
Output is correct |
22 |
Correct |
0 ms |
2160 KB |
Output is correct |
23 |
Correct |
0 ms |
2160 KB |
Output is correct |
24 |
Correct |
0 ms |
2160 KB |
Output is correct |
25 |
Correct |
39 ms |
10912 KB |
Output is correct |
26 |
Correct |
56 ms |
10560 KB |
Output is correct |
27 |
Correct |
79 ms |
9912 KB |
Output is correct |
28 |
Correct |
76 ms |
9780 KB |
Output is correct |
29 |
Correct |
103 ms |
9976 KB |
Output is correct |
30 |
Correct |
46 ms |
5956 KB |
Output is correct |
31 |
Correct |
59 ms |
10912 KB |
Output is correct |
32 |
Correct |
79 ms |
10912 KB |
Output is correct |
33 |
Correct |
76 ms |
10016 KB |
Output is correct |
34 |
Correct |
89 ms |
10912 KB |
Output is correct |
35 |
Correct |
53 ms |
9888 KB |
Output is correct |
36 |
Correct |
76 ms |
10912 KB |
Output is correct |
37 |
Correct |
43 ms |
10400 KB |
Output is correct |