#include<bits/stdc++.h>
using namespace std;
#define taskname "A"
#define pb push_back
#define mp make_pair
#ifndef LOCAL
#define cerr if(0)cout
#endif
typedef long double ld;
typedef long long ll;
typedef pair<int,int> ii;
const int maxn = 1e5 + 5;
int k , n;
vector<ii> a;
namespace Median_heap{
ll lsum , hsum;
priority_queue<int,vector<int>,greater<int>> h;
priority_queue<int> l;
void add(int a , int b){
if(a > b)swap(a,b);
l.push(a);h.push(b);
lsum += a , hsum += b;
while(l.top() > h.top()){
int u = l.top();int v = h.top();
lsum += v - u;
hsum += u - v;
l.pop();h.pop();
l.push(v);
h.push(u);
}
}
ll Now(){
assert(l.size() == h.size());
// cout << hsum << " " << lsum << endl;
return hsum - lsum;
}
void clear(){
lsum = hsum = 0;
while(!l.empty()){
cerr << l.top() << endl;
l.pop();
}
while(!h.empty()){
cerr << h.top() << endl;
h.pop();
}
}
};
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
if(fopen(taskname".INP","r")){
freopen(taskname".INP", "r",stdin);
freopen(taskname".OUT", "w",stdout);
}
cin >> k >> n;
ll res = 0;
ll res1 = (ll)1e18;
for(int i = 1 ; i <= n ; ++i){
char side , side1;
int x, y;
cin >> side >> x >> side1 >> y;
if(x > y)swap(x,y);
if(side == side1){
res += y - x;
}else{
res++;
a.pb(mp(x,y));
}
}
if(a.size() == 0)return cout << res , 0;
sort(a.begin(),a.end(),[](const ii x , const ii y){
return x.first + x.second < y.first + y.second;
});
vector<ll> pre(a.size() + 2 , 0) , suf(a.size() + 2 , 0);
Median_heap::clear();
for(int i = 0 ; i < a.size() ; ++i){
Median_heap::add(a[i].first,a[i].second);
pre[i] = Median_heap::Now();
}
Median_heap::clear();
for(int i = a.size() - 1 ; i >= 0 ; --i){
Median_heap::add(a[i].first,a[i].second);
suf[i] = Median_heap::Now();
}
if(k == 2){
for(int i = 0 ; i < a.size() ; ++i){
res1 = min(res1 , pre[i] + suf[i + 1]);
}
}else res1 = pre[a.size() - 1];
assert(suf[0] == pre[a.size() -1]);
res += res1;
cout << res;
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:83:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0 ; i < a.size() ; ++i){
~~^~~~~~~~~~
bridge.cpp:93:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0 ; i < a.size() ; ++i){
~~^~~~~~~~~~
bridge.cpp:59:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(taskname".INP", "r",stdin);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
bridge.cpp:60:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(taskname".OUT", "w",stdout);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
34 ms |
3952 KB |
Output is correct |
13 |
Correct |
92 ms |
4076 KB |
Output is correct |
14 |
Correct |
74 ms |
3696 KB |
Output is correct |
15 |
Correct |
52 ms |
2556 KB |
Output is correct |
16 |
Correct |
33 ms |
3952 KB |
Output is correct |
17 |
Correct |
79 ms |
3952 KB |
Output is correct |
18 |
Correct |
47 ms |
3952 KB |
Output is correct |
19 |
Correct |
85 ms |
4048 KB |
Output is correct |
20 |
Correct |
55 ms |
3992 KB |
Output is correct |
21 |
Correct |
83 ms |
4100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
428 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
3 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
3 ms |
384 KB |
Output is correct |
21 |
Correct |
3 ms |
384 KB |
Output is correct |
22 |
Correct |
3 ms |
384 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
2 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
380 KB |
Output is correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Correct |
3 ms |
384 KB |
Output is correct |
24 |
Correct |
3 ms |
384 KB |
Output is correct |
25 |
Correct |
35 ms |
3960 KB |
Output is correct |
26 |
Correct |
67 ms |
3960 KB |
Output is correct |
27 |
Correct |
106 ms |
3952 KB |
Output is correct |
28 |
Correct |
89 ms |
4080 KB |
Output is correct |
29 |
Correct |
86 ms |
4080 KB |
Output is correct |
30 |
Correct |
45 ms |
2428 KB |
Output is correct |
31 |
Correct |
34 ms |
3952 KB |
Output is correct |
32 |
Correct |
78 ms |
3952 KB |
Output is correct |
33 |
Correct |
46 ms |
3952 KB |
Output is correct |
34 |
Correct |
85 ms |
4076 KB |
Output is correct |
35 |
Correct |
54 ms |
4080 KB |
Output is correct |
36 |
Correct |
84 ms |
4000 KB |
Output is correct |
37 |
Correct |
26 ms |
3928 KB |
Output is correct |