#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:25: 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:46: 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 |
0 ms |
3520 KB |
Output is correct |
2 |
Correct |
0 ms |
3520 KB |
Output is correct |
3 |
Correct |
0 ms |
3520 KB |
Output is correct |
4 |
Correct |
1 ms |
3520 KB |
Output is correct |
5 |
Correct |
1 ms |
3520 KB |
Output is correct |
6 |
Correct |
0 ms |
3520 KB |
Output is correct |
7 |
Correct |
1 ms |
3520 KB |
Output is correct |
8 |
Correct |
0 ms |
3520 KB |
Output is correct |
9 |
Correct |
0 ms |
3520 KB |
Output is correct |
10 |
Correct |
1 ms |
3520 KB |
Output is correct |
11 |
Correct |
1 ms |
3520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
3520 KB |
Output is correct |
2 |
Correct |
0 ms |
3520 KB |
Output is correct |
3 |
Correct |
0 ms |
3520 KB |
Output is correct |
4 |
Correct |
1 ms |
3520 KB |
Output is correct |
5 |
Correct |
1 ms |
3520 KB |
Output is correct |
6 |
Correct |
1 ms |
3520 KB |
Output is correct |
7 |
Correct |
1 ms |
3520 KB |
Output is correct |
8 |
Correct |
0 ms |
3520 KB |
Output is correct |
9 |
Correct |
1 ms |
3520 KB |
Output is correct |
10 |
Correct |
0 ms |
3520 KB |
Output is correct |
11 |
Correct |
1 ms |
3520 KB |
Output is correct |
12 |
Correct |
43 ms |
6676 KB |
Output is correct |
13 |
Correct |
76 ms |
6676 KB |
Output is correct |
14 |
Correct |
69 ms |
6676 KB |
Output is correct |
15 |
Correct |
49 ms |
5140 KB |
Output is correct |
16 |
Correct |
34 ms |
6676 KB |
Output is correct |
17 |
Correct |
71 ms |
6676 KB |
Output is correct |
18 |
Correct |
40 ms |
6676 KB |
Output is correct |
19 |
Correct |
85 ms |
6676 KB |
Output is correct |
20 |
Correct |
57 ms |
6676 KB |
Output is correct |
21 |
Correct |
81 ms |
6676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
3520 KB |
Output is correct |
2 |
Correct |
0 ms |
3520 KB |
Output is correct |
3 |
Correct |
0 ms |
3520 KB |
Output is correct |
4 |
Correct |
0 ms |
3520 KB |
Output is correct |
5 |
Correct |
0 ms |
3520 KB |
Output is correct |
6 |
Correct |
0 ms |
3520 KB |
Output is correct |
7 |
Correct |
0 ms |
3520 KB |
Output is correct |
8 |
Correct |
0 ms |
3520 KB |
Output is correct |
9 |
Correct |
0 ms |
3520 KB |
Output is correct |
10 |
Correct |
0 ms |
3520 KB |
Output is correct |
11 |
Correct |
0 ms |
3520 KB |
Output is correct |
12 |
Correct |
0 ms |
3520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
3520 KB |
Output is correct |
2 |
Correct |
0 ms |
3520 KB |
Output is correct |
3 |
Correct |
0 ms |
3520 KB |
Output is correct |
4 |
Correct |
0 ms |
3520 KB |
Output is correct |
5 |
Correct |
0 ms |
3520 KB |
Output is correct |
6 |
Correct |
0 ms |
3520 KB |
Output is correct |
7 |
Correct |
0 ms |
3520 KB |
Output is correct |
8 |
Correct |
0 ms |
3520 KB |
Output is correct |
9 |
Correct |
0 ms |
3520 KB |
Output is correct |
10 |
Correct |
0 ms |
3520 KB |
Output is correct |
11 |
Correct |
0 ms |
3520 KB |
Output is correct |
12 |
Correct |
0 ms |
3520 KB |
Output is correct |
13 |
Correct |
0 ms |
3520 KB |
Output is correct |
14 |
Correct |
1 ms |
3520 KB |
Output is correct |
15 |
Correct |
1 ms |
3520 KB |
Output is correct |
16 |
Correct |
0 ms |
3520 KB |
Output is correct |
17 |
Correct |
0 ms |
3520 KB |
Output is correct |
18 |
Correct |
0 ms |
3520 KB |
Output is correct |
19 |
Correct |
1 ms |
3520 KB |
Output is correct |
20 |
Correct |
1 ms |
3520 KB |
Output is correct |
21 |
Correct |
1 ms |
3520 KB |
Output is correct |
22 |
Correct |
1 ms |
3520 KB |
Output is correct |
23 |
Correct |
1 ms |
3520 KB |
Output is correct |
24 |
Correct |
1 ms |
3520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
3520 KB |
Output is correct |
2 |
Correct |
0 ms |
3520 KB |
Output is correct |
3 |
Correct |
0 ms |
3520 KB |
Output is correct |
4 |
Correct |
0 ms |
3520 KB |
Output is correct |
5 |
Correct |
0 ms |
3520 KB |
Output is correct |
6 |
Correct |
0 ms |
3520 KB |
Output is correct |
7 |
Correct |
0 ms |
3520 KB |
Output is correct |
8 |
Correct |
0 ms |
3520 KB |
Output is correct |
9 |
Correct |
0 ms |
3520 KB |
Output is correct |
10 |
Correct |
0 ms |
3520 KB |
Output is correct |
11 |
Correct |
0 ms |
3520 KB |
Output is correct |
12 |
Correct |
0 ms |
3520 KB |
Output is correct |
13 |
Correct |
0 ms |
3520 KB |
Output is correct |
14 |
Correct |
1 ms |
3520 KB |
Output is correct |
15 |
Correct |
1 ms |
3520 KB |
Output is correct |
16 |
Correct |
0 ms |
3520 KB |
Output is correct |
17 |
Correct |
0 ms |
3520 KB |
Output is correct |
18 |
Correct |
0 ms |
3520 KB |
Output is correct |
19 |
Correct |
1 ms |
3520 KB |
Output is correct |
20 |
Correct |
1 ms |
3520 KB |
Output is correct |
21 |
Correct |
0 ms |
3520 KB |
Output is correct |
22 |
Correct |
1 ms |
3520 KB |
Output is correct |
23 |
Correct |
1 ms |
3520 KB |
Output is correct |
24 |
Correct |
1 ms |
3520 KB |
Output is correct |
25 |
Correct |
46 ms |
6676 KB |
Output is correct |
26 |
Correct |
62 ms |
6676 KB |
Output is correct |
27 |
Correct |
81 ms |
6676 KB |
Output is correct |
28 |
Correct |
96 ms |
6676 KB |
Output is correct |
29 |
Correct |
81 ms |
6676 KB |
Output is correct |
30 |
Correct |
43 ms |
5140 KB |
Output is correct |
31 |
Correct |
36 ms |
6676 KB |
Output is correct |
32 |
Correct |
67 ms |
6676 KB |
Output is correct |
33 |
Correct |
36 ms |
6676 KB |
Output is correct |
34 |
Correct |
74 ms |
6676 KB |
Output is correct |
35 |
Correct |
76 ms |
6676 KB |
Output is correct |
36 |
Correct |
82 ms |
6676 KB |
Output is correct |
37 |
Correct |
43 ms |
6676 KB |
Output is correct |