This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
long long INF = 1e18;
const int maxn = 1e5+5;
vector<int> cp;
vector<pair<int, int>> vec;
bool cmp(pair<int, int> a, pair<int, int> b){
return a.first + a.second < b.first + b.second;
}
map<int, long long> mp[2];
long long pfx[maxn], sfx[maxn];
long long crr = -1, a, b;
void init(){
mp[0].clear();
mp[1].clear();
mp[0][-1] = 0;
crr = -1;
a = 0;
b = 0;
}
void upd_crr() {
while(a < 0){
crr = mp[0].upper_bound(crr)->first;
a += mp[0][crr];
b += mp[1][crr];
}
while(a > 0){
a -= mp[0][crr];
b -= mp[1][crr];
crr = prev(mp[0].lower_bound(crr))->first;
}
}
long long get_ans(){
long long re = crr*a + b;
auto it = mp[0].upper_bound(crr);
if(it != mp[0].end())re = min(re, it->first*(a+it->second) + mp[1][it->first] + b);
return re;
}
void add_new(int l, int r){
if(crr < l){
a--;
b += l;
}
if(crr >= r){
a++;
b -= r;
}
mp[0][l]++;
mp[0][r]++;
mp[1][l]-=l;
mp[1][r]-=r;
upd_crr();
}
int k, n;
int main(){
ios::sync_with_stdio(false);
cin >> k >> n;
long long ans = INF;
long long base = 0;
for(int i = 0; i < n; i++){
char t1, t2;
pair<int, int> p;
cin >> t1 >> p.first >> t2 >> p.second;
if(p.first > p.second)swap(p.first, p.second);
base += p.second - p.first;
if(t1 != t2) {
base++;
vec.push_back(p);
cp.push_back(p.first);
cp.push_back(p.second);
}
}
if(vec.empty()){
cout << base;
return 0;
}
sort(vec.begin(), vec.end(), cmp);
init();
for(int i = 0; i < vec.size(); i++){
add_new(vec[i].first, vec[i].second);
pfx[i] = get_ans();
}
init();
for(int i = vec.size()-1; i >= 0; i--){
add_new(vec[i].first, vec[i].second);
sfx[i] = get_ans();
if(i && k == 2)ans = min(ans, pfx[i-1] + sfx[i]);
else ans = min(ans, sfx[i]);
}
cout << base + ans*2;
}
Compilation message (stderr)
bridge.cpp: In function 'int main()':
bridge.cpp:91:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
91 | for(int i = 0; i < vec.size(); i++){
| ~~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |