#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ii pair<int, int>
#define fi first
#define se second
#define debug(x) cout << #x << ": " << x << "\n"
#define all(x) x.begin(), x.end()
template<class T> bool umax(T &a, T b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool umin(T &a, T b) { if (a>b) { a=b; return 1; } return 0; }
void setIO(string name) {
ios_base::sync_with_stdio(0); cin.tie(0);
freopen((name+".inp").c_str(),"r",stdin);
freopen((name+".out").c_str(),"w",stdout);
}
bool cmp(ii &a, ii &b) { return a.fi+a.se<b.fi+b.se; }
struct med {
ll lef, rig;
priority_queue<int, vector<int>, greater<int>> rb;
priority_queue<int> lb;
void reset() {
while (lb.size()) lb.pop();
while (rb.size()) rb.pop();
lef=rig=0;
}
void insert(int x) {
if (lb.empty()) {
lb.push(x);
lef+=x;
return;
}
if (x<=lb.top()) lef+=x, lb.push(x);
else rig+=x, rb.push(x);
if (lb.size()-1>rb.size()) {
lef-=lb.top();
rig+=lb.top();
rb.push(lb.top());
lb.pop();
} else if (lb.size()<rb.size()) {
lef+=rb.top();
rig-=rb.top();
lb.push(rb.top());
rb.pop();
}
}
ll get() {
return rig-lef;
}
} med;
const int mxN=100100;
int n, k, N;
ll same_side, ans, pref[mxN];
vector<ii> v;
int main() {
cin >> k >> n;
for(int i=1; i<=n; ++i) {
char c, d; int x, y;
cin >> c >> x >> d >> y;
if (c==d) same_side+=abs(x-y);
else v.push_back({x, y});
}
N=v.size();
sort(all(v), cmp);
med.reset();
for(int i=1; i<=N; ++i) {
med.insert(v[i-1].fi);
med.insert(v[i-1].se);
pref[i]=med.get();
}
ans=pref[N];
if (k==2) {
med.reset();
for(int i=N; i>=1; --i) {
med.insert(v[i-1].fi);
med.insert(v[i-1].se);
umin(ans, pref[i-1]+med.get());
}
}
cout << same_side+N+ans;
}
Compilation message
bridge.cpp: In function 'void setIO(std::string)':
bridge.cpp:16:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
16 | freopen((name+".inp").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bridge.cpp:17:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
17 | freopen((name+".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
312 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
3 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
312 KB |
Output is correct |
9 |
Correct |
2 ms |
320 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 |
1 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 |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
324 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
57 ms |
4176 KB |
Output is correct |
13 |
Correct |
120 ms |
5516 KB |
Output is correct |
14 |
Correct |
78 ms |
4280 KB |
Output is correct |
15 |
Correct |
68 ms |
3492 KB |
Output is correct |
16 |
Correct |
85 ms |
4788 KB |
Output is correct |
17 |
Correct |
125 ms |
5388 KB |
Output is correct |
18 |
Correct |
95 ms |
4984 KB |
Output is correct |
19 |
Correct |
113 ms |
5436 KB |
Output is correct |
20 |
Correct |
89 ms |
5352 KB |
Output is correct |
21 |
Correct |
137 ms |
5116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
312 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
308 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 |
312 KB |
Output is correct |
8 |
Correct |
1 ms |
316 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
312 KB |
Output is correct |
3 |
Correct |
1 ms |
308 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 |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
304 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
312 KB |
Output is correct |
17 |
Correct |
1 ms |
316 KB |
Output is correct |
18 |
Correct |
1 ms |
316 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
324 KB |
Output is correct |
22 |
Correct |
3 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
308 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 |
312 KB |
Output is correct |
8 |
Correct |
1 ms |
304 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
312 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
312 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
344 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
25 |
Correct |
61 ms |
3780 KB |
Output is correct |
26 |
Correct |
104 ms |
4328 KB |
Output is correct |
27 |
Correct |
171 ms |
4996 KB |
Output is correct |
28 |
Correct |
154 ms |
5684 KB |
Output is correct |
29 |
Correct |
160 ms |
5368 KB |
Output is correct |
30 |
Correct |
82 ms |
3012 KB |
Output is correct |
31 |
Correct |
125 ms |
4676 KB |
Output is correct |
32 |
Correct |
131 ms |
5396 KB |
Output is correct |
33 |
Correct |
120 ms |
5376 KB |
Output is correct |
34 |
Correct |
140 ms |
5400 KB |
Output is correct |
35 |
Correct |
101 ms |
5376 KB |
Output is correct |
36 |
Correct |
166 ms |
5080 KB |
Output is correct |
37 |
Correct |
57 ms |
3828 KB |
Output is correct |