이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "wiring.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define F first
#define S second
#define INF 1e18
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define OK cout<<"Ok"<<endl;
#define MOD (ll)(1e9+7)
ll dp[500][500];
long long min_total_length(vector<int> a, vector<int> b) {
set<int>st;
set<int>st2;
if(a.size()>b.size()) swap(a,b);
for(int v:a){
st2.insert(v);
}
int n=a.size(),m=b.size();
if(max(n,m)<=200){
for(int i=0;i<=n;i++){
for(int j=0;j<=m;j++){
dp[i][j]=INF;
}
}
dp[0][0]=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
dp[i][j]=abs(a[i-1]-b[j-1])+min(dp[i-1][j-1],min(dp[i][j-1],dp[i-1][j]));
//cout<<dp[i][j]<<' ';
}
//cout<<endl;
}
return dp[n][m];
}
for(int v:b){
st.insert(v);
}
ll ans=0;
for(int x:a){
auto l=st.upper_bound(x-1);
auto r=st.lower_bound(x);
if(l!=st.begin()&&r!=st.end()){
l--;
int y=*l,z=*r;
if(abs(y-x)<abs(z-x)){
ans+=abs(y-x);
st.erase(l);
}
else{
ans+=abs(z-x);
st.erase(r);
}
}
else if(l!=st.begin()){
l--;
int y=*l;
ans+=abs(y-x);
st.erase(l);
}
else if(r!=st.end()){
int y=*r;
ans+=abs(y-x);
st.erase(r);
}
}
for(int x:st){
auto l=st2.upper_bound(x-1);
auto r=st2.lower_bound(x);
if(l!=st2.begin()&&r!=st2.end()){
l--;
int y=*l,z=*r;
if(abs(y-x)<abs(z-x)){
ans+=abs(y-x);
}
else{
ans+=abs(z-x);
}
}
else if(l!=st2.begin()){
l--;
int y=*l;
ans+=abs(y-x);
}
else if(r!=st2.end()){
int y=*r;
ans+=abs(y-x);
}
}
return ans;
}
# | 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... |