이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define fr(i, n, m) for(int i = (n); i < (m); i ++)
#define pb push_back
#define st first
#define nd second
#define pq priority_queue
#define all(x) begin(x), end(x)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const int i_inf = 1e9;
const ll inf = 4e9;
const ll mod = 1e9+7;
const ld eps = 1e-13;
const ld pi = 3.14159265359;
mt19937 _rand(time(NULL));
clock_t z;
const int &mxn = 2e5+5;
int n, k;
vector<pair<ll, ll> > v;
ll lef[mxn];
ll rig[mxn];
void solve(){
cin >> k >> n;
ll sum = 0;
fr(i, 0, n){
char c1, c2;
ll a, b;
cin >> c1 >> a >> c2 >> b;
if(c1 == c2){
sum += abs(a-b);
continue;
}
if(a > b) swap(a, b);
v.pb({a, b});
sum += b-a+1;
}
if(k == 1){
vector<pair<ll,int> > g;
for(auto u : v){
g.pb({u.st, 0});
g.pb({u.nd, 1});
}
sort(all(g));
int m = g.size();
ll temp_sum = 0;
int cnt = 0;
fr(i, 0, m){
ll pos = g[i].st;
lef[i] = pos*cnt - temp_sum;
if(g[i].nd == 1){
temp_sum += pos;
++cnt;
}
}
temp_sum = 0;
cnt = 0;
for(int i = m-1; i >= 0; i --){
ll pos = g[i].st;
rig[i] = temp_sum - pos*cnt;
if(g[i].nd == 0){
temp_sum += pos;
++cnt;
}
}
ll ans = inf;
fr(i, 0, m){
ans = min(ans, 2*(lef[i]+rig[i]) + sum);
}
cout<<ans<<endl;
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
# | 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... |