Nowcoder CSP-S Senior Division Pre-Contest Training Camp 1 - Problem-Solving Report
A - The Hamster's Stone Game
A heh-heh problem. After drawing a few diagrams I found that only the \(1\) case is a first-player win, so just consider swapping the first and second player over multiple rounds.
// A.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e3 + 200, table[8] = {0, 0, 1, 1, 1, 1, 1, 1};
int T, n, ai[MAX_N];
int main()
{
scanf("%d", &T);
while (T--)
{
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &ai[i]);
bool flag = false;
for (int i = 1; i <= n; i++)
flag ^= (ai[i] == 1);
printf(flag ? "rabbit\n" : "hamster\n");
}
return 0;
}
B - Nai'ai and City Congestion
\(\Theta(n^2)\) is easy, so I won't go into it.
Consider rerooting DP: move the parent's information over to the son, then restore it afterward. But it's so damn hard to write, so I'd still recommend everyone write it with Up & Down (and that way of writing it really does look nicer, too). This is my 80-point code from the contest:
// B.cpp
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
const int MAX_N = 1e5 + 200, MAX_K = 15, mod = 1e9 + 7;
int head[MAX_N], current, n, k, dp[MAX_N][MAX_K], prod[MAX_N][MAX_K];
int ans[MAX_N], ans2[MAX_N], org[MAX_N][MAX_K];
struct edge
{
int to, nxt;
} edges[MAX_N << 1];
namespace IO
{
const int MAXSIZE = 1 << 20;
char buf[MAXSIZE], *p1, *p2;
#define gc() \
(p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin), p1 == p2) \
? EOF \
: *p1++)
inline int rd()
{
int x = 0, f = 1;
char c = gc();
while (!isdigit(c))
{
if (c == '-')
f = -1;
c = gc();
}
while (isdigit(c))
x = x * 10 + (c ^ 48), c = gc();
return x * f;
}
} // namespace IO
int quick_pow(int bas, int tim)
{
int ret = 1;
while (tim)
{
if (tim & 1)
ret = 1LL * ret * bas % mod;
bas = 1LL * bas * bas % mod;
tim >>= 1;
}
return ret;
}
void addpath(int src, int dst)
{
edges[current].to = dst, edges[current].nxt = head[src];
head[src] = current++;
}
void build_prod(int u, int fa)
{
prod[u][0] = dp[u][0] % mod;
for (int i = 1; i <= k; i++)
prod[u][i] = (1LL * prod[u][i - 1] + 1LL * dp[u][i]) % mod;
for (int i = head[u]; i != -1; i = edges[i].nxt)
if (edges[i].to != fa)
for (int j = 1; j <= k; j++)
prod[u][j] = (1LL * prod[u][j] * prod[edges[i].to][j - 1] % mod);
}
void dfs(int u, int fa)
{
dp[u][0] = 1;
for (int i = head[u]; i != -1; i = edges[i].nxt)
if (edges[i].to != fa)
{
dfs(edges[i].to, u);
for (int j = 1; j <= k; j++)
dp[u][j] += dp[edges[i].to][j - 1];
}
// get the prod;
build_prod(u, fa);
}
void redfs(int u, int fa)
{
// collecting data;
for (int i = 0; i <= k; i++)
ans[u] += dp[u][i];
build_prod(u, 0);
ans2[u] = prod[u][k];
// to change the root;
for (int i = head[u]; i != -1; i = edges[i].nxt)
if (edges[i].to != fa)
{
// make things ready for the subtree;
for (int j = 1; j <= k; j++)
dp[u][j] -= dp[edges[i].to][j - 1];
for (int j = 1; j <= k; j++)
dp[edges[i].to][j] += dp[u][j - 1];
build_prod(u, edges[i].to);
redfs(edges[i].to, u);
for (int j = 1; j <= k; j++)
dp[edges[i].to][j] -= dp[u][j - 1];
for (int j = 1; j <= k; j++)
dp[u][j] += dp[edges[i].to][j - 1];
build_prod(edges[i].to, u);
build_prod(u, 0);
// rec on myself;
for (int j = 1; j <= k; j++)
prod[u][j] = 1LL * prod[u][j] * prod[edges[i].to][j - 1] % mod;
}
}
int main()
{
memset(head, -1, sizeof(head));
n = IO::rd(), k = IO::rd();
for (int i = 1, u, v; i <= n - 1; i++)
u = IO::rd(), v = IO::rd(), addpath(u, v), addpath(v, u);
dfs(1, 0), redfs(1, 0);
for (int i = 1; i <= n; i++)
printf("%d ", ans[i]);
puts("");
for (int i = 1; i <= n; i++)
printf("%d ", ans2[i]);
puts("");
return 0;
}
C - Little w's Magic Poker
Looks like I need to properly review bipartite graphs.
We can build a bipartite graph, with the card faces on the left side and the card indices on the right. For how the graph is built, see the code: building it this way uses a property, namely that straights are all one big maximal connected block, and every straight is a subset of a maximal connected block; running the Hungarian algorithm from the right endpoint matches the largest part that can be matched, and then we find that if it can't be matched, the left end reached last time has to be given up: because it has been cut off from the part it could connect to before.
// C.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 2e5 + 200;
int head[MAX_N], current, n, k, q, vis[MAX_N], pre[MAX_N], lft[MAX_N], ncnt, matching[MAX_N];
struct edge
{
int to, nxt;
} edges[MAX_N << 1];
void addpath(int src, int dst)
{
edges[current].to = dst, edges[current].nxt = head[src];
head[src] = current++;
}
bool hungery(int u, int org)
{
for (int i = head[u]; i != -1; i = edges[i].nxt)
if (vis[edges[i].to] != org)
{
vis[edges[i].to] = org;
if (!matching[edges[i].to] || hungery(matching[edges[i].to], org))
{
matching[edges[i].to] = u, pre[u] = edges[i].to;
return true;
}
}
return false;
}
int main()
{
memset(head, -1, sizeof(head));
scanf("%d%d", &n, &k);
for (int i = 1, u, v; i <= k; i++)
{
scanf("%d%d", &u, &v);
addpath(u, i + n), addpath(i + n, u);
addpath(v, i + n), addpath(i + n, v);
}
int lcur = 1;
for (int i = 1; i <= n; i++)
{
// fine the leftmost point to pair a conti;
while (!hungery(i, ++ncnt) && lcur <= i)
matching[pre[lcur]] = false, lcur++;
lft[i] = lcur;
}
scanf("%d", &q);
while (q--)
{
int l, r;
scanf("%d%d", &l, &r);
if (lft[r] <= l)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}