이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "wiring.h"
#include <bits/stdc++.h>
using namespace std;
//macros
typedef long long ll;
typedef pair<ll, ll> ii;
typedef pair<ll, ll> lll;
typedef tuple<ll, ll, ll> iii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<ll> vll;
typedef vector<lll> vlll;
#define REP(a,b,c) for(ll a=ll(b); a<ll(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(ll a=ll(c-1); a>=ll(b); a--)
#define INF 1e18
#define pb push_back
#define fi first
#define se second
const ll MX = 1e6;
vector<int> f[2];
ll N[2], n = 0;
ll a[MX], b[MX], sm[MX], ch[MX], CENT[MX];
ll dp[MX];
struct Seg {
void create(int size) {
sz = size;
SEG.assign(sz*4, 0);
LAZY.assign(sz*4, 0);
}
void add(int i, int j, ll v, ll lazy=0, int p=0, int l=0, int r=-1) {
if(r == -1) r=sz-1;
SEG[p] += lazy;
LAZY[p] += lazy;
if(j < l || i > r) return;
if(i <= l && j >= r) {
SEG[p] += v;
LAZY[p] += v;
return;
}
int m=(l+r)/2;
add(i,j,v,LAZY[p],p*2+1,l,m);
add(i,j,v,LAZY[p],p*2+2,m+1,r);
SEG[p] = min(SEG[p*2+1], SEG[p*2+2]);
LAZY[p] = 0;
}
ll get(int i, int j, ll lazy=0, int p=0, int l=0, int r=-1) {
if(r == -1) r=sz-1;
SEG[p] += lazy;
LAZY[p] += lazy;
if(j < l || i > r) return INF;
if(i <= l && j >= r) return SEG[p];
int m=(l+r)/2;
ll a=get(i,j,LAZY[p],p*2+1,l,m);
ll b=get(i,j,LAZY[p],p*2+2,m+1,r);
LAZY[p] = 0;
return min(a,b);
}
vi SEG, LAZY;
int sz;
};
Seg seg[2];
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].size();
// fill a and b
priority_queue<ii, vii, greater<ii>> pq;
RE(i,2) RE(j,N[i]) pq.push({f[i][j], i});
while(!pq.empty()) {
ii p = pq.top(); pq.pop();
a[n] = p.fi; b[n] = p.se;
n++;
}
sm[0] = 0; ch[0] = 0; CENT[0] = 0;
REP(i,1,n+1) {
ch[i] = ch[i-1];
sm[i] = sm[i-1]+a[i-1];
CENT[i] = CENT[i-1];
if(b[i] != b[i-1]) ch[i]++, CENT[i]=i;
}
RE(i,2) seg[i].create(n);
REP(i,1,n+1) {
dp[i] = INF;
ll ed = i-1;
ll cent = CENT[ed];
ll beg = CENT[max(0ll, cent-1)];
if(cent == ed) {
REP(j,beg,cent) RE(k,2) seg[k].add(beg,j,-a[j]);
REP(j,beg,cent) seg[0].add(j,j,-a[cent ]*j);
REP(j,beg,cent) seg[1].add(j,j,-a[cent-1]*j);
}
ll switchJ = 2*cent-ed-1;
dp[i] = min(dp[i], seg[0].get(beg,switchJ-1) + sm[ed+1]-sm[cent] + a[cent ]*switchJ);
dp[i] = min(dp[i], seg[1].get(switchJ,cent-1) + sm[ed+1]-sm[cent] + a[cent-1]*switchJ);
RE(j,2) seg[j].add(i,i,dp[i]);
if(dp[i] < dp[i-1]) RE(j,2) seg[j].add(i-1,i-1,dp[i] - dp[i-1]);
}
return dp[n];
}
# | 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... |