Luogu Elementary Round #14 & Flanksy Round I Editorial by Redshift_Shine × SyuAyaka (PARTIAL)
Redshift_Shine · · 算法·理论
1. Up to the Clouds
The answer depends on parity.
If
#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
#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
If we have a virtual rose which blooms in the range
The answer of the question raised in hint
Prefix sum.
We can calculate the sequence
Then, calculate value in hint
It can be proved that moving
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);
}