이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define forn(i,n) for(int i=0;i<n;i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(),v.rend()
#define pb push_back
#define sz(a) (int)a.size()
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define GR(a,n,m) vector<vector<int>> a(n, vector<int>(m, 0));
ll min_total_length(vector<int> a, vector<int> b)
{
if(sz(a) > sz(b))swap(a,b);
int n = sz(a), m = sz(b);
ll ret = LLONG_MAX;
if(max(n, m) <= 5000)
{
for(int start_poz = 0;start_poz + n - 1 < m;start_poz++){
int idx = start_poz;
ll ans = 0;
for(int i = start_poz;i < n;i++){
ans += abs(a[i] - b[i]);
idx++;
}
for(int i = 0;i < start_poz;i++){
int poz = lower_bound(all(a), b[i]) - a.begin();
ll mn = LLONG_MAX;
for(int j = max(poz - 5, 0);j < min(poz + 5, m);j++){
mn = min(mn, 1LL * abs(b[i] - a[j]));
}
ans += mn;
}
for(int i = idx;i < m;i++){
int poz = lower_bound(all(a), b[i]) - a.begin();
ll mn = LLONG_MAX;
for(int j = max(poz - 5, 0);j < min(poz + 5, m);j++){
mn = min(mn, 1LL * abs(b[i] - a[j]));
}
ans += mn;
}
ret = min(ret, ans);
}
return ret;
}
int idx = 0;
ll ans = 0;
for(int i = 0;i < n;i++){
ans += abs(a[i] - b[i]);
idx++;
}
for(int i = idx;i < m;i++){
int poz = lower_bound(all(a), b[i]) - a.begin();
ll mn = LLONG_MAX;
for(int j = max(poz - 5, 0);j < min(poz + 5, m);j++){
mn = min(mn, 1LL * abs(b[i] - a[j]));
}
ans += mn;
}
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... |