1. Up to the Clouds
The answer depends on parity.
If is even, then the result of the formula is always . Otherwise, it can be any positive integer, because all the elements in the sequence is positive.
#include <iostream>
using namespace std;
const int N = 1e5 + 10;
int n, m;
void run()
{
scanf("%d%d", &n, &m);
puts((m & 1) ^ (!n) ? "Yes" : "No");
}
int main()
{
int T = 1;
scanf("%d", &T);
while (T--)
run();
}2. Song of Silent River
Lexicographically smallest.
If you can keep the current character a, then do it.
Since a is the lexicographically smallest character, then it’s always optimal to insert a instead of others.
Use # in the string greedily.
#include <iostream>
#include <string>
using namespace std;
const int N = 1e5 + 10;
int n, m, cts, idx;
string s, t;
int main()
{
cin >> n >> m >> s >> t;
for (int i = 0; i < n; i++)
{
cts += (t[i] == '#');
}
for (auto &i : s)
{
if (i != '#')
continue;
i = idx + 'a';
if (cts >= 25 - idx)
{
cts -= 25 - idx;
idx = 0;
continue;
}
idx++;
idx = (idx == 26 ? 0 : idx);
}
cout << s << '\n';
}3. Drifting Clouds’ Rhythm
Denote as the total number moments when exactly roses are blooming when or before the moment .
If we have a virtual rose which blooms in the range , then how to denote the number the answer will increase when we move that flower’s blooming time to an infinitely faraway place?
The answer of the question raised in hint is .
Prefix sum.
We can calculate the sequence at each turning point. We call a moment a turning point when a rose blooms or fades at this moment.
Then, calculate value in hint for each turning point.
It can be proved that moving away is equivalent to move away then move back.
So use prefix sum to calculate answer for each rose.
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 2e5 + 10;
using ll = long long;
int n, m, x, idx, cur;
ll mx, res[4], p2[N];
using pii = pair<int, int>;
pii dst[N << 2];
bool mp[N];
int main()
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
{
scanf("%d", &x);
dst[++idx] = {x, i};
dst[++idx] = {x + m, i};
}
sort(dst + 1, dst + idx + 1);
for (int i = 1; i <= idx; i++)
{
auto &[a, b] = dst[i];
res[min(cur, 3)] += a - dst[i - 1].first;
if (!mp[b])
{
cur++;
mp[b] = true;
p2[b] = res[2] * 2 + res[3];
continue;
}
mx = max(mx, res[2] * 2 + res[3] - p2[b]);
cur--;
}
printf("%lld\n", res[1] + mx);
}