1611 字
8 分钟
abc459
NOTE

比赛链接:Atcoder Beginner Contest 459

A - Hell, World!#

关键词:签到

Code#

// Problem: AT ABC459 A
// Contest: AtCoder - Tokio Marine & Nichido Fire Insurance Programming Contest 2026 (AtCoder Beginner Contest 459)
// URL: https://atcoder.jp/contests/abc459/tasks/abc459_a
// Time: 2026-05-23 22:11:25
#include <bits/stdc++.h>
using namespace std;
// clang-format off
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define fastio() ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// clang-format on
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pdd = pair<double, double>;
using pll = pair<long long, long long>;
using i128 = __int128;
const int dx[] = {-1, 0, 1, 0, -1, 1, 1, -1};
const int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int inf = 0x3f3f3f3f;
const int N = 0;
void solve()
{
int n;
cin >> n;
string s = "HelloWorld";
for (int i = 0; i < 10; i++)
{
if (i == n - 1)
continue;
cout << s[i];
}
}
int main()
{
fastio();
int T = 1;
// cin >> T;
while (T--)
solve();
return 0;
}

B - 459#

关键词:签到

Code#

// Problem: AT ABC459 B
// Contest: AtCoder - Tokio Marine & Nichido Fire Insurance Programming Contest 2026 (AtCoder Beginner Contest 459)
// URL: https://atcoder.jp/contests/abc459/tasks/abc459_b
// Time: 2026-05-23 22:13:23
#include <bits/stdc++.h>
using namespace std;
// clang-format off
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define fastio() ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// clang-format on
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pdd = pair<double, double>;
using pll = pair<long long, long long>;
using i128 = __int128;
const int dx[] = {-1, 0, 1, 0, -1, 1, 1, -1};
const int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int inf = 0x3f3f3f3f;
const int N = 0;
void solve()
{
int n;
cin >> n;
vector<int> res;
while (n--)
{
string s;
cin >> s;
if (s[0] >= 'a' && s[0] <= 'c')
res.emplace_back(2);
else if (s[0] >= 'd' && s[0] <= 'f')
res.emplace_back(3);
else if (s[0] >= 'g' && s[0] <= 'i')
res.emplace_back(4);
else if (s[0] >= 'j' && s[0] <= 'l')
res.emplace_back(5);
else if (s[0] >= 'm' && s[0] <= 'o')
res.emplace_back(6);
else if (s[0] >= 'p' && s[0] <= 's')
res.emplace_back(7);
else if (s[0] >= 't' && s[0] <= 'v')
res.emplace_back(8);
else if (s[0] >= 'w' && s[0] <= 'z')
res.emplace_back(9);
}
for (auto i: res)
cout << i;
}
int main()
{
fastio();
int T = 1;
// cin >> T;
while (T--)
solve();
return 0;
}

C - Drop Blocks#

关键词:思维

思路#

通过记录“历史总增加量”和“全局被消除的层数”,我们可以把每次操作的时间复杂度降到 O(1)O(1)

a[i]:第 ii 个格子历史上总共被放入过多少个积木(不考虑被消除的操作)。

b[k]:目前为止,历史总放入积木数 k\ge k 的格子有多少个

  • 注意:这里是不扣减消除操作的。例如 b[3] = 2 代表有 2 个格子至少被放进过 3 次积木。

h:全局触发了多少次“消除操作”。可以把它理解为“水位线”。

操作 11 的逻辑如下:

  1. a[i]++:给第 ii 个格子的历史总数 +1+1
  2. b[a[i]]++:因为第 ii 个格子的历史总数到达了 a[i],所以 ++
  3. if (b[a[i]] == n) h++;:当所有 NN 个格子的积木数都 1\ge 1 时,触发“全部格子减 1”。换成历史数据来看,就是当所有 NN 个格子的历史总数都 h+1\ge h + 1 时触发消除。由于每次只加 1 个积木,触发消除条件的那一瞬间,必定是目前积木最少的那个格子刚好补齐了短板。所以此时的 a[i] 恰好就是 h+1h + 1。当 b[a[i]] == n 时,说明 nn 个格子历史总数都达到了当前高度,全局消除次数 h 增加 1。

操作 22 的逻辑如下:

题目要求输出当前积木数 y\ge y 的格子数量,格子的当前积木数 = 历史总增加量 (a[x]) - 全局消除量 (h)。所以,条件 当前积木数 >= y 等价于 a[x] - h >= y,移项后得到 a[x] >= h + y。所以 b[h + y] 恰好就是“历史总增加量 h+y\ge h + y 的格子数量”

NOTE

由于 h + i 的最大值可以达到 6×1056 \times 10^5,代码上注意数组要开大点。

Code#

// Problem: AT ABC459 C
// Contest: AtCoder - Tokio Marine & Nichido Fire Insurance Programming Contest 2026 (AtCoder Beginner Contest 459)
// URL: https://atcoder.jp/contests/abc459/tasks/abc459_c
// Time: 2026-05-23 22:17:08
#include <bits/stdc++.h>
using namespace std;
// clang-format off
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define fastio() ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// clang-format on
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pdd = pair<double, double>;
using pll = pair<long long, long long>;
using i128 = __int128;
const int dx[] = {-1, 0, 1, 0, -1, 1, 1, -1};
const int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int inf = 0x3f3f3f3f;
const int N = 1e6 + 10;
int a[N], b[N];
void solve()
{
int n, q;
cin >> n >> q;
ll h = 0;
while (q--)
{
int op, i;
cin >> op >> i;
if (op == 1)
{
a[i]++;
b[a[i]]++;
if (b[a[i]] == n)
h++;
}
else
cout << b[h + i] << endl;
}
}
int main()
{
fastio();
int T = 1;
// cin >> T;
while (T--)
solve();
return 0;
}

D - Adjacent Distinct String#

关键词:构造,贪心

思路#

能不能成功重排,取决于出现次数最多的那个字符。假如在长度为 NN 的字符串中,某个字符出现了 MM 次,为了让这个字符不相邻,最优的摆放方式就是隔一个一放,位置只有 (N+1)/2\lfloor (N + 1) / 2 \rfloor 个,因此如果所有字符出现的次数最大值 M>(N+1)/2M > \lfloor (N + 1) / 2 \rfloor,则一定无解。

确认有解后,我们考虑构造方案:

  1. 统计并排序:统计每个字符的出现次数,然后按出现次数从大到小对字符进行排序

  2. 先填偶数位:把排序后的字符依次填入新字符串的偶数索引位置(0,2,40, 2, 4 \dots)。

  3. 再填奇数位:当偶数位全部填满后,回到开头,继续把剩下的字符填入奇数索引位置(1,3,51, 3, 5 \dots)。

Code#

// Problem: AT ABC459 D
// Contest: AtCoder - Tokio Marine & Nichido Fire Insurance Programming Contest 2026 (AtCoder Beginner Contest 459)
// URL: https://atcoder.jp/contests/abc459/tasks/abc459_d
// Time: 2026-05-23 22:51:34
#include <bits/stdc++.h>
using namespace std;
// clang-format off
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define fastio() ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// clang-format on
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pdd = pair<double, double>;
using pll = pair<long long, long long>;
using i128 = __int128;
const int dx[] = {-1, 0, 1, 0, -1, 1, 1, -1};
const int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int inf = 0x3f3f3f3f;
const int N = 0;
void solve()
{
string s;
cin >> s;
int n = s.length();
// pair<出现次数, 字符>
vector<pair<int, char>> cnt(26);
for (int i = 0; i < 26; i++)
cnt[i] = {0, i + 'a'};
for (char c: s)
cnt[c - 'a'].first++;
sort(cnt.begin(), cnt.end(), greater<pair<int, char>>());
if (cnt[0].first > (n + 1) / 2)
{
cout << "No" << endl;
return;
}
string res(n, ' ');
int idx = 0;
for (int i = 0; i < 26; i++)
{
while (cnt[i].first > 0)
{
res[idx] = cnt[i].second;
cnt[i].first--;
idx += 2;
// 如果偶数位填满了,回到开头填奇数位
if (idx >= n)
idx = 1;
}
}
cout << "Yes" << endl << res << endl;
}
int main()
{
fastio();
int T = 1;
cin >> T;
while (T--)
solve();
return 0;
}