#include <stdio.h>
#include <algorithm>
#include <queue>
#include <vector>
#include <functional>
#define N 100002
#define INF 999999999999999999
#define min2(x,y) (x<y?x:y)
using namespace std;
int n, K;
long long dp1[N], dp2[N];
pair<int, int> in[N];
priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > Q1;
priority_queue<pair<int, int> > Q2;
bool cmp(pair<int, int> x, pair<int, int> y) {
return x.first + x.second < y.first + y.second;
}
int main() {
//freopen("input.txt", "r", stdin);
int k, x, y;
char in_a, in_b;
long long R = 0;
scanf("%d %d ", &K, &k);
while (k--) {
scanf("%c %d %c %d ", &in_a, &x, &in_b, &y);
if (x > y) x += y, y = x - y, x -= y;
R += (long long)(y - x);
if (in_a != in_b) R++, in[++n] = { x,y };
} sort(in + 1, in + n + 1, cmp);
k = 0, x = 0, y = 0;
for (int i = 1; i <= n; i++) {
dp1[i] = dp1[i - 1];
if (k < in[i].first) {
y++, dp1[i] += (long long)(in[i].first - k);
Q1.push({ in[i].first,1 }), Q1.push({ in[i].second,2 });
}
else if (in[i].second < k)
x++, dp1[i] += (long long)(k - in[i].second);
else Q1.push({ in[i].second,2 });
while (Q1.empty() == false && x < y) {
dp1[i] += (long long)(Q1.top().first - k)*(long long)(x - y);
if (Q1.top().second == 1) y--;
else x++;
k = Q1.top().first, Q1.pop();
}
}
k = in[n].second, x = 0, y = 0;
for (int i = n; i >= 1; i--) {
dp2[i] = dp2[i + 1];
if (in[i].second < k) {
x++, dp2[i] += (long long)(k - in[i].second);
Q2.push({ in[i].second,1 }), Q2.push({ in[i].first,2 });
}
else if (in[i].first > k)
y++, dp2[i] += (long long)(in[i].first - k);
else Q2.push({ in[i].first,2 });
while (Q2.empty() == false && x > y) {
dp2[i] += (long long)(k - Q2.top().first)*(long long)(y - x);
if (Q2.top().second == 1) x--;
else y++;
k = Q2.top().first, Q2.pop();
}
}
if (K == 1) printf("%lld", R + dp1[n] * 2);
else {
long long MIN = INF;
for (int i = 0; i <= n; i++) MIN = min2(MIN, dp1[i] + dp2[i + 1]);
printf("%lld", R + MIN * 2);
}
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:26:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d ", &K, &k);
~~~~~^~~~~~~~~~~~~~~~~~
bridge.cpp:28:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%c %d %c %d ", &in_a, &x, &in_b, &y);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
396 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
43 ms |
5484 KB |
Output is correct |
13 |
Correct |
85 ms |
7020 KB |
Output is correct |
14 |
Correct |
65 ms |
5740 KB |
Output is correct |
15 |
Correct |
57 ms |
4208 KB |
Output is correct |
16 |
Correct |
44 ms |
6380 KB |
Output is correct |
17 |
Correct |
70 ms |
7020 KB |
Output is correct |
18 |
Correct |
52 ms |
6636 KB |
Output is correct |
19 |
Correct |
79 ms |
7020 KB |
Output is correct |
20 |
Correct |
61 ms |
6636 KB |
Output is correct |
21 |
Correct |
74 ms |
6764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
368 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
44 ms |
5612 KB |
Output is correct |
26 |
Correct |
62 ms |
5740 KB |
Output is correct |
27 |
Correct |
78 ms |
6508 KB |
Output is correct |
28 |
Correct |
82 ms |
7020 KB |
Output is correct |
29 |
Correct |
82 ms |
7020 KB |
Output is correct |
30 |
Correct |
47 ms |
3824 KB |
Output is correct |
31 |
Correct |
46 ms |
6380 KB |
Output is correct |
32 |
Correct |
70 ms |
7020 KB |
Output is correct |
33 |
Correct |
53 ms |
6764 KB |
Output is correct |
34 |
Correct |
78 ms |
7056 KB |
Output is correct |
35 |
Correct |
60 ms |
6636 KB |
Output is correct |
36 |
Correct |
74 ms |
6764 KB |
Output is correct |
37 |
Correct |
42 ms |
5484 KB |
Output is correct |