This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "wiring.h"
#include <bits/stdc++.h>
using namespace std;
//macros
typedef long long ll;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<ll> vll;
#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()
//===================//
// begin program //
//===================//
const int MX = 5e5;
vi pos, col;
bitset<MX> connected;
int n, m, N;
ll nxtC[MX];
ll dp[MX];
long long min_total_length(std::vector<int> r, std::vector<int> b) {
n = r.sz, m = b.sz;
N = n+m;
{
int i=0, j=0;
while(i != n || j != m) {
if(j == m || (i != n && r[i] < b[j])) {
pos.pb(r[i++]);
col.pb(0);
} else {
pos.pb(b[j++]);
col.pb(1);
}
}
}
dp[0] = 0;
int beg = -1, cent=-1, lastCol=-1;
ll regionMin = INF;
REP(i,1,N+1) {
if(col[i-1] != lastCol) {
lastCol = col[i-1];
beg = cent;
cent = i-1;
regionMin = dp[i-1];
}
dp[i] = INF;
ll mn = regionMin;
if(beg == -1) continue;
int y = i-1;
REV(x,max(beg,2*cent-y),cent) {
mn = min(mn, dp[x]);
ll cst = 0;
RE(i,cent-x) cst += pos[cent-1] - pos[x];
RE(i,y-cent+1) cst += pos[y-i] - pos[cent-1];
dp[i] = min(dp[i], mn+cst);
}
REV(x,beg,min(2*cent-y, cent)) {
mn = min(mn, dp[x]);
ll cst = 0;
RE(i,cent-x) cst += pos[cent] - pos[x+i];
RE(i,y-cent+1) cst += pos[y-i] - pos[cent];
dp[i] = min(dp[i], mn+cst);
}
regionMin = min(regionMin, dp[i]);
}
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... |