AtCoder Grand Contest 041 - Problem-Solving Report
A - Table Tennis Training
A brain-dead problem; just consider the two ends and the parity.
// A.cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ll n, A, B, ans = 0x7fffffffffffffff;
scanf("%lld%lld%lld", &n, &A, &B);
ans = min(A - 1, n - B) + 1 + ((B - A - 1) >> 1);
if (!((A^B) & 1))
ans = min(ans, (B - A) >> 1);
printf("%lld\n", ans);
return 0;
}
B - Voting Judges
First you need to realize that this problem can be binary searched, because if a larger answer is feasible, then a smaller answer is feasible too. Next, think about writing a check. First rule out the case where it can't make the cut even with the votes added to it alone, then count the votes that can be provided by the ones smaller than \(a_{mid}\) among the \(a_i\). Finally, just check whether this many votes can get it to make the cut.
// B.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5 + 200;
int n, m, V, P, ai[MAX_N];
bool check(int mid)
{
if (ai[mid] + m < ai[P])
return false;
long long acc = 0;
for (int i = P; i <= n; i++)
if (i != mid && ai[i] < ai[mid] + m)
acc += min(m, ai[mid] + m - ai[i]);
return acc >= 1LL * (V - 1) * m;
}
int main()
{
scanf("%d%d%d%d", &n, &m, &V, &P);
for (int i = 1; i <= n; i++)
scanf("%d", &ai[i]);
sort(ai + 1, ai + 1 + n), reverse(ai + 1, ai + 1 + n);
V = max(1, V - P + 1);
int l = P, r = n, res = P;
while (l <= r)
{
int mid = (l + r) >> 1;
if (check(mid))
res = mid, l = mid + 1;
else
r = mid - 1;
}
printf("%d\n", res);
return 0;
}
C - Domino Quality
First, we can find that case for \(3\) with a unit count of \(2\), and that case can just be output directly; besides that, we can work out ourselves the cases \(4 \to 7\) with a unit count of \(3\). After that, if \(n > 7\), write it as \(n = 4x + y, y > 3\), then just piece them together along the diagonal.
// C.cpp
#include <bits/stdc++.h>
using namespace std;
int n;
char buffer[1010][1010];
const char m3[3][4] =
{"aa.",
"..a",
"..a"};
const char m4[4][5] =
{"aabc",
"ddbc",
"bcaa",
"bcdd"};
const char m5[5][6] =
{"aabba",
"bcc.a",
"b..cb",
"a..cb",
"abbaa"};
const char m6[6][7] =
{"aabc..",
"ddbc..",
"..aabc",
"..ddbc",
"bc..aa",
"bc..dd"};
const char m7[7][8] =
{"aabbcc.",
"dd.dd.a",
"..d..da",
"..d..db",
"dd.dd.b",
"..d..dc",
"..d..dc"};
void writeBuffer(int id, int x, int y)
{
for (int i = 0; i < id; i++)
{
const char *str;
switch (id)
{
case 3:
str = m3[i];
break;
case 4:
str = m4[i];
break;
case 5:
str = m5[i];
break;
case 6:
str = m6[i];
break;
case 7:
str = m7[i];
break;
}
for (int j = 0; str[j]; j++)
buffer[x + i][y + j] = str[j];
}
}
int main()
{
scanf("%d", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
buffer[i][j] = '.';
if (n <= 2)
puts("-1"), exit(0);
if (n <= 7)
writeBuffer(n, 0, 0);
else
{
int base = 0;
writeBuffer(n % 4 + 4, base, base), base += n % 4 + 4;
while (base < n)
writeBuffer(4, base, base), base += 4;
}
for (int i = 0; i < n; i++)
printf("%s\n", buffer[i]);
return 0;
}
D - Problem Scores
This should be the most interesting problem this time; it was posted a while ago in one of lcx's coach group chats.
Really, it just needs its \(k + 1\) prefix sum to be strictly greater than its \(k\) suffix sum. Written out, that is:
\[ \sum_{i = 1}^{k + 1} a_i > \sum_{i = n - k + 1}^n a_i \]
There are \(n\) such constraints in total, and we try to find properties to cut them down. First, for the case \(k > \lfloor \frac{n}{2} \rfloor\), the prefix and the suffix overlap in the middle; this part can be moved to one side and canceled directly, turning it into the case \(k \leq \lfloor \frac{n}{2} \rfloor\).
Next, consider using the property \(a_i \leq a_{i + 1}\); we find that if it is satisfied when \(k = \lfloor \frac{n}{2} \rfloor\), the cases \(k \leq \lfloor \frac{n}{2} \rfloor\) are fully satisfied.
So now we only need to guarantee \(\sum_{i = 1}^{k + 1} a_i > \sum_{i = n - k + 1}^n a_i, k = \lfloor \frac{n}{2} \rfloor\).
Now we take differences of this sequence, so that \(a_k = 1 + \sum_{i = 1}^k \Delta_i\). First, obviously \(\Delta \geq 0\). Second, when plugging back into the formula, we find that the prefix sums can be broken down into \(\sum \Delta_i c_i\), where this \(c_i\) is easy to compute. Let's substitute:
\[ \begin{gathered} \sum_{i = 1}^{k + 1} 1 + \sum_{j = 1}^i \Delta_j > \sum_{i = n - k + 1}^n 1 + \sum_{j = 1}^i \Delta_j \\ 1 + \sum_{i = 1}^{k + 1} \sum_{j = 1}^i \Delta_j > \sum_{i = n - k + 1}^n \sum_{j = 1}^i \Delta_j \\ \sum_{i = 1}^{k + 1} \sum_{j = 1}^i \Delta_j \geq \sum_{i = n - k + 1}^n \sum_{j = 1}^i \Delta_j \\ \sum_{j = 1}^n \Delta_j \sum_{i = 1}^{k + 1} [j \leq i] \geq \sum_{j = 1}^n \Delta_j \sum_{i = n - k + 1}^n [j \leq i] \\ \sum_{j = 1}^{k + 1} \Delta_j (k + 2 - j) \geq \sum_{j = 1}^n \Delta_j (n - \max(j, n - k + 1) + 1) \end{gathered} \]
This gives:
\[ \begin{gathered} (k + 1) \Delta_1 + \sum_{j = 2}^{k + 1} \Delta_j (k + 2 - j) \geq k\Delta_1 + \sum_{j = 2}^n \Delta_j (n - \max(j, n - k + 1) + 1) \\ \Delta_1 \geq \sum_{j = 2}^n \Delta_j (n - \max(j, n - k + 1) + 1) - \sum_{j = 2}^{k + 1} \Delta_j (k + 2 - j) \\ \Delta_1 \geq \sum_{j = 2}^n \Delta_j (n - \max(j, n - k + 1) + 1) - \sum_{j = 2}^n \Delta_j \max(0, k + 2 - j) \\ \Delta_1 \geq \sum_{j = 2}^n \Delta_j (n + 1 - (\max(j, n - k + 1) + \max(0, k + 2 - j))) \\ \Delta_1 \geq \sum_{j = 2}^n \Delta_j (n + 1 - \max(j, k + 2, n - k + 1, n + 3 - j)) \\ \Delta_1 \geq \sum_{j = 2}^n \Delta_j (n + 1 + \min(-j, - k - 2, - n + k - 1, - n - 3 + j)) \\ \Delta_1 \geq \sum_{j = 2}^n \Delta_j \min(n - j + 1, n - k - 1, k, j - 2) \\ \end{gathered} \]
As a reminder, \(k = \lfloor \frac{n}{2} \rfloor\):

The intervals are discrete, so in the end \(\min(n - j + 1, n - k - 1, k, j - 2) = \min(j - 2, n - j + 1)\). In the end, the whole inequality looks like:
\[ \Delta_1 \geq [\Delta_2, \Delta_3, \dots \Delta_n] \cdot [0, 1, 2, 3, \dots 3, 2, 1] \]
Let:
\[ \begin{gathered} a = \Delta_2 + \Delta_3 + \Delta_4 + \dots + \Delta_n \\ b = [\Delta_2, \Delta_3, \dots \Delta_n] \cdot [0, 1, 2, 3, \dots 3, 2, 1] \end{gathered} \]
We find that \(\Delta_1 \leq n - 1 - a, \Delta_1 \geq b \), so \(\Delta_1\) can be chosen in \(\max(0, n - 1 - (a + b))\) ways. \(a + b = [\Delta_2, \Delta_3, \dots \Delta_n] \cdot [1, 2, 3, 4, \dots 4, 3, 2]\), so we use \(DP\) to count, for a sum of \(j = a + b\), the number of \(\Delta_i\) sequences, and then multiplying that count by \(\max(0, n - 1 - (a + b))\) gives the answer.
// D.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 5050;
int n, mod, dp[MAX_N], coeff[MAX_N];
int main()
{
scanf("%d%d", &n, &mod), dp[0] = 1;
for (int i = 1; i < n; i++)
for (int j = min(n - i + 1, i), k = j; j < n; j++)
dp[j] = (0LL + dp[j] + dp[j - k]) % mod;
int ans = 0;
for (int i = 0; i < n; i++)
ans = (0LL + ans + 1LL * dp[i] * (n - i) % mod) % mod;
printf("%d\n", ans);
return 0;
}