1523 字
8 分钟
AtCoder Beginner Contest 431
2025-11-08 22:39
2025-11-10 01:12

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 HH grams, and the weight of the body part is BB 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#

  • 1H1001\le H\le100
  • 1B1001\le B\le100
  • All input values are integers.

Input#

The input is given from Standard Input in the following format:

HH BB

Output#

Print the answer.


思路#

本题只需要判断 HH 是否小于等于 BB 即可, 小于等于输出 00 ,反之输出 HBH-B 。 最基本的分支结构练习,没什么好说的。

代码#

#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 XX. This robot has NN types of parts that can be attached simultaneously: type 1,1, type 2,,2,\ldots, type NN. The weight of the type i (1iN)i\ (1\le i\le N) part is WiW _ i. Initially, none of the NN types of parts are attached to the robot.

Process the following QQ queries in order. The ii-th query (1iQ)(1\le i\le Q) is represented by an integer PiP _ i and is as follows:

  • If the type PiP _ i 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#

  • 1X1001\le X\le100
  • 1N1001\le N\le100
  • 1Wi100 (1iN)1\le W _ i\le100\ (1\le i\le N)
  • 1Q1001\le Q\le100
  • 1PiN (1iQ)1\le P _ i\le N\ (1\le i\le Q)
  • All input values are integers.

Input#

The input is given from Standard Input in the following format:

XX
NN
W1W _ 1 W2W _ 2 \ldots WNW _ N
QQ
P1P _ 1
P2P _ 2
\vdots
PQP _ Q

Output#

Output QQ lines. The ii-th line (1iQ)(1\le i\le Q) should contain the result of processing the ii-th query.


思路#

将第 ii 个零件是否安装在机器人上用逻辑 truefalse 表示, 不难发现每次 PP 操作就是在对这个零件进行 xor 操作。

所以只需要单开一个布尔数组用以记录零件是否被安装, 然后每次操作根据被操作零件当前状态对总重量进行加减, 最后对应布尔变量执行异或操作即可。

需要注意的是机器人初始重量为 XX

当然像我一样直接开一个 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 NN head parts and MM body parts. The weight of the ii-th (1iN)(1\le i\le N) head part is HiH _ i grams, and the weight of the ii-th (1iM)(1\le i\le M) body part is BiB _ i grams.

He wants to create a total of KK 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#

  • 1N2×1051\le N\le2\times10 ^ 5
  • 1M2×1051\le M\le2\times10 ^ 5
  • 1Kmin{N,M}1\le K\le\min\lbrace N,M\rbrace
  • 1Hi109 (1iN)1\le H _ i\le10 ^ 9\ (1\le i\le N)
  • 1Bi109 (1iM)1\le B _ i\le10 ^ 9\ (1\le i\le M)
  • All input values are integers.

Input#

The input is given from Standard Input in the following format:

NN MM KK
H1H _ 1 H2H _ 2 \ldots HNH _ N
B1B _ 1 B2B _ 2 \ldots BMB _ M

Output#

Print Yes if Takahashi can combine the parts well to create KK robots that do not fall over; otherwise, print No.

思路#

题目是要在数列 {HN}\{H_N\}{BM}\{B_M\} 中找到 KK 个完全不同的二元对 HiBjH_i\leq B_j, 所以只需考虑最小的 KKHH 和最大的 KKBB 是否可以配对即可。

最开始在敲代码的时候犯了“田忌赛马式”的错误,用最小的 HH 去和最大的 BB 配对。 需要注意的是在保留上文提及的 KK 个元素后,应当以第 ii 小的 HH 和第 ii 小的 BB 配对。

代码#

#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 NN types of parts that can be attached simultaneously: type 1,1, type 2,,2,\ldots, type NN. The weight of the type i (1iN)i\ (1\le i\le N) part is WiW _ i. Each part has a different happiness when attached to the head versus when attached to the body. The happiness when the type i (1iN)i\ (1\le i\le N) part is attached to the head is HiH _ i, and the happiness when attached to the body is BiB _ i.

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 NN 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#

  • 1N5001\le N\le500
  • 1Wi500 (1iN)1\le W _ i\le500\ (1\le i\le N)
  • 1Hi109 (1iN)1\le H _ i\le10 ^ 9\ (1\le i\le N)
  • 1Bi109 (1iN)1\le B _ i\le10 ^ 9\ (1\le i\le N)
  • All input values are integers.

Input#

The input is given from Standard Input in the following format:

NN
W1W _ 1 H1H _ 1 B1B _ 1
W2W _ 2 H2H _ 2 B2B _ 2
\vdots
WNW _ N HNH _ N BNB _ N

Output#

Print the maximum possible sum of the happiness of all parts when the parts are attached without causing the robot to fall over.

思路#

不妨先让所有零件组成身体,然后从中选取一部分零件组成头部, 组成头部零件的总重量要小于等于身体的总重量。

于是题目可以转化为总容纳质量为 i=1NW2\frac{\sum_{i=1}^{N} W}{2} , 每个物品价值为 HiBiH_i-B_i 的 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

AtCoder Beginner Contest 431
https://chiyoyuki.uk/posts/2025110822/
作者
千代有希=>
发布于
2025-11-08 22:39
许可协议
CC BY-NC-SA 4.0
Comment seems to stuck. Try to refresh?✨