[Fortuna OJ] Feb 25th – Group A Problem-Solving Report
A - Tourist Problem
A really fun math problem; let's analyze it. Original notes: https://comfortablecom.wordpress.com/2017/10/06/jzoj-b%E7%BB%84-10-6-%E6%80%BB%E7%BB%93/
First, let the answer for one case be \(S_x\). Obviously, it can be written as:
\[ S_x = |a_{x_1}-0|+|a_{x_2}-a_{x_1}|+|a_{x_3}-a_{x_2}|+|a_{x_4}-a_{x_3}|+\dots +|a_{x_n}-a_{x_{n-1}}| \]
This long string of an answer is really just a combination of minuends and subtrahends. We find that almost every number plays the role of both a minuend and a subtrahend in this expression, except the last element, namely \(a_{x_n}\). This element only acts as a minuend once, and that's it.
Let's consider the contribution of \(i\) to the answer. First, as a minuend, \(i\) has \(n!\) possibilities in total, of which some are missing a subtrahend; this missing part is the number of possibilities with \(i\) as \(a_{x_n}\), that is, \((n-1)!\). Next, consider the contribution of \(i\) within one sequence. When \(j<i\), counting \(0\), there are \(i\) numbers in total that let \(i\) subtract the subtrahend without changing sign; but when \(i<j\), things are different: there are \(n-i\) kinds of numbers that make \(i\) contribute to the answer with its sign flipped. That part of the analysis is done; summed up as one expression:
\[ a_i(n-1)!i+(-a_i)(n-1)!(n-i) \]
Put this together with the counts of possible arrangements and the case analysis I gave above, and please, reader, take it to heart and work it out.
As for being a subtrahend, it's really just old wine in a new bottle. You only need to swap the positive and negative relationships; the formula is:
\[ (-a_i)(n-1)!(n-i)+a_i(n-1)!(i-1) \]
Note that the \(i-1\) in the last term of this expression comes from \(0\) being unable to serve as its minuend. It's a tiny little bit asymmetric, I guess.
Code
// A.cpp
#include <bits/stdc++.h>
#define ll unsigned long long
using namespace std;
const int MAX_N = 100200;
int n, arr[MAX_N];
ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &arr[i]);
sort(arr + 1, arr + 1 + n);
ll x = 0;
for (int i = 1; i <= n; i++)
x = x + 1LL * arr[i] * (1LL * 4 * i - 1LL * 2 * n - 1);
ll d = gcd(x, n);
printf("%lld %lld", x / d, n / d);
return 0;
}
B - Work Arrangement (Work)
On this problem I had actually derived even the slope formula during the contest, but I was too lazy and didn't know how to transition across a gap of \(k\), so I gave up on it, and in the end I forgot to check the boundaries and didn't even get the 50 points for the st*pid brute force.
The DP equation for this problem can be obtained by the one-glance method; it requires sorting first:
\[ dp[i] = min\{ f[j-1]+C+(f[i]-f[j])^2 \}, j-1 \in [0,i-k] \]
Next, let's think about how to apply the convex hull trick. The convex hull trick is a very commonly used DP optimization technique, and I suggest learning it in depth. Let \(k\) be the optimal solution we want; then it must satisfy:
\[ \begin{gathered} f[k-1]+C+(f[i]-f[k])^2 < f[j-1]+C+(f[i]-f[j]))^2, j \in [1, i-k+1], j \neq k \\ (f[k-1]-f[j-1])+(f[k]^2-f[j]^2) < 2f[i] (f[k]-f[j]) \end{gathered} \]
When comparing, mind how the signs affect the inequality.
Code
// B.cpp
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX_N = 2e6 + 20000;
const ll INF = (0x3f3f3f3f3f3f3f3f);
ll f[MAX_N], arr[MAX_N], n, k, c, q[MAX_N << 2];
int head = 1, tail = 0;
ll pow(ll num) { return num * num; }
double check(ll a, ll b)
{
return ((f[b - 1] + pow(arr[b])) - (f[a - 1] + pow(arr[a]))) / (2.0 * (arr[b] - arr[a]));
}
void insert(int x)
{
while (head <= tail && arr[q[tail]] == arr[x])
if (f[x - 1] < f[q[tail] - 1])
tail--;
else
return;
while (head < tail && check(q[tail - 1], q[tail]) > check(q[tail], x))
tail--;
q[++tail] = x;
}
int main()
{
freopen("work1.in", "r", stdin);
scanf("%lld%lld%lld", &n, &k, &c);
for (int i = 1; i <= n; i++)
scanf("%lld", &arr[i]);
f[0] = 0;
sort(arr + 1, arr + 1 + n);
for (int i = 1; i < k; i++)
f[i] = INF;
for (int i = k; i <= n; i++)
{
int x = i - k + 1;
insert(x);
while (head < tail && check(q[head], q[head + 1]) < arr[i])
head++;
f[i] = f[q[head] - 1] + c + pow(arr[q[head]] - arr[i]);
}
printf("%lld", f[n]);
return 0;
}
C - Ah Q's Parking Lot (Park)
A segment tree problem (while upsolving it I nearly made up my mind to spend the whole night writing a Treap version, but in the end found a segment tree was more than enough). We maintain the left extreme point, the right extreme point, the length of the longest empty stretch, and the best parking spot. When updating the segment tree, just compare the left interval, the merged interval, and the right interval.
Code
// C.cpp
#include <bits/stdc++.h>
#define mid ((l + r) >> 1)
#define lson (p << 1)
#define rson ((p << 1) | 1)
using namespace std;
const int MAX_N = 200200, INF = 0x3f3f3f3f;
int n, m, tree_l[MAX_N << 2], tree_r[MAX_N << 2], tree_len[MAX_N << 2], tree_pt[MAX_N << 2], park[(int)1e6 + 2000];
int getFirst(int a, int b, int c, int d)
{
if (a > 0)
return a;
if (b > 0)
return b;
if (c > 0)
return c;
if (d > 0)
return d;
return d;
}
void update(int qx, int l, int r, int p)
{
if (l == r && l == qx)
{
tree_l[p] = l, tree_r[p] = r, tree_len[p] = 0, tree_pt[p] = 0;
return;
}
if (qx <= mid)
update(qx, l, mid, lson);
else
update(qx, mid + 1, r, rson);
tree_l[p] = getFirst(tree_l[lson], tree_r[lson], tree_l[rson], tree_r[rson]);
tree_r[p] = getFirst(tree_r[rson], tree_l[rson], tree_r[lson], tree_l[lson]);
// check three intervals;
// first;
tree_len[p] = tree_len[lson], tree_pt[p] = tree_pt[lson];
if (tree_l[rson] > 0 && tree_r[lson] > 0 && ((tree_l[rson] - tree_r[lson]) >> 1) > tree_len[p])
tree_len[p] = (tree_l[rson] - tree_r[lson]) >> 1, tree_pt[p] = (tree_r[lson] + tree_l[rson]) >> 1;
if (tree_len[rson] > tree_len[p])
tree_len[p] = tree_len[rson], tree_pt[p] = tree_pt[rson];
}
void remove(int qx, int l, int r, int p)
{
if (l == r && l == qx)
{
tree_l[p] = tree_r[p] = tree_len[p] = tree_pt[p] = 0;
return;
}
if (qx <= mid)
remove(qx, l, mid, lson);
else
remove(qx, mid + 1, r, rson);
tree_l[p] = getFirst(tree_l[lson], tree_r[lson], tree_l[rson], tree_r[rson]);
tree_r[p] = getFirst(tree_r[rson], tree_l[rson], tree_r[lson], tree_l[lson]);
// check three intervals;
// first;
tree_len[p] = tree_len[lson], tree_pt[p] = tree_pt[lson];
if (tree_l[rson] > 0 && tree_r[lson] > 0 && ((tree_l[rson] - tree_r[lson]) >> 1) > tree_len[p])
tree_len[p] = (tree_l[rson] - tree_r[lson]) >> 1, tree_pt[p] = (tree_r[lson] + tree_l[rson]) >> 1;
if (tree_len[rson] > tree_len[p])
tree_len[p] = tree_len[rson], tree_pt[p] = tree_pt[rson];
}
int main()
{
scanf("%d%d", &n, &m);
while (m--)
{
int opt, x;
scanf("%d%d", &opt, &x);
if (opt == 1)
{
if (tree_l[1] > 0)
{
int tmp = tree_len[1], key = tree_pt[1];
if (tree_l[1] - 1 >= tmp)
key = 1, tmp = tree_l[1] - 1;
if (n - tree_r[1] > tmp)
key = n, tmp = n - tree_r[1];
park[x] = key;
}
else
park[x] = 1;
printf("%d\n", park[x]), update(park[x], 1, n, 1);
}
else
remove(park[x], 1, n, 1);
}
return 0;
}