- 公开赛事
AtCoder Beginner Contest 471 题解 (A - F)
- @ 2026-8-15 21:48:36
AtCoder Beginner Contest 471 题解 (A - F)
A - Nine or Nein
题意
给定两个数字 和 。如果它们满足 、、 或者 中的任意一个条件,输出 Nine,否则输出 Nein。
思路
简单的条件判断。直接用 if 语句检查四个条件,如果有任何一个成立,就输出 Nine,否则输出 Nein。
核心代码 (C++)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>PII;
const int N=1e6+10;
const int mod=998244353;
const int INF = 0x3f3f3f3f;
const ll INFll = 0x3f3f3f3f3f3f3f3f;
#define endl "\n"
#define x first
#define y second
//vector<vector<int>>adj(N);
void solve()
{
int a, b;
cin >> a >> b;
bool ky = false;
if(a + b == 9 || a - b == 9 || a * b == 9 || a == 9 * b) ky = true;
if(ky) cout << "Nine";
else cout << "Nein";
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cout << setprecision(11) << fixed;
int t;t=1;
//cin>>t;
for(int i=1;i<=t;i++){
//printf("Case %d: ",i);
solve();
}
return 0;
}
B - Survey Tabulation
题意
给定 个字符串。需要统计出现次数最多的字符串,并忽略大小写的区别。求出现频率最高的字符串的出现次数。
思路
可以使用哈希表(如 map<string, int>)来统计词频。在统计之前,先将所有的字符串中的大写字母统一转换为小写字母,以消除大小写的影响。在遍历的同时更新最大频次 ans 即可。
核心代码 (C++)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>PII;
const int N=1e6+10;
const int mod=998244353;
const int INF = 0x3f3f3f3f;
const ll INFll = 0x3f3f3f3f3f3f3f3f;
#define endl "\n"
#define x first
#define y second
//vector<vector<int>>adj(N);
void solve()
{
int n;
cin >> n;
int ans = 0;
map<string,int> cnt;
while(n --) {
string s;
cin >> s;
for(auto &t : s) {
if('A' <= t && t <= 'Z') t = t - 'A' + 'a';
}
cnt[s] ++;
ans = max(ans, cnt[s]);
}
cout << ans;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cout << setprecision(11) << fixed;
int t;t=1;
//cin>>t;
for(int i=1;i<=t;i++){
//printf("Case %d: ",i);
solve();
}
return 0;
}
C - Cookies and Greedy Takahashi
题意
数轴上有 个饼干,分别位于坐标 。高桥君初始在原点 。他每次会贪心地朝着距离他当前位置最近的饼干移动并吃掉它。如果有两个饼干距离相同,他优先向左走。求高桥君吃完所有饼干所移动的总距离。
思路
先对所有饼干的坐标进行排序。利用双指针 和 分别指向原点左侧最靠近原点的饼干和右侧最靠近原点的饼干。 高桥君当前位置为 。每次比较 到左侧未吃饼干 的距离和到右侧未吃饼干 的距离,选择较近的一边进行移动(若相等则向左走)。累加移动距离更新 ,并移动对应方向的指针,直到所有饼干都被吃完。这种方法可以在 内高效模拟整个贪心过程。
核心代码 (C++)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>PII;
const int N=1e6+10;
const int mod=998244353;
const int INF = 0x3f3f3f3f;
const ll INFll = 0x3f3f3f3f3f3f3f3f;
#define endl "\n"
#define x first
#define y second
//vector<vector<int>>adj(N);
int a[N];
void solve()
{
int n;
cin >> n;
for(int i = 1; i <= n; i ++) {
cin >> a[i];
}
sort(a + 1, a + n + 1);
int l = 0, r = n + 1;
for(int i = 1; i <= n; i ++) {
if(a[i] <= 0) l = i;
if(a[i] >= 0 && r == n + 1) r = i;
}
ll x = 0;
ll ans = 0;
while(l != 0 || r != n + 1) {
if(l != 0 && r != n + 1) {
if(abs(x - a[l]) <= abs(x - a[r])) {
ans += abs(x - a[l]);
x = a[l];
l --;
} else {
ans += abs(x - a[r]);
x = a[r];
r ++;
}
} else if(l != 0) {
ans += abs(x - a[l]);
x = a[l];
l --;
} else {
ans += abs(x - a[r]);
x = a[r];
r ++;
}
// cout << l << " " << r << " " << x << endl;
}
cout << ans;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cout << setprecision(11) << fixed;
int t;t=1;
//cin>>t;
for(int i=1;i<=t;i++){
//printf("Case %d: ",i);
solve();
}
return 0;
}
D - Chargers
题意
有 次操作,操作分为两类:
1 t w:在时刻 ,将一个电量需求为 的设备放入充电队列。2 t:在时刻 ,如果有设备在排队,取出最优(电量需求最大)的设备为其充电。充电的最大电量限制为 。输出实际充入的电量;如果没有设备排队,输出-1。
思路
维护一个优先队列(大根堆)。当操作 1 发生时,我们将该设备的优先级设为 w - t 并存入优先队列。
对于操作 2,如果队列为空则输出 -1;否则取出堆顶元素。该设备需要充的电量实际上就是当时传入的 ,利用 T.first + T.second (即 )就能直接还原出 。最后输出 min(v, w) 即可,表示受最大充电量 限制后的实际充电量。
核心代码 (C++)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e6+10;
const int mod=998244353;
const int INF = 0x3f3f3f3f;
const ll INFll = 0x3f3f3f3f3f3f3f3f;
#define endl "\n"
#define x first
#define y second
//vector<vector<int>>adj(N);
void solve()
{
ll q, v;
cin >> q >> v;
typedef pair<ll,ll>PII;
priority_queue<PII> pq;
while(q --) {
ll op, t, w;
cin >> op >> t;
if(op == 1) {
cin >> w;
pq.push({w - t, t});
} else {
if(!pq.size()) {
cout << "-1\n";
} else {
auto T = pq.top(); pq.pop();
int ans = min(v, t - T.second + T.first + T.second);
cout << ans << endl;
}
}
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cout << setprecision(11) << fixed;
int t;t=1;
//cin>>t;
for(int i=1;i<=t;i++){
//printf("Case %d: ",i);
solve();
}
return 0;
}
E - Sum of Square of Sum
题意
给定一个长度为 的数组 和一个整数 。需要求出从这 个数中任选 个数的所有子集,对于每个子集计算其元素和的平方,最后将所有子集的这个平方值求和,结果对 取模。
思路
对于任意一个大小为 的子集 ,其元素和的平方展开后包含两部分:每个元素自身的平方和 ,以及两两交叉项 。 通过组合数学计数可得,在所有大小为 的子集中:
- 每一个平方项 出现的总次数为 。
- 每对交叉乘积项 出现的总次数为 。
因此,我们在 遍历数组时,动态维护当前已遍历元素的累加和 sum。对于当前元素 :
它对答案的独立贡献为 ;
它与前面所有元素产生的交叉项贡献为 。
利用预处理好的阶乘逆元以 算出组合数,实时累加这些贡献即可得到最终答案,整体时间复杂度为 。
核心代码 (C++)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>PII;
const int N=1e6+10;
const int mod=998244353;
const int INF = 0x3f3f3f3f;
const ll INFll = 0x3f3f3f3f3f3f3f3f;
#define endl "\n"
#define x first
#define y second
//vector<vector<int>>adj(N);
ll fact[N], infact[N];
ll qmi(ll a, ll k, ll p)
{
ll res = 1;
while (k) {
if (k & 1) res = (ll)res * a % p;
a = (ll)a * a % p;
k >>= 1;
}
return res;
}
void init()
{
fact[0] = infact[0] = 1;
for (ll i = 1; i < N; i ++ )
{
fact[i] = (ll)fact[i - 1] * i % mod;
infact[i] = (ll)infact[i - 1] * qmi(i, mod - 2, mod) % mod;
}
}
ll C(ll a,ll b) {
return (ll)fact[a] * infact[b] % mod * infact[a - b] % mod;
}
void solve()
{
init();
int n, k;
cin >> n >> k;
ll sum = 0, ans = 0;
for(int i = 1; i <= n; i ++) {
ll x; cin >> x;
ans += C(n - 1, k - 1) * x % mod * x % mod
+ C(n - 2, k - 2) * x % mod * sum % mod * 2 % mod;
ans %= mod;
sum = (sum + x) % mod;
}
cout << ans;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cout << setprecision(11) << fixed;
int t;t=1;
//cin>>t;
for(int i=1;i<=t;i++){
//printf("Case %d: ",i);
solve();
}
return 0;
}
F - Concat maximize
题意
给定 个仅由数字组成的字符串。你需要选出正好 个字符串,将它们拼接在一起,使得最终组成的数字尽可能大。
思路
不会证明,但可以参考这个题目(luogu P1012)
[P1012 NOIP 1998 提高组] 拼数 - 洛谷
要使得拼接出的数字最大,首先应当保证它的位数(长度)最长,其次在长度相同的情况下,要保证前缀较大。
- 首先对所有字符串进行排序。如果长度不同,长度长的优先;如果长度相同,利用经典的拼接比较策略
a + b > b + a进行排序。 - 选取排在前 的字符串,作为ans1。
- 从后面再挑一个与s[k] 替换一下,作为ans2
- 选定这 个字符串后,再次只按照
a + b > b + a的规则对这 个字符串进行局部排序,以确保它们内部的拼接顺序是最优的。 - 最后将它们拼接并输出,注意跳过前导零。
核心代码 (C++)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>PII;
const int N=1e6+10;
const int mod=998244353;
const int INF = 0x3f3f3f3f;
const ll INFll = 0x3f3f3f3f3f3f3f3f;
#define endl "\n"
#define x first
#define y second
//vector<vector<int>>adj(N);
string s[N];
bool cmp(string a, string b) {
return a + b > b + a;
}
void solve()
{
int n, k;
cin >> n >> k;
for(int i = 1; i <= n; i ++) {
cin >> s[i];
}
sort(s + 1, s + n + 1,[](string a, string b){
if(a.size() != b.size()) return a.size() > b.size();
return a + b > b + a;
} );
vector<string> v1(s + 1, s + k + 1);
vector<string> v2 = v1;
v2.back() = *max_element(s + k, s + n + 1,[](string a, string b){
return stoll(a) < stoll(b);
});
sort(v1.begin(), v1.end(),cmp);
sort(v2.begin(), v2.end(),cmp);
string ans1 = "",ans2 = "";
for(auto t : v1) ans1 += t;
for(auto t : v2) ans2 += t;
ans1 = ans1.substr(min(ans1.find_first_not_of('0'), ans1.size() - 1));
ans2 = ans2.substr(min(ans2.find_first_not_of('0'), ans2.size() - 1));
if(ans1.size() != ans2.size()) {
if(ans1.size() > ans2.size()) cout << ans1;
else cout << ans2;
} else cout << max(ans1, ans2);
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cout << setprecision(11) << fixed;
int t;t=1;
//cin>>t;
for(int i=1;i<=t;i++){
//printf("Case %d: ",i);
solve();
}
return 0;
}
2 条评论
-
sdqe LV 3 @ 2026-8-16 16:07:03
最后一题不是如果存在前k都包含0作为前缀就要尝试去交换吗 比如
3 2 00001 00002 30应该输出3000002
-
@ 2026-8-15 21:52:36嗯……那个……C题和D题是不是反了?
- 1