This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
PROG: ffff
LANG: C++11
_____
.' '.
/ 0 0 \
| ^ |
| \ / |
\ '---' /
'._____.'
*/
#include <bits/stdc++.h>
using namespace std;
template<class T>
void readi(T &x)
{
T input = 0;
bool negative = false;
char c = ' ';
while (c < '-')
{
c = getchar();
}
if (c == '-')
{
negative = true;
c = getchar();
}
while (c >= '0')
{
input = input * 10 + (c - '0');
c = getchar();
}
if (negative)
{
input = -input;
}
x = input;
}
template<class T>
void printi(T output)
{
if (output == 0)
{
putchar('0');
return;
}
if (output < 0)
{
putchar('-');
output = -output;
}
int aout[20];
int ilen = 0;
while(output)
{
aout[ilen] = ((output % 10));
output /= 10;
ilen++;
}
for (int i = ilen - 1; i >= 0; i--)
{
putchar(aout[i] + '0');
}
return;
}
template<class T>
void ckmin(T &a, T b)
{
a = min(a, b);
}
template<class T>
void ckmax(T &a, T b)
{
a = max(a, b);
}
long long randomize(long long mod)
{
return ((1ll << 30) * rand() + (1ll << 15) * rand() + rand()) % mod;
}
#define MP make_pair
#define PB push_back
#define PF push_front
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
const long double PI = 4.0 * atan(1.0);
const long double EPS = 1e-20;
#define MAGIC 347
#define SINF 10007
#define CO 1000007
#define INF 1000000007
#define BIG 1000000931
#define LARGE 1696969696967ll
#define GIANT 2564008813937411ll
#define LLINF 2696969696969696969ll
#define MAXN 200013
long long normalize(long long x, long long mod = INF)
{
return (((x % mod) + mod) % mod);
}
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
int N, M, K = -1;
vector<ll> A, B;
vector<pll> pts;
vector<ll> group[MAXN];
vector<ll> psum[MAXN];
ll dp[2][MAXN];
int pref[MAXN];
ll rsum(int idx, int lt, int rt)
{
return psum[idx][rt + 1] - psum[idx][lt];
}
ll cost(int idx, int a, int b)
{
if (a == group[idx].size())
{
if (b < 0) return 0ll;
a--;
}
if (b < 0) b++;
ll res = 0;
res += rsum(idx + 1, 0, b);
res -= rsum(idx, a, group[idx].size() - 1);
ll na = group[idx].size() - a;
if (na >= b + 1)
{
res += (na - (b + 1)) * group[idx + 1][0];
}
else
{
res -= ((b + 1) - na) * group[idx].back();
}
return res;
}
void solve(int i, int L, int R, int optl, int optr)
{
if (L > R)
{
return;
}
int mid = (L + R)/2, opt = -1;
for (int j = optl; j <= optr; j++)
{
if (dp[0][j] + cost(i, j, mid - 1) < dp[1][mid])
{
dp[1][mid] = dp[0][j] + cost(i, j, mid - 1);
opt = j;
}
}
solve(i, L, mid - 1, opt, optr);
solve(i, mid + 1, R, optl, opt);
}
ll min_total_length(vector<int> a, vector<int> b)
{
N = a.size(), M = b.size();
if (N == 0 || M == 0) return LLINF;
for (int i = 0; i < N; i++)
{
A.PB(a[i]);
pts.PB(MP(a[i], 0));
}
for (int i = 0; i < M; i++)
{
B.PB(b[i]);
pts.PB(MP(b[i], 1));
}
sort(pts.begin(), pts.end());
for (int i = 0; i < N + M; i++)
{
if (i == 0 || pts[i].se != pts[i - 1].se)
{
K++;
}
group[K].PB(pts[i].fi);
}
K++;
for (int i = 0; i < K; i++)
{
psum[i].resize(group[i].size() + 1);
for (int j = 0; j < group[i].size(); j++)
{
psum[i][j + 1] = psum[i][j] + group[i][j];
}
}
pref[0] = 0;
for (int i = 0; i < K; i++)
{
pref[i + 1] = pref[i] + group[i].size();
}
// cerr << cost(2, 0, 0) << endl;
// for (int i = 0; i < K; i++)
// {
// cout << "group " << i << ":";
// for (int x : group[i])
// {
// cout << ' ' << x;
// }
// cout << '\n';
// }
for (int i = 0; i <= N + M; i++)
{
dp[0][i] = LLINF;
dp[1][i] = LLINF;
}
dp[0][0] = 0;
for (int i = 0; i < K - 1; i++)
{
// for (int j = 0; j <= group[i].size(); j++)
// {
// cerr << dp[0][j] << ' ';
// }
// cerr << endl;
// cerr << i << endl;
solve(i, 0, group[i + 1].size(), 0, group[i].size());
// for (int j = 0; j <= group[i].size(); j++)
// {
// //you already have 0...j-1 covered
// for (int k = 0; k <= group[i + 1].size(); k++)
// {
// ckmin(dp[1][k], dp[0][j] + cost(i, j, k - 1));
// continue;
// }
// }
for (int j = 0; j <= group[i].size(); j++)
{
dp[0][j] = LLINF;
}
for (int j = 0; j <= group[i + 1].size(); j++)
{
dp[0][j] = dp[1][j];
dp[1][j] = LLINF;
}
}
// for (int i = 0; i <= K; i++)
// {
// cerr << pref[i] << endl;
// }
return dp[0][group[K - 1].size()];
}
Compilation message (stderr)
wiring.cpp: In function 'll cost(int, int, int)':
wiring.cpp:129:8: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (a == group[idx].size())
~~^~~~~~~~~~~~~~~~~~~~
wiring.cpp: In function 'll min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:194:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < group[i].size(); j++)
~~^~~~~~~~~~~~~~~~~
wiring.cpp:238:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j <= group[i].size(); j++)
~~^~~~~~~~~~~~~~~~~~
wiring.cpp:242:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j <= group[i + 1].size(); j++)
~~^~~~~~~~~~~~~~~~~~~~~~
# | 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... |