이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define all(aa) aa.begin(), aa.end()
#define endl ("\n")
#define pb push_back
typedef long long ll;
const int maxn = 1e5 + 5;
using namespace std;
int n, k;
vector<pair<ll, ll>> v;
vector<ll> u;
multiset<ll> vis;
multiset<pair<ll, ll>> come; // vis r'ye gore artan come l'ye gore artan
ll lft, rgt, lst;
ll ans = LONG_LONG_MAX, cur;
int main(){
cin >> k >> n;
for(int i = 0; i < n; i++){
char c1, c2;
int a, b;
cin >> c1 >> a >> c2 >> b;
if(b > a) swap(a, b);
cur+=a-b;
if(c1!=c2){
++cur;
v.pb({b, a});
u.pb(a), u.pb(b);
come.insert({b, a});
cur+=2*b;
}
}
rgt = n = v.size();
sort(all(v));
sort(all(u));
ans = cur;
for(int i = 0; i < 2*n; i++){
cur += lft*2*(u[i] - lst) - rgt*2*(u[i] - lst);
if((*come.begin()).first == u[i]){
pair<ll, ll> p = *come.begin();
vis.insert({p.second});
come.erase(come.find(p));
rgt--;
}
if(*vis.begin() == u[i]){
lft++;
vis.erase(vis.find(u[i]));
}
ans = min(ans, cur);
lst = u[i];
}
cout << ans << endl;
}
| # | 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... |