635 字
2 分钟
Atcoder_Beginner_Contest_466(A~D题)
2026-07-12
浏览量 82 · 访客 8

题目链接:https://atcoder.jp/contests/abc466/tasks

A - Compromise#

我们直接判断是否存在大于等于00的数,存在即说明幸福值可能为正数,输出NoNo,否则输出YesYes

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define endl '\n'
typedef pair<int, int> pii;
void solved(){
int n; cin >> n;
bool f = true;
for (int i = 1; i <= n;i++){
int x; cin >> x;
if(x >= 0) f = false;
}
if(f) cout << "Yes" << endl;
else cout << "No" << endl;
}
int main(){
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
solved();
return 0;
}

B - Representative Balls#

我们用一个二维数组来存储对应mm种的大小ss的球有哪些。然后遍历判断对应的颜色ii是否存在,存在则yesyes设置为真,然后遍历A[i]A[i]中球的大小ss找出最大值mama输出。否则则yesyes为假,直接输出1-1

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define endl '\n'
typedef pair<int, int> pii;
vector<vector<int>> A;
void solved(){
int n, m; cin >> n >> m;
A.resize(m + 1);
for(int i = 1; i <= n;i++){
int c, s; cin >> c >> s;
A[c].push_back(s);
}
for(int i = 1; i <= m;i++){
int ma = 0; bool yes = false;
for(auto it : A[i]){
yes = true;
if(it > ma) ma = it;
}
if(yes) cout << ma << " ";
else cout << -1 << " ";
}
}
int main(){
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
solved();
return 0;
}

C - Count Close Pairs#

交互式问题,我们需要根据系统给出的输入来计算输出正确结果。这题我们需要用一个双指针来解决。我们每次可以询问两个点之间的距离是否大于11,然后系统会给出YesYesNoNo的答案。
我们用一个右指针jj来记录满足条件的区间的右端点。然后开始逐个判断左端点ii对应的最大的右端点jj是多少。也就是不断输出”? i j”,如果输入是YesYes,那么我们就继续右移右端点(j++)(j++),直到系统输入NoNo,则说明当前的jjii的距离已经超过了11,那么我们就计入贡献ansans[i,j)[i, j)这个区间内共有ji1j - i - 1个合法的答案。因为两个指针一共最多移动2×n2 \times n次,所以不会超过2N2N次询问就能得到答案,可以通过本题。
tipstips:注意本题要求每次询问都需要刷新标准输出,也就是每次输出都要使用endlendl

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
//#define endl '\n'
typedef pair<int, int> pii;
void solved(){
int n; cin >> n;
int j = 1;
ll ans = 0;
for(int i = 1; i <= n;i++){
//如果右指针小于等于左指针就要将右指针移到左指针右边
if(j < i + 1) j = i + 1;
while(j <= n){
cout << "? " << i << " " << j << endl;
string resp;
cin >> resp;
if(resp == "Yes") j++;
else break;
}
int r = j - 1;
ans += (r - i);
}
cout << "! " << ans << endl;
}
int main(){
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
solved();
return 0;
}

D - Placing Rooks#

这题我们直接模拟会超时,这里的方法是通过记录判断每个棋子最终是否会被保留。
我们先用row[i],col[i]row[i], col[i]记录下第ii次操作时的行和列。然后定义last_rowlast\_ rowlast_collast\_ col来分别记录每行每列最后一次被操作是第几次。最后我们再遍历mm次,判断我们在操作中最终能被保留下来的棋子有多少个。
这里的遍历是判断当前次数ii对应行row[i]row[i]和列col[i]col[i]是否是最后一次操作(即last_rowlast\_ rowlast_collast\_ col均为ii),如果是,则说明后续的操作都不会清空这个棋子对应的行和列,这个棋子就会被保留,我们ansans就加一。反之,如果last_rowlast\_ rowlast_collast\_ col有一个不为ii,则说明后续这个棋子会被拿掉。最后我们输出ansans即为结果。

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define endl '\n'
#define mod 998244353
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
void solved() {
int n, m;
cin >> n >> m;
vector<int> row(m + 1), col(m + 1);
vector<int> last_row(n + 1, 0), last_col(n + 1, 0);
for (int i = 1; i <= m; ++i) {
cin >> row[i] >> col[i];
last_row[row[i]] = i;
last_col[col[i]] = i;
}
ll ans = 0;
for (int i = 1; i <= m; ++i) {
if (last_row[row[i]] == i && last_col[col[i]] == i)
ans++;
}
cout << ans << endl;
}
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
int _ = 1;
//cin >> _;
while (_--) {
solved();
}
return 0;
}
分享

如果这篇文章对你有帮助,欢迎分享给更多人!

Atcoder_Beginner_Contest_466(A~D题)
https://mkrari.cn/posts/abc_466/
作者
Mkrari
发布于
2026-07-12
许可协议
CC BY-NC-SA 4.0