이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//雪花飄飄北風嘯嘯
//天地一片蒼茫
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << " is " << x << endl
#define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
//change less to less_equal for non distinct pbds, but erase will bug
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
int n,k;
vector<ii> v;
ll val[100005];
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin.exceptions(ios::badbit | ios::failbit);
cin>>k>>n;
char ca,cb;
ll qa,qb;
ll same=0;
rep(x,0,n){
cin>>ca>>qa>>cb>>qb;
same+=abs(qa-qb);
if (ca!=cb) v.push_back(ii(min(qa,qb),max(qa,qb))),same++;
}
sort(all(v),[](ii i,ii j){
return i.fi+i.se<j.fi+j.se;
});
if (k==1){
ll delta=-sz(v);
ll curr=0;
priority_queue<ll,vector<ll>,greater<ll> > pq;
for (auto &it:v){
//cout<<it.fi<<" "<<it.se<<endl;
pq.push(it.fi);
pq.push(it.se);
curr+=it.fi;
}
ll ans=curr;
ll pos=0;
while (!pq.empty()){
ll temp=pq.top();
pq.pop();
curr+=delta*(temp-pos);
pos=temp;
delta++;
ans=min(ans,curr);
//cout<<curr<<endl;
}
cout<<ans*2+same<<endl;
}
else{
ll delta,curr,pos;
delta=0;
curr=0;
pos=0;
priority_queue<ll,vector<ll>,greater<ll> > pq;
rep(x,0,sz(v)){
if (pos<v[x].fi){
delta--;
pq.push(v[x].fi);
curr+=v[x].fi-pos;
}
pq.push(v[x].se);
if (delta<0){
ll temp=pq.top();
pq.pop();
curr+=delta*(temp-pos);
pos=temp;
delta++;
}
val[x+1]+=curr;
}
//rep(x,0,sz(v)+1) cout<<val[x]<<" "; cout<<endl;
delta=0;
curr=0;
pos=1e9+100;
priority_queue<ll> pq2;
rep(x,sz(v),0){
if (v[x].se<pos){
delta--;
pq2.push(v[x].se);
curr+=pos-v[x].se;
}
pq2.push(v[x].fi);
if (delta<0){
ll temp=pq2.top();
pq2.pop();
curr+=delta*(pos-temp);
pos=temp;
delta++;
}
val[x]+=curr;
}
//rep(x,0,sz(v)+1) cout<<val[x]<<" "; cout<<endl;
ll ans=1e18;
rep(x,0,sz(v)+1) ans=min(ans,val[x]);
cout<<ans*2+same<<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... |