Codeforces Round #500 (Div. 1) [based on EJOI] - Problem-Solving Report
A - Photo of The Sky
Fairly obviously, we need to pick two point sets, and the final contribution is the product of the two sets’ ranges. Sort them, and then just go through every interval of length \(n\).
// A.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5 + 200;
int n, ai[MAX_N << 1];
int main()
{
scanf("%d", &n);
for (int i = 1; i <= (n << 1); i++)
scanf("%d", &ai[i]);
sort(ai + 1, ai + 1 + (n << 1));
long long ans = 1e18;
for (int i = 1; i <= n; i++)
ans = min(ans, 1LL * (ai[i + n - 1] - ai[i]) * (ai[n << 1] - (i == 1 ? ai[n + 1] : ai[1])));
printf("%lld\n", ans);
return 0;
}
B - Chemical table
Damn it, I thought about it forever and in the end found out it’s done with a bipartite graph. Drives me crazy.
Consider connecting each row number to its column number, which draws a bipartite graph. Think about what a triple formed by two edges means: say two left vertices \(u, v\) and one right vertex \(w\). We find that the right vertex lets the right vertices connected to \(u, v\) be “shared”, because for the points \(u, v\) of one row, the column information of \(u, v\) (think of it as a bitset) can be merged, so a connected component of the bipartite graph makes the whole component work.
The answer is just the number of connected components minus a \(1\).
// B.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 4e5 + 200;
typedef pair<int, int> pii;
int n, m, q, sumx[MAX_N], sumy[MAX_N], color[MAX_N];
pii pts[MAX_N];
vector<int> G[MAX_N];
void dfs(int u, int org)
{
color[u] = org;
for (auto v : G[u])
if (color[v] == 0)
dfs(v, org);
}
int main()
{
scanf("%d%d%d", &n, &m, &q);
for (int i = 1, u, v; i <= q; i++)
scanf("%d%d", &u, &v), G[u].push_back(v + n), G[v + n].push_back(u);
int ans = 0;
for (int i = 1; i <= n + m; i++)
if (color[i] == 0)
dfs(i, ++ans);
printf("%d\n", ans - 1);
return 0;
}
C - Hills
A stupid DP, harder than the previous problem (sweat).
Think about designing the states, and you find you can just f*ck your way through it directly in \(\Theta(n^2)\). Three states turn out to be enough: placed, not placed, and already lowered. Then just transition however you like.
// C.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 5050;
int n, ai[MAX_N], dp[MAX_N][MAX_N][3];
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &ai[i]);
memset(dp, 0x3f, sizeof(dp));
dp[0][0][0] = 0;
for (int i = 1; i <= n; i++)
{
dp[i][0][0] = 0;
for (int j = 1; j <= (i + 1) >> 1; j++)
{
if (ai[i] > ai[i - 1])
dp[i][j][1] = min(dp[i][j][1], dp[i - 1][j - 1][0]);
if (i != 1)
{
if (ai[i] > ai[i - 2] - 1)
dp[i][j][1] = min(dp[i][j][1], dp[i - 1][j - 1][2]);
else
dp[i][j][1] = min(dp[i][j][1], dp[i - 1][j - 1][2] + ai[i - 2] - 1 - ai[i] + 1);
}
if (ai[i] <= ai[i - 1])
dp[i][j][1] = min(dp[i][j][1], dp[i - 1][j - 1][0] + ai[i - 1] - ai[i] + 1);
// choose;
dp[i][j][0] = min(dp[i][j][0], min(dp[i - 1][j][2], dp[i - 1][j][0]));
if (ai[i] < ai[i - 1])
dp[i][j][0] = min(dp[i][j][0], dp[i - 1][j][1]);
if (ai[i] >= ai[i - 1])
dp[i][j][2] = min(dp[i][j][2], min(dp[i - 1][j][1], dp[i - 1][j][0]) + ai[i] - ai[i - 1] + 1);
}
}
for (int i = 1; i <= (n + 1) >> 1; i++)
printf("%d ", min(dp[n][i][0], min(dp[n][i][1], dp[n][i][2])));
puts("");
return 0;
}
D - AB-Strings
First, consecutive characters can be compressed; you can use a linked list. Once you have the linked list, you find that if the heads differ, you swap an odd number of units; if they are the same, just pick an odd and an even number.
However, this isn’t necessarily globally optimal. If you want to reduce things right now, this is obviously optimal; but global optimality requires that each operation makes the two strings as close to equal in size as possible, so that the next step is a better one.
The code didn’t pass, and it’s hard to write, so I won’t post it.