이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// In the name of God
// Hope is last to die
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define pb push_back
// #define int long long
#define S second
#define F first
#define mp make_pair
#define smax(x, y) (x) = max((x), (y))
#define smin(x, y) (x) = min((x), (y))
#define all(x) (x).begin(), (x).end()
#define len(x) ((int)(x).size())
const int maxn = 1e5 + 5;
const int inf = 1e9 + 7;
ll k, n, sup, slow, ps[maxn];
vector<pii> a;
multiset<int> up, low;
bool cmp(pii x, pii y){
return x.F + x.S < y.F + y.S;
}
void reset(){
if(up.size() > low.size()){
int x = (*up.begin());
low.insert(x);
up.erase(up.find(x));
slow += x;
sup -= x;
}
if(low.size() > up.size() + 1){
int x = (*low.rbegin());
low.erase(low.find(x));
up.insert(x);
sup += x;
slow -= x;
}
}
void insert(int x){
int mid = (low.size() ? (*low.rbegin()) : inf);
if(x > mid){
up.insert(x);
sup += x;
}
else{
low.insert(x);
slow += x;
}
reset();
}
int32_t main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> k >> n;
ll sum = 0;
for(int i = 0; i < n; i++){
char h, g;
int u, v;
cin >> h >> u >> g >> v;
if(h == g) sum += abs(u - v);
else a.pb(mp(u, v));
}
sort(all(a), cmp);
n = a.size();
sum += n;
for(int i = 0; i < n; i++){
insert(a[i].S);
insert(a[i].F);
ps[i] = sup - slow;
}
ll ans = ps[n - 1] + sum;
if(k == 2){
low.clear();
up.clear();
slow = 0;
sup = 0;
for(int i = n - 1; i >= 1; i--){
insert(a[i].F);
insert(a[i].S);
smin(ans, sum + sup - slow + ps[i - 1]);
}
}
cout << ans << '\n';
return 0;
}
# | 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... |