Nowcoder CSP-S Senior Division Pre-Contest Training Camp 2 - Problem-Solving Report
I have to reflect carefully on this contest. The state of mind I had over the summer break must not happen again.
A - Server Demand
Suppose we already have plenty of servers. Now we consider things day by day and allocate the servers: suppose there is a set of server IDs \(\{id_i\}\), and consider allocating several of them each day, one day at a time. This guarantees that no server's allocation contains the same date twice.
That way everything can be allocated, and we directly get \(\sum a_i \leq |id_i| m\). After that it's just idiotic code.
// A.cpp
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int MAX_N = 4e5 + 200;
ll n, m, q, ai[MAX_N];
int main()
{
ll ans = 0;
multiset<ll> ms;
scanf("%lld%lld%lld", &n, &m, &q);
for (int i = 1; i <= n; i++)
scanf("%lld", &ai[i]), ms.insert(ai[i]), ans += ai[i];
printf("%lld\n", max(*ms.rbegin(), (ll)(double(ans + m - 1) / double(m))));
while (q--)
{
ll x, y;
scanf("%lld%lld", &x, &y);
ans -= ai[x], ms.erase(ms.find(ai[x]));
ms.insert(ai[x] = y), ans += ai[x];
printf("%lld\n", max(*ms.rbegin(), (ll)(double(ans + m - 1) / double(m))));
}
return 0;
}
B - Desert Point Sequence
In the contest I only coded the 60 points of special-case scoring on this one, which was still pretty easy to write.
Consider the intended solution: first find how many connected components there are, then work out how many cycles there are, to handle the case where \(limit\) is extremely large. When \(limit\) can't cover everything outright, we first take the contribution of all the bridges, and then break cycles to contribute bridges. Find the individual cycles with DFS (if you find them directly with Tarjan, you'll GG), then sort them and pick from largest to smallest.
// B.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e6 + 200, MAX_E = 2e6 + 2000;
int head[MAX_N], current, n, m, limit, ncnt, dfn[MAX_N], low[MAX_N], ptot, stk[MAX_N], tail;
int SCC, aff[MAX_N], dist[MAX_N], loops[MAX_N], loop_cnt;
struct edge
{
int to, nxt;
} edges[MAX_E << 1];
int read()
{
char ch = getchar();
int x = 0, f = 1;
while (!isdigit(ch))
{
if (ch == '-')
f = -1;
ch = getchar();
}
while (isdigit(ch))
x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
return f * x;
}
void addpath(int src, int dst)
{
edges[current].to = dst, edges[current].nxt = head[src];
head[src] = current++;
}
void tarjan(int u, int fa)
{
dfn[u] = low[u] = ++ptot, stk[++tail] = u;
for (int i = head[u]; i != -1; i = edges[i].nxt)
if (edges[i].to != fa)
if (dfn[edges[i].to] == 0)
tarjan(edges[i].to, u), low[u] = min(low[u], low[edges[i].to]);
else
low[u] = min(low[u], dfn[edges[i].to]);
if (dfn[u] == low[u])
{
SCC++;
do
{
aff[stk[tail]] = SCC;
} while (stk[tail--] != u);
}
}
void find_loop(int u, int fa)
{
for (int i = head[u]; i != -1; i = edges[i].nxt)
if (edges[i].to != fa)
if (dist[edges[i].to] == 0)
{
dist[edges[i].to] = dist[u] + 1;
find_loop(edges[i].to, u);
}
else if (dist[edges[i].to] < dist[u] + 1)
loops[++loop_cnt] = dist[u] - dist[edges[i].to] + 1;
}
int main()
{
memset(head, -1, sizeof(head));
n = read(), m = read(), limit = read();
for (int i = 1, u, v; i <= m; i++)
u = read(), v = read(), addpath(u, v), addpath(v, u);
for (int i = 1; i <= n; i++)
if (dfn[i] == 0)
tarjan(i, 0), ncnt++;
// get the non-loop component;
ncnt = SCC - ncnt;
if (limit <= ncnt)
printf("%d\n", SCC + limit - ncnt), exit(0);
limit -= ncnt;
for (int i = 1; i <= n; i++)
if (dist[i] == 0)
dist[i] = 1, find_loop(i, 0);
int ans = SCC;
sort(loops + 1, loops + 1 + loop_cnt);
reverse(loops + 1, loops + 1 + loop_cnt);
for (int i = 1; i <= loop_cnt; i++)
{
limit--;
ans += min(limit, loops[i] - 1);
limit -= loops[i] - 1;
if (limit < 0)
break;
}
printf("%d\n", ans);
return 0;
}
Summary
This contest badly exposed my weakness: my problem-solving ideas are garbage. From now on I'll write a few CF greedy problems every day to keep my IQ up.