#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);
}
while(l.size() > h.size()){
int u = l.top();
l.pop();
h.push(u);
lsum += -u;
hsum += u;
}
while(l.size() < h.size()){
int u = h.top();
h.pop();
l.push(u);
lsum += u;
hsum += -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:97:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0 ; i < a.size() ; ++i){
~~^~~~~~~~~~
bridge.cpp:107:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0 ; i < a.size() ; ++i){
~~^~~~~~~~~~
bridge.cpp:73: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:74: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 |
1 ms |
256 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 |
3 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 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 |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
384 KB |
Output is correct |
12 |
Correct |
34 ms |
3952 KB |
Output is correct |
13 |
Correct |
87 ms |
4032 KB |
Output is correct |
14 |
Correct |
71 ms |
3700 KB |
Output is correct |
15 |
Correct |
50 ms |
2800 KB |
Output is correct |
16 |
Correct |
34 ms |
3952 KB |
Output is correct |
17 |
Correct |
79 ms |
3952 KB |
Output is correct |
18 |
Correct |
48 ms |
3960 KB |
Output is correct |
19 |
Correct |
86 ms |
3952 KB |
Output is correct |
20 |
Correct |
56 ms |
3952 KB |
Output is correct |
21 |
Correct |
86 ms |
3952 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 |
256 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 |
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 |
3 ms |
384 KB |
Output is correct |
15 |
Correct |
3 ms |
384 KB |
Output is correct |
16 |
Correct |
3 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 |
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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
15 |
Correct |
3 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 |
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 |
3 ms |
384 KB |
Output is correct |
25 |
Correct |
36 ms |
4080 KB |
Output is correct |
26 |
Correct |
65 ms |
4980 KB |
Output is correct |
27 |
Correct |
86 ms |
5744 KB |
Output is correct |
28 |
Correct |
92 ms |
6380 KB |
Output is correct |
29 |
Correct |
92 ms |
6256 KB |
Output is correct |
30 |
Correct |
47 ms |
3648 KB |
Output is correct |
31 |
Correct |
36 ms |
5616 KB |
Output is correct |
32 |
Correct |
89 ms |
6384 KB |
Output is correct |
33 |
Correct |
51 ms |
6000 KB |
Output is correct |
34 |
Correct |
86 ms |
6256 KB |
Output is correct |
35 |
Correct |
55 ms |
5872 KB |
Output is correct |
36 |
Correct |
84 ms |
6088 KB |
Output is correct |
37 |
Correct |
27 ms |
4728 KB |
Output is correct |