AtCoder Beginner Contest 431 记录
大概是复健第一场 atc (上次 430 打得太烂被我除名了)
A - Robot Balance
2 sec / 1024 MiB / 100 pts
Problem Statement
Takahashi can combine a head part and a body part to create a robot. A robot falls over if the weight of the head part is greater than the weight of the body part.
Currently, he has one head part and one body part. The weight of the head part is grams, and the weight of the body part is grams.
He wants to make the body part heavier so that the robot does not fall over. Find how many more grams the body part needs to be made heavier so that his robot does not fall over.
Constraints
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Output
Print the answer.
思路
本题只需要判断 是否小于等于 即可, 小于等于输出 ,反之输出 。 最基本的分支结构练习,没什么好说的。
代码
#include<bits/stdc++.h>
using namespace std;
int main() { int h,b; cin >> h >> b; cout << max(0,h-b); cout << '\n'; return 0;}B - Robot Weight
2 sec / 1024 MiB / 200 pts
Problem Statement
There is a robot, and initially the weight of the robot is . This robot has types of parts that can be attached simultaneously: type type type . The weight of the type part is . Initially, none of the types of parts are attached to the robot.
Process the following queries in order. The -th query is represented by an integer and is as follows:
- If the type part is not currently attached to the robot, attach it; if it is attached, remove it. Then, print the current weight of the robot.
Constraints
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Output
Output lines. The -th line should contain the result of processing the -th query.
思路
将第 个零件是否安装在机器人上用逻辑 true 和 false 表示,
不难发现每次 操作就是在对这个零件进行 xor 操作。
所以只需要单开一个布尔数组用以记录零件是否被安装, 然后每次操作根据被操作零件当前状态对总重量进行加减, 最后对应布尔变量执行异或操作即可。
需要注意的是机器人初始重量为 。
当然像我一样直接开一个 pair 也可以。
代码
#include<bits/stdc++.h>
using namespace std;
int main() { int x,n; cin >> x >> n; vector<pair<int,int>> w(n,{0,0}); for(int i = 0; i < n; i++) { cin >> w[i].first; } int q; int p; cin >> q; for(int i = 0; i< q; i++) { cin >> p; p--; if(w[p].second) { x -= w[p].first; } else { x += w[p].first; } w[p].second = !w[p].second; cout << x <<'\n'; } return 0;}C - Robot Factory
2 sec / 1024 MiB / 300 pts
Problem Statement
Takahashi can combine a head part and a body part to create a robot. A robot falls over if the weight of the head part is greater than the weight of the body part.
Currently, he has head parts and body parts. The weight of the -th head part is grams, and the weight of the -th body part is grams.
He wants to create a total of robots that do not fall over by appropriately combining the parts he has. Determine whether he can achieve his goal by combining the parts well.
Here, a part cannot be used to create multiple robots, and two or more head parts (or two or more body parts) cannot be used to create one robot.
Constraints
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Output
Print Yes if Takahashi can combine the parts well to create robots that do not fall over; otherwise, print No.
思路
题目是要在数列 和 中找到 个完全不同的二元对 , 所以只需考虑最小的 个 和最大的 个 是否可以配对即可。
最开始在敲代码的时候犯了“田忌赛马式”的错误,用最小的 去和最大的 配对。 需要注意的是在保留上文提及的 个元素后,应当以第 小的 和第 小的 配对。
代码
#include<bits/stdc++.h>
using namespace std;
int main() { int n,m,k; cin >> n >> m >> k; vector<int> h(n),b(m); for(int i = 0; i < n; i++) { cin >> h[i]; } for(int i = 0; i < m; i++) { cin >> b[i]; } sort(h.begin(),h.end()); sort(b.begin(),b.end()); for(int i = 0; i < k; i++) { if(h[i] > b[m-k+i]) { cout << "No"; return 0; } } cout << "Yes"; return 0;}D - Robot Customize
2 sec / 1024 MiB / 400 pts
Problem Statement
There is a robot consisting of a head and a body. This robot has types of parts that can be attached simultaneously: type type type . The weight of the type part is . Each part has a different happiness when attached to the head versus when attached to the body. The happiness when the type part is attached to the head is , and the happiness when attached to the body is .
The robot falls over if the weight of the head is greater than the weight of the body. Here, the weight of the head and the weight of the body are the sums of the weights of the parts attached to the head or body, respectively.
Takahashi wants to attach all types of parts to the robot, one of each. Find the maximum possible sum of the happiness of all parts when the parts are attached without causing the robot to fall over.
Constraints
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Output
Print the maximum possible sum of the happiness of all parts when the parts are attached without causing the robot to fall over.
思路
不妨先让所有零件组成身体,然后从中选取一部分零件组成头部, 组成头部零件的总重量要小于等于身体的总重量。
于是题目可以转化为总容纳质量为 , 每个物品价值为 的 01 背包问题, dp 求解即可。
写代码的时候忘了 01 背包 dp 怎么写,现去看的 wiki 。
代码
#include<bits/stdc++.h>
using namespace std;
int main() { int n; long long w = 0,happy = 0; cin >> n; vector<pair<int,int>> a(n); for(int i = 0; i<n; i++) { cin >> a[i].first; int h,b; cin >> h >> b; a[i].second = h-b; happy += b; w+=a[i].first; } w >>= 1; vector<long long> dp(w+1); for(int i = 0; i < n;i ++) { for(int j = w; j >= a[i].first; j --) { dp[j] = max(dp[j],dp[j-a[i].first]+a[i].second); } } cout << happy + dp[w]; return 0;}本文封面为作品ATRI -My Dear Moments-中的CG