#include "iostream"
#include "algorithm"
#include "vector"
#include "set"
#include "map"
#include "cstring"
#include "string"
#include "vector"
#include "cassert"
#include "queue"
#include "cstdio"
#include "cstdlib"
#include "ctime"
#include "cmath"
#include "bitset"
using namespace std;
typedef long long ll;
typedef pair < int, int > ii;
const int N = 1e5 + 5;
int n, k;
ll pre[N], suf[N];
class node{ public:
int size;
ll sum;
multiset < int > s;
void init() {
size = 0;
sum = 0;
s.clear();
}
int first() {
return *s.begin();
}
int second() {
return *--s.end();
}
int del(int x) {
s.erase(s.find(x));
size--;
sum -= x;
return x;
}
void add(int x) {
size++;
sum += x;
s.insert(x);
}
bool fit(int x) {
return s.size() and x >= *s.begin();
}
}s[2];
void update(int x) {
//printf("x = %d\n", x);
if(s[1].fit(x))
s[1].add(x);
else
s[0].add(x);
if(s[0].size > s[1].size + 1)
s[1].add(s[0].del(s[0].second()));
if(s[1].size > s[0].size)
s[0].add(s[1].del(s[1].first()));
}
ll get() {
int p = s[0].second();
//printf("p = %d\n", p);
return -s[0].sum + s[1].sum + (ll) (s[0].size - s[1].size) * p;
}
int main () {
scanf("%d %d", &k, &n);
ll add = 0;
vector < ii > v;
for(int i = 1; i <= n; i++) {
char c1, c2;
int x, y;
scanf(" %c %d %c %d", &c1, &x, &c2, &y);
if(c1 == c2) {
add += abs(x - y);
}
else {
if(x > y)
swap(x, y);
v.push_back({x, y});
}
}
sort(v.begin(), v.end(), [&](ii x, ii y){
return x.first + x.second < y.first + y.second;
});
n = v.size();
add += n;
for(int i = 1; i <= n; i++) {
int x = v[i - 1].first, y = v[i - 1].second;
update(x);
update(y);
pre[i] = get();
}
s[0].init();
s[1].init();
for(int i = n; i >= 1; i--) {
int x = v[i - 1].first, y = v[i - 1].second;
update(x);
update(y);
suf[i] = get();
}
// printf("pre[n] = %d add = %d\n", pre[n], add);
if(k == 1)
printf("%lld\n", pre[n] + add);
else {
ll ans = pre[n];
for(int i = 1; i < n; i++) {
//printf("pre[%d] = %d suf[%d] = %d\n", i, pre[i], i + 1, suf[i + 1]);
ans = min(ans, pre[i] + suf[i + 1]);
}
printf("%lld\n", ans + add);
}
return 0;
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:78:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &k, &n);
^
bridge.cpp:87:48: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf(" %c %d %c %d", &c1, &x, &c2, &y);
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
3588 KB |
Output is correct |
2 |
Correct |
0 ms |
3588 KB |
Output is correct |
3 |
Correct |
0 ms |
3720 KB |
Output is correct |
4 |
Correct |
0 ms |
3720 KB |
Output is correct |
5 |
Correct |
0 ms |
3720 KB |
Output is correct |
6 |
Correct |
0 ms |
3720 KB |
Output is correct |
7 |
Correct |
0 ms |
3720 KB |
Output is correct |
8 |
Correct |
0 ms |
3720 KB |
Output is correct |
9 |
Correct |
0 ms |
3720 KB |
Output is correct |
10 |
Correct |
0 ms |
3720 KB |
Output is correct |
11 |
Correct |
0 ms |
3720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
3588 KB |
Output is correct |
2 |
Correct |
0 ms |
3588 KB |
Output is correct |
3 |
Correct |
0 ms |
3720 KB |
Output is correct |
4 |
Correct |
0 ms |
3720 KB |
Output is correct |
5 |
Correct |
0 ms |
3720 KB |
Output is correct |
6 |
Correct |
0 ms |
3720 KB |
Output is correct |
7 |
Correct |
0 ms |
3720 KB |
Output is correct |
8 |
Correct |
0 ms |
3720 KB |
Output is correct |
9 |
Correct |
0 ms |
3720 KB |
Output is correct |
10 |
Correct |
0 ms |
3720 KB |
Output is correct |
11 |
Correct |
0 ms |
3720 KB |
Output is correct |
12 |
Correct |
183 ms |
14064 KB |
Output is correct |
13 |
Correct |
433 ms |
14064 KB |
Output is correct |
14 |
Correct |
353 ms |
13008 KB |
Output is correct |
15 |
Correct |
216 ms |
9592 KB |
Output is correct |
16 |
Correct |
159 ms |
14064 KB |
Output is correct |
17 |
Correct |
206 ms |
14064 KB |
Output is correct |
18 |
Correct |
129 ms |
14064 KB |
Output is correct |
19 |
Correct |
219 ms |
14064 KB |
Output is correct |
20 |
Correct |
196 ms |
14064 KB |
Output is correct |
21 |
Correct |
226 ms |
14064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
3588 KB |
Output is correct |
2 |
Correct |
0 ms |
3588 KB |
Output is correct |
3 |
Correct |
0 ms |
3588 KB |
Output is correct |
4 |
Correct |
0 ms |
3588 KB |
Output is correct |
5 |
Correct |
0 ms |
3588 KB |
Output is correct |
6 |
Correct |
0 ms |
3588 KB |
Output is correct |
7 |
Correct |
0 ms |
3588 KB |
Output is correct |
8 |
Correct |
0 ms |
3588 KB |
Output is correct |
9 |
Correct |
0 ms |
3588 KB |
Output is correct |
10 |
Correct |
0 ms |
3588 KB |
Output is correct |
11 |
Correct |
0 ms |
3588 KB |
Output is correct |
12 |
Correct |
0 ms |
3588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
3588 KB |
Output is correct |
2 |
Correct |
0 ms |
3588 KB |
Output is correct |
3 |
Correct |
0 ms |
3588 KB |
Output is correct |
4 |
Correct |
0 ms |
3588 KB |
Output is correct |
5 |
Correct |
0 ms |
3588 KB |
Output is correct |
6 |
Correct |
0 ms |
3588 KB |
Output is correct |
7 |
Correct |
0 ms |
3588 KB |
Output is correct |
8 |
Correct |
0 ms |
3588 KB |
Output is correct |
9 |
Correct |
0 ms |
3588 KB |
Output is correct |
10 |
Correct |
0 ms |
3588 KB |
Output is correct |
11 |
Correct |
0 ms |
3588 KB |
Output is correct |
12 |
Correct |
0 ms |
3588 KB |
Output is correct |
13 |
Correct |
0 ms |
3720 KB |
Output is correct |
14 |
Correct |
0 ms |
3720 KB |
Output is correct |
15 |
Correct |
0 ms |
3720 KB |
Output is correct |
16 |
Correct |
0 ms |
3588 KB |
Output is correct |
17 |
Correct |
0 ms |
3720 KB |
Output is correct |
18 |
Correct |
0 ms |
3588 KB |
Output is correct |
19 |
Correct |
0 ms |
3720 KB |
Output is correct |
20 |
Correct |
0 ms |
3720 KB |
Output is correct |
21 |
Correct |
0 ms |
3720 KB |
Output is correct |
22 |
Correct |
0 ms |
3720 KB |
Output is correct |
23 |
Correct |
0 ms |
3720 KB |
Output is correct |
24 |
Correct |
0 ms |
3720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
3588 KB |
Output is correct |
2 |
Correct |
0 ms |
3588 KB |
Output is correct |
3 |
Correct |
0 ms |
3588 KB |
Output is correct |
4 |
Correct |
0 ms |
3588 KB |
Output is correct |
5 |
Correct |
0 ms |
3588 KB |
Output is correct |
6 |
Correct |
0 ms |
3588 KB |
Output is correct |
7 |
Correct |
0 ms |
3588 KB |
Output is correct |
8 |
Correct |
0 ms |
3588 KB |
Output is correct |
9 |
Correct |
0 ms |
3588 KB |
Output is correct |
10 |
Correct |
0 ms |
3588 KB |
Output is correct |
11 |
Correct |
0 ms |
3588 KB |
Output is correct |
12 |
Correct |
0 ms |
3588 KB |
Output is correct |
13 |
Correct |
0 ms |
3720 KB |
Output is correct |
14 |
Correct |
0 ms |
3720 KB |
Output is correct |
15 |
Correct |
0 ms |
3720 KB |
Output is correct |
16 |
Correct |
0 ms |
3588 KB |
Output is correct |
17 |
Correct |
0 ms |
3720 KB |
Output is correct |
18 |
Correct |
0 ms |
3588 KB |
Output is correct |
19 |
Correct |
0 ms |
3720 KB |
Output is correct |
20 |
Correct |
0 ms |
3720 KB |
Output is correct |
21 |
Correct |
0 ms |
3720 KB |
Output is correct |
22 |
Correct |
0 ms |
3720 KB |
Output is correct |
23 |
Correct |
0 ms |
3720 KB |
Output is correct |
24 |
Correct |
0 ms |
3720 KB |
Output is correct |
25 |
Correct |
196 ms |
14064 KB |
Output is correct |
26 |
Correct |
276 ms |
14064 KB |
Output is correct |
27 |
Correct |
433 ms |
14064 KB |
Output is correct |
28 |
Correct |
449 ms |
14064 KB |
Output is correct |
29 |
Correct |
356 ms |
14064 KB |
Output is correct |
30 |
Correct |
233 ms |
9064 KB |
Output is correct |
31 |
Correct |
163 ms |
14064 KB |
Output is correct |
32 |
Correct |
216 ms |
14064 KB |
Output is correct |
33 |
Correct |
133 ms |
14064 KB |
Output is correct |
34 |
Correct |
209 ms |
14064 KB |
Output is correct |
35 |
Correct |
219 ms |
14064 KB |
Output is correct |
36 |
Correct |
239 ms |
14064 KB |
Output is correct |
37 |
Correct |
183 ms |
14064 KB |
Output is correct |