- 公开赛事
# AtCoder Beginner Contest 469 题解 (A - E)
- @ 2026-8-1 21:52:25
A - Train Car
题意
一列火车共有 节车厢。你现在位于从车头往后数的第 节车厢。请问,你当前所在的位置是从车尾往前数的第几节车厢?
思路
简单的数学计算。从前往后的索引加上从后往前的索引等于总数加一。因此,从后往前的序号为 。
核心代码 (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, k;
cin >> n >> k;
cout << (n + 1 - k);
}
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 - Isolated Seats
题意
给定一排共 个座位,用一个长度为 的字符串表示。其中 'o' 表示该座位已被占用,'x' 表示该座位为空。
一个空的座位被称为“孤立的”(Isolated),当且仅当它的左边和右边(如果存在的话)也都是空的。求孤立空座位的数量。
思路
直接遍历数组,对于每一个位置 (),如果它本身是空的,并且前一个位置也是空的(或者不存在),且后一个位置也是空的(或者不存在),我们就将计数器加 。
在实现上,借助全局数组外围默认初始化为 的特性,可以直接判断 a[i] == 0 && a[i - 1] == 0 && a[i + 1] == 0,非常简洁。
核心代码 (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 ++) {
char t; cin >> t;
if(t == 'o') a[i] = 1;
}
int ans = 0;
for(int i = 1; i <= n; i ++) {
if(a[i] == 0 && a[i - 1] == 0 && a[i + 1] == 0) ans ++;
}
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 - Cantrip
题意
给定一个长度为 的字符串,由 'o' 和 'x' 组成。需要执行 次查询操作:在第 次操作中,输出第 个 'x' 所在的位置(1-based 索引)。
思路
使用一个指针 来动态追踪 'x' 的位置。初始时 。
每次查询时,先让 向前走一步(前提是 ),然后通过 while 循环跳过所有连续的 'o',直到找到下一个 'x'。找到后直接输出当前位置的 。
这种做法的总体时间复杂度是 的,因为指针 只会不断向右移动,不会回退。
核心代码 (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];
int b[N];
void solve()
{
int n;
cin >> n;
int r = 0;
for(int i = 1; i <= n; i ++) {
char t; cin >> t;
if(t == 'o') a[i] = 1;
}
for(int i = 1; i <= n; i ++) {
if(r < n) r ++;
while(r < n && a[r] == 1) r ++;
cout << r << endl;
}
// for(int i = 1; i <= n; i ++) cout << b[i] << "\n";
}
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 - The Big Two
题意
给定一个包含 个顶点和 条边的无向图。请问,有多少对不同的顶点 (即大小恰好为 2 的顶点覆盖),满足图中的每一条边都至少连接了 或 中的一个?
思路
图中的任意一条边都必须被顶点覆盖。我们可以特别关注第一条边:它连接了顶点 和 。为了覆盖这条边,合法的顶点对 中必定至少包含 或 。 于是,我们可以分两种情况进行讨论:
- 假设其中一个顶点是 :遍历所有的边,如果一条边已经被 覆盖,那就不需要管它了;如果一条边没有被 覆盖,那么它的两个端点必须有一个是 。统计剩下需要覆盖的边数
mb。如果要用一个顶点 覆盖完这mb条边,由于没有重边等干扰,那么 在这些边中出现的次数cnt[y]必须恰好等于mb。满足条件的 和 就能构成合法对。 - 假设其中一个顶点是 :同理进行筛选。
最后把找到的无序对 全部放入一个 set 中去重,set 的大小就是合法对的数量。整体时间复杂度为 。
核心代码 (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], b[N];
int check(int x, int y, int c) {
if(x == c || y == c) return 0;
if(x == c) return y;
if(y == c) return x;
}
void solve()
{
int n, m;
cin >> n >> m;
for(int i = 1; i <= m; i ++) {
cin >> a[i] >> b[i];
if(a[i] > b[i]) swap(a[i], b[i]);
}
set<PII> s;
{
int mb = m - 1;
vector<int>cnt(n + 1, 0);
for(int i = 2; i <= m; i ++) {
if(a[i] == a[1] || b[i] == a[1]) mb --;
else {
cnt[a[i]] ++;
cnt[b[i]] ++;
}
}
for(int i = 1; i <= n; i ++) {
if(cnt[i] == mb) {
int x = a[1], y = i;
if(x == y) continue;
if(x > y) swap(x, y);
s.insert({x, y});
}
}
}
{
int mb = m - 1;
vector<int>cnt(n + 1, 0);
for(int i = 2; i <= m; i ++) {
if(a[i] == b[1] || b[i] == b[1]) mb --;
else {
cnt[a[i]] ++;
cnt[b[i]] ++;
}
}
for(int i = 1; i <= n; i ++) {
if(cnt[i] == mb) {
int x = b[1], y = i;
if(x == y) continue;
if(x > y) swap(x, y);
s.insert({x, y});
}
}
}
cout << s.size();
}
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 - Pro Exam Eligibility
题意
给定 场考试的结果,用长度为 的字符串表示('o' 表示通过,'x' 表示未通过)。
我们需要找出一个连续的考试区间,要求该区间内至少包含 次通过,且区间的通过率(通过次数 / 区间总考试次数)最大。求这个最大通过率。
思路
求平均值/比例的最大值,标准的解法是二分答案(01分数规划)。 我们可以二分猜测最大通过率 :
- 我们将每个
'o'视为 ,每个'x'视为 。然后求出这个变换后数组的前缀和 。 - 如果存在一个区间 ,其包含的
'o'次数 ,且区间和 ,就说明我们能够达到通过率 。 - 为了在 的时间内完成检查,我们维护两个指针 和 。当右端点 向右移动时,我们累计窗口内的通过次数
cnt。 - 如果
cnt >= m,就意味着当前的 以及更小的 都是满足“包含通过次数至少为 ”的合法左端点。因此我们将合法的 更新到一个记录最小值的变量mn中。同时通过递增 缩小窗口边界。 - 最后只需要判断是否有 即可。
核心代码 (C++)
#include<bits/stdc++.h>
#define double long double
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
const double eps = 1e-10;
//vector<vector<int>>adj(N);
double a[N], b[N];
void solve()
{
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; i ++) {
char t; cin >> t;
if(t == 'o') a[i] = 1;
}
auto check = [&](double mid) -> bool {
for(int i = 1; i <= n; i ++) {
b[i] = a[i] - mid;
b[i] += b[i - 1];
// cerr << b[i] << endl;
}
int cnt = 0;
double mn = INF;
for(int r = 1, l = 0; r <= n; r ++) {
if(a[r] >= 1) cnt ++;
while(cnt >= m) {
mn = min(mn, b[l]);
if(a[l] >= 1) cnt --;
l ++;
}
if(b[r] - mn >= 0) return true;
}
return false;
};
// cout << check(0.6) << endl;
// cout << check(0.7) << endl;
double l = 0, r = 1;
while(r - l > eps) {
double mid = (l + r) /2;
if(check(mid)) l = mid;
else r = mid;
}
cout << l << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cout << setprecision(12) << fixed;
int t;t=1;
//cin>>t;
for(int i=1;i<=t;i++){
//printf("Case %d: ",i);
solve();
}
return 0;
}
1 条评论
-
gaofeng LV 3 @ 2026-8-1 22:02:35
1
👍 2
- 1