이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "wiring.h"
#include <bits/stdc++.h>
using namespace std;
//macros
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, ll> lll;
typedef tuple<int, int, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<ll> vll;
typedef vector<lll> vlll;
#define REP(a,b,c) for(int a=int(b); a<int(c); a++)
#define RE(a,c) REP(a,0,c)
#define RE1(a,c) REP(a,1,c+1)
#define REI(a,b,c) REP(a,b,c+1)
#define REV(a,b,c) for(int a=int(c-1); a>=int(b); a--)
#define INF 1e18
#define pb push_back
#define fi first
#define se second
#define sz size()
const int MX = 6e5;
priority_queue<ii, vii, greater<ii>> pq;
int n[2], p[2], m=0;
vi f[2];
ll a[MX], b[MX], sm[MX];
ll dp[MX], ch[MX];
ll md[MX];
ll ans=0;
ll min_total_length(std::vector<int> R, std::vector<int> B) {
f[0] = R; f[1] = B;
RE(i,2) n[i] = f[i].sz;
// fill a and b
RE(i,2) RE(j,n[i]) pq.push({f[i][j], i});
while(!pq.empty()) {
a[m] = pq.top().fi;
b[m] = pq.top().se;
pq.pop(); m++;
}
ch[0]=0; md[0]=0; sm[0]=0;
REP(i,1,m) {
md[i] = md[i-1];
ch[i] = ch[i-1];
sm[i] = sm[i-1] + a[i-1];
if(b[i] != b[i-1]) ch[i]++, md[i]=i;
}
sm[m] = sm[m-1]+a[m-1];
dp[0] = 0;
REP(i,1,m+1) {
ll mn=INF;
dp[i] = INF;
REV(j,0,i) {
mn = min(mn, dp[j]);
ll bg = j, ed = i-1;
if(ch[ed]-ch[bg] == 0) continue;
if(ch[ed]-ch[bg] > 1) break;
ll mid = md[ed];
ll cost = 0;
cost += a[mid-1]*(mid-bg) - (sm[mid]-sm[bg]);
cost += (sm[ed+1]-sm[mid]) - a[mid]*(ed+1-mid);
cost += (a[mid]-a[mid-1])*max(mid-bg,ed-mid+1);
dp[i] = min(dp[i], mn + cost);
}
}
return dp[m];
}
# | 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... |