#include <bits/stdc++.h>
#include <bits/extc++.h>
#define int long long
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
using namespace __gnu_pbds;
const int N = 2e5+5;
pair<int,int> bit[N];
int now[N];
pair<int,int> operator + (pair<int,int> a, pair<int,int> b){
return {a.first+b.first,a.second+b.second};
}
pair<int,int> operator - (pair<int,int> a, pair<int,int> b){
return {a.first-b.first,a.second-b.second};
}
void update(int pos, pair<int,int> val){
while(pos < N){
bit[pos] = bit[pos] + val;
pos += pos&-pos;
}
}
pair<int,int> query(int pos){
pair<int,int> res = {0,0};
while(pos){
res = res + bit[pos];
pos -= pos&-pos;
}
return res;
}
typedef tree<pair<int,int>,null_type,less<pair<int,int>>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
int t;
signed main(){
fastio
int k, n;
cin >> k >> n;
vector<int> tmp;
int ans = 1e18, anstmp = 0;
vector<pair<int,int>> v;
for(int i = 0;i < n;i++){
char a,b;
int x,y;
cin >> a >> x >> b >> y;
if(a == b) anstmp += abs(x-y);
else{
anstmp++;
tmp.push_back(x);
tmp.push_back(y);
v.push_back({x,y});
}
}
sort(tmp.begin(),tmp.end());
sort(v.begin(),v.end(),[&](auto a, auto b){
return a.first+a.second < b.first+b.second;
});
ordered_set s;
for(int i = 0;i < v.size();i++){
auto [x,y] = v[i];
s.insert({x,t++});
s.insert({y,t++});
update(lower_bound(tmp.begin(),tmp.end(),x)-tmp.begin()+1,{1,x});
update(lower_bound(tmp.begin(),tmp.end(),y)-tmp.begin()+1,{1,y});
int med = (*s.find_by_order(s.size()/2)).first;
pair<int,int> q1 = query(N-1)-query(lower_bound(tmp.begin(),tmp.end(),med)-tmp.begin()+1), q2 = query(lower_bound(tmp.begin(),tmp.end(),med)-tmp.begin());
now[i] = (q1.second-med*q1.first) + (med*q2.first-q2.second);
}
ans = min(ans,now[v.size()-1]);
s.clear();
fill(bit,bit+N,make_pair(0,0));
for(int i = v.size()-1;i >= 0;i--){
auto [x,y] = v[i];
s.insert({x,t++});
s.insert({y,t++});
update(lower_bound(tmp.begin(),tmp.end(),x)-tmp.begin()+1,{1,x});
update(lower_bound(tmp.begin(),tmp.end(),y)-tmp.begin()+1,{1,y});
int med = (*s.find_by_order(s.size()/2)).first;
pair<int,int> q1 = query(N-1)-query(lower_bound(tmp.begin(),tmp.end(),med)-tmp.begin()+1), q2 = query(lower_bound(tmp.begin(),tmp.end(),med)-tmp.begin());
if(i != 0) ans = min(ans,now[i-1]+(q1.second-med*q1.first) + (med*q2.first-q2.second));
else ans = min(ans,(q1.second-med*q1.first) + (med*q2.first-q2.second));
}
if(k==1){
cout << now[v.size()-1] + anstmp << "\n";
}else{
cout << ans+anstmp << "\n";
}
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:72:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for(int i = 0;i < v.size();i++){
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3404 KB |
Output is correct |
2 |
Correct |
2 ms |
3404 KB |
Output is correct |
3 |
Correct |
3 ms |
3532 KB |
Output is correct |
4 |
Correct |
4 ms |
3532 KB |
Output is correct |
5 |
Correct |
4 ms |
3532 KB |
Output is correct |
6 |
Correct |
3 ms |
3532 KB |
Output is correct |
7 |
Correct |
3 ms |
3532 KB |
Output is correct |
8 |
Correct |
3 ms |
3532 KB |
Output is correct |
9 |
Correct |
3 ms |
3496 KB |
Output is correct |
10 |
Correct |
3 ms |
3648 KB |
Output is correct |
11 |
Correct |
3 ms |
3532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3404 KB |
Output is correct |
2 |
Correct |
2 ms |
3404 KB |
Output is correct |
3 |
Correct |
3 ms |
3524 KB |
Output is correct |
4 |
Correct |
3 ms |
3532 KB |
Output is correct |
5 |
Correct |
5 ms |
3524 KB |
Output is correct |
6 |
Correct |
3 ms |
3532 KB |
Output is correct |
7 |
Correct |
3 ms |
3532 KB |
Output is correct |
8 |
Correct |
3 ms |
3532 KB |
Output is correct |
9 |
Correct |
4 ms |
3532 KB |
Output is correct |
10 |
Correct |
4 ms |
3532 KB |
Output is correct |
11 |
Correct |
3 ms |
3532 KB |
Output is correct |
12 |
Correct |
261 ms |
19944 KB |
Output is correct |
13 |
Correct |
529 ms |
19864 KB |
Output is correct |
14 |
Correct |
494 ms |
18212 KB |
Output is correct |
15 |
Correct |
260 ms |
13072 KB |
Output is correct |
16 |
Correct |
267 ms |
19844 KB |
Output is correct |
17 |
Correct |
265 ms |
19804 KB |
Output is correct |
18 |
Correct |
224 ms |
19884 KB |
Output is correct |
19 |
Correct |
302 ms |
19980 KB |
Output is correct |
20 |
Correct |
270 ms |
19996 KB |
Output is correct |
21 |
Correct |
274 ms |
19868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3404 KB |
Output is correct |
2 |
Correct |
1 ms |
3404 KB |
Output is correct |
3 |
Correct |
2 ms |
3404 KB |
Output is correct |
4 |
Correct |
2 ms |
3404 KB |
Output is correct |
5 |
Correct |
2 ms |
3404 KB |
Output is correct |
6 |
Correct |
2 ms |
3388 KB |
Output is correct |
7 |
Correct |
2 ms |
3404 KB |
Output is correct |
8 |
Correct |
2 ms |
3404 KB |
Output is correct |
9 |
Correct |
2 ms |
3404 KB |
Output is correct |
10 |
Correct |
2 ms |
3404 KB |
Output is correct |
11 |
Correct |
2 ms |
3404 KB |
Output is correct |
12 |
Correct |
2 ms |
3404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3404 KB |
Output is correct |
2 |
Correct |
2 ms |
3404 KB |
Output is correct |
3 |
Correct |
2 ms |
3404 KB |
Output is correct |
4 |
Correct |
3 ms |
3404 KB |
Output is correct |
5 |
Correct |
2 ms |
3404 KB |
Output is correct |
6 |
Correct |
2 ms |
3404 KB |
Output is correct |
7 |
Correct |
2 ms |
3388 KB |
Output is correct |
8 |
Correct |
2 ms |
3464 KB |
Output is correct |
9 |
Correct |
2 ms |
3404 KB |
Output is correct |
10 |
Correct |
2 ms |
3404 KB |
Output is correct |
11 |
Correct |
2 ms |
3404 KB |
Output is correct |
12 |
Correct |
2 ms |
3404 KB |
Output is correct |
13 |
Correct |
3 ms |
3532 KB |
Output is correct |
14 |
Correct |
3 ms |
3532 KB |
Output is correct |
15 |
Correct |
3 ms |
3532 KB |
Output is correct |
16 |
Correct |
2 ms |
3404 KB |
Output is correct |
17 |
Correct |
3 ms |
3532 KB |
Output is correct |
18 |
Correct |
2 ms |
3404 KB |
Output is correct |
19 |
Correct |
3 ms |
3532 KB |
Output is correct |
20 |
Correct |
3 ms |
3532 KB |
Output is correct |
21 |
Correct |
3 ms |
3532 KB |
Output is correct |
22 |
Correct |
3 ms |
3536 KB |
Output is correct |
23 |
Correct |
3 ms |
3532 KB |
Output is correct |
24 |
Correct |
3 ms |
3532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3404 KB |
Output is correct |
2 |
Correct |
2 ms |
3404 KB |
Output is correct |
3 |
Correct |
2 ms |
3404 KB |
Output is correct |
4 |
Correct |
2 ms |
3404 KB |
Output is correct |
5 |
Correct |
2 ms |
3404 KB |
Output is correct |
6 |
Correct |
2 ms |
3404 KB |
Output is correct |
7 |
Correct |
2 ms |
3388 KB |
Output is correct |
8 |
Correct |
2 ms |
3404 KB |
Output is correct |
9 |
Correct |
2 ms |
3392 KB |
Output is correct |
10 |
Correct |
2 ms |
3404 KB |
Output is correct |
11 |
Correct |
2 ms |
3392 KB |
Output is correct |
12 |
Correct |
2 ms |
3404 KB |
Output is correct |
13 |
Correct |
4 ms |
3532 KB |
Output is correct |
14 |
Correct |
4 ms |
3532 KB |
Output is correct |
15 |
Correct |
4 ms |
3568 KB |
Output is correct |
16 |
Correct |
2 ms |
3404 KB |
Output is correct |
17 |
Correct |
3 ms |
3532 KB |
Output is correct |
18 |
Correct |
2 ms |
3532 KB |
Output is correct |
19 |
Correct |
3 ms |
3528 KB |
Output is correct |
20 |
Correct |
3 ms |
3528 KB |
Output is correct |
21 |
Correct |
5 ms |
3532 KB |
Output is correct |
22 |
Correct |
4 ms |
3532 KB |
Output is correct |
23 |
Correct |
4 ms |
3532 KB |
Output is correct |
24 |
Correct |
5 ms |
3532 KB |
Output is correct |
25 |
Correct |
247 ms |
20672 KB |
Output is correct |
26 |
Correct |
393 ms |
20968 KB |
Output is correct |
27 |
Correct |
562 ms |
21624 KB |
Output is correct |
28 |
Correct |
503 ms |
22196 KB |
Output is correct |
29 |
Correct |
564 ms |
22172 KB |
Output is correct |
30 |
Correct |
205 ms |
13352 KB |
Output is correct |
31 |
Correct |
271 ms |
21424 KB |
Output is correct |
32 |
Correct |
268 ms |
22212 KB |
Output is correct |
33 |
Correct |
239 ms |
21776 KB |
Output is correct |
34 |
Correct |
268 ms |
22212 KB |
Output is correct |
35 |
Correct |
272 ms |
21660 KB |
Output is correct |
36 |
Correct |
270 ms |
21844 KB |
Output is correct |
37 |
Correct |
260 ms |
20708 KB |
Output is correct |