635 字
2 分钟
Atcoder_Beginner_Contest_466(A~D题)

题目链接:https://atcoder.jp/contests/abc466/tasks
A - Compromise
我们直接判断是否存在大于等于的数,存在即说明幸福值可能为正数,输出,否则输出。
#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
我们用一个二维数组来存储对应种的大小的球有哪些。然后遍历判断对应的颜色是否存在,存在则设置为真,然后遍历中球的大小找出最大值输出。否则则为假,直接输出。
#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
交互式问题,我们需要根据系统给出的输入来计算输出正确结果。这题我们需要用一个双指针来解决。我们每次可以询问两个点之间的距离是否大于,然后系统会给出或的答案。
我们用一个右指针来记录满足条件的区间的右端点。然后开始逐个判断左端点对应的最大的右端点是多少。也就是不断输出”? i j”,如果输入是,那么我们就继续右移右端点,直到系统输入,则说明当前的和的距离已经超过了,那么我们就计入贡献,这个区间内共有个合法的答案。因为两个指针一共最多移动次,所以不会超过次询问就能得到答案,可以通过本题。
注意本题要求每次询问都需要刷新标准输出,也就是每次输出都要使用。
#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
这题我们直接模拟会超时,这里的方法是通过记录判断每个棋子最终是否会被保留。
我们先用记录下第次操作时的行和列。然后定义和来分别记录每行每列最后一次被操作是第几次。最后我们再遍历次,判断我们在操作中最终能被保留下来的棋子有多少个。
这里的遍历是判断当前次数对应行和列是否是最后一次操作(即和均为),如果是,则说明后续的操作都不会清空这个棋子对应的行和列,这个棋子就会被保留,我们就加一。反之,如果和有一个不为,则说明后续这个棋子会被拿掉。最后我们输出即为结果。
#include <bits/stdc++.h>
using namespace std;using ll = long long;using ull = unsigned long long;#define endl '\n'#define mod 998244353typedef 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/