이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ff first
#define ss second
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define N 100005
const ll MOD = 1000000007;
const int INF = 0x3f3f3f3f;
vector<pair<int, int> > v;
ll tmp;
multiset<int>::iterator it;
multiset<int> in;
ll sum, all;
ll pr[N], su[N];
int n;
void wtf(ll* ans){
all = sum = 0;
in.clear();
in.insert(-1);
it = in.begin();
int i = 1;
for(auto& x : v){
all += x.ff;
in.insert(x.ff);
in.insert(x.ss);
if(x.ss < *it){
sum += x.ff + x.ss - (*it);
--it;
} else if(x.ff >= *it){
++it;
sum += *it;
} else {
sum += x.ff;
}
ans[i] = all - sum;
++i;
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int k;
cin>>k>>n;
while(n--){
char c, d;
int a, b;
cin>>c>>a>>d>>b;
tmp += abs(a - b);
if(c == d) continue;
++tmp;
if(a > b) swap(a, b);
v.push_back({a, b});
}
sort(v.begin(), v.end(), [](const pair<int, int>& lhs, const pair<int, int>& rhs){
return lhs.ff + lhs.ss < rhs.ff + rhs.ss;
});
n = v.size();
wtf(pr);
reverse(v.begin(), v.end());
wtf(su);
if(k == 1){
cout<<pr[n] * 2 + tmp;
} else {
ll mn = pr[n];
for(int i = 0; i <= n; ++i){
mn = min(pr[i] + su[n - i], mn);
}
cout<<mn * 2 + tmp;
}
}
# | 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... |