ABC431 赛后总结
WRT_Partisan · · 题解
A - Robot Balance
题目大意
一个机器人有一个头部部件和一个身体部件,头部的重量为
思路
用时
若 我没压成两行就已经很尊重这道题了。
代码
/*
* @FilePath: \c++\ABC431A.cpp
* @Author: WRT_Partisan
* @Date: 2025-11-08 19:56:51
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
int h, b;
signed main()
{
cin.tie(0)->sync_with_stdio(0);
cin >> h >> b;
cout << (h > b ? h - b : 0);
return 0;
}
B - Robot Weight
题目大意
有一个机器人,初始重量为
依次处理以下
- 如果
P_i 型部件当前没有安装在机器人上,则将其安装上;如果已安装,则将其移除。然后,打印机器人当前的重量。
思路
用时
记录每种零件是否安装,若没有安装就装上,否则就拆下来。
代码
/*
* @FilePath: \c++\ABC431B.cpp
* @Author: WRT_Partisan
* @Date: 2025-11-08 19:57:43
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
int x, n, w[105], q, p;
bool book[105];
signed main()
{
cin.tie(0)->sync_with_stdio(0);
cin >> x >> n;
for (int i = 1; i <= n; i++)
cin >> w[i];
cin >> q;
while (q--)
{
cin >> p;
if (book[p])
x -= w[p], book[p] = 0;
else
x += w[p], book[p] = 1;
cout << x << "\n";
}
return 0;
}
C - Robot Factory
题目大意
高桥有
如果头部部件的重量大于身体部件的重量,机器人就会摔倒。高桥希望制造出总共
思路
用时 请不要拷打我)。
将头从小到大排序,对于 multiset(我就因为这个吃了一计罚时)。
代码
/*
* @FilePath: \c++\ABC431C.cpp
* @Author: WRT_Partisan
* @Date: 2025-11-08 19:57:54
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, m, k, h[200005], b;
multiset<int> st;
signed main()
{
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m >> k;
for (int i = 1; i <= n; i++)
cin >> h[i];
for (int i = 1; i <= m; i++)
cin >> b, st.insert(b);
sort(h + 1, h + n + 1);
for (int i = 1; i <= k; i++)
{
auto it = st.lower_bound(h[i]);
if (it == st.end())
cout << "No", exit(0);
st.erase(it);
}
cout << "Yes";
return 0;
}
D - Robot Customize
题目大意
有一种由头部和身体组成的机器人,该机器人可同时连接
如果头部的重量大于身体的重量,机器人就会倒下。这里,头部重量和身体重量分别是头部或身体上各部分重量之和。
高桥希望在机器人上安装所有
思路
用时 请不要拷打我)。
是个很板的 01 背包。
设
状态转移方程:
发现
代码
/*
* @FilePath: \c++\ABC431D.cpp
* @Author: WRT_Partisan
* @Date: 2025-11-08 19:58:02
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, w[505], h[505], b[505], dp[250005], s;
signed main()
{
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; i++)
cin >> w[i] >> h[i] >> b[i], s += w[i];
s /= 2;
for (int i = 1; i <= n; i++)
{
for (int j = s; j >= w[i]; j--)
dp[j] = max(dp[j] + b[i], dp[j - w[i]] + h[i]);
for (int j = w[i] - 1; j >= 0; j--)
dp[j] += b[i];
}
cout << *max_element(dp, dp + s + 1);
return 0;
}
E - Reflection on Grid
题目大意
有一个网格,网格中有
高桥站在
高桥的目标是通过网格中的镜子将手电筒的光线传递给青木。
镜子的位置有三种。当光线照射到镜子上时,光线的方向会根据镜子的位置发生变化。对于每种镜面位置,每个入射方向的出射方向如下图所示。
- 类型 A(未放置反射镜)
- B 型(在连接左上角和右下角的对角线上放置一面镜子)
- C 型(在连接右上角和左下角的对角线上放置一面镜子)
网格上的镜像位置由长度为
高桥可以执行以下任意次数的操作,将光线传送给青木:
- 选择一个单元格,并将该单元格的镜面位置改为不同的类型。
求将光传送给青木所需的最少操作次数。
给你
思路
成功在离比赛结束不到十分钟时通过此题(请不要拷打我)。
首先多测和数据范围就很恶心,只能在循环内定义 vector 来存储,其中的细节也比较多,建议使用辅助数组。
剩下就是最短路了,我用的 Dijkstra,但似乎 0-1bfs 也可以,且复杂度更优。O(能过)
多测不清空,爆零两行泪。
代码
/*
* @FilePath: \c++\ABC431E.cpp
* @Author: WRT_Partisan
* @Date: 2025-11-08 20:41:16
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
constexpr int INF = 1e18;
struct Node
{
int x, y, f, dis;
bool operator<(const Node &a) const { return this->dis > a.dis; }
};
// 0:up,1:right,2:down,3:left
int t, n, m, fx[4] = {-1, 0, 1, 0}, fy[4] = {0, 1, 0, -1}, ans;
int f[3][4] = {{0, 1, 2, 3}, {3, 2, 1, 0}, {1, 0, 3, 2}};
string s;
priority_queue<Node> q;
signed main()
{
cin.tie(0)->sync_with_stdio(0);
cin >> t;
while (t--)
{
while (!q.empty())
q.pop();
ans = INF, cin >> n >> m;
vector<vector<int>> mp(n + 1, vector<int>(m + 1));
vector<vector<vector<int>>> dis(n + 1, vector<vector<int>>(m + 1, vector<int>(4, INF)));
for (int i = 1; i <= n; i++)
{
cin >> s;
for (int j = 0; j < m; j++)
{
if (s[j] == 'A')
mp[i][j + 1] = 0;
else if (s[j] == 'B')
mp[i][j + 1] = 1;
else if (s[j] == 'C')
mp[i][j + 1] = 2;
}
}
dis[1][1][1] = 0, q.push({1, 1, 1, 0});
while (!q.empty())
{
Node t = q.top();
q.pop();
if (t.dis != dis[t.x][t.y][t.f])
continue;
if (t.x == n && t.y == m)
{
if (f[mp[t.x][t.y]][t.f] == 1)
ans = min(ans, t.dis);
else
ans = min(ans, t.dis + 1);
}
for (int i = 0; i < 3; i++)
{
int tmp = (i == mp[t.x][t.y] ? 0 : 1) + t.dis;
int fn = f[i][t.f], nx = t.x + fx[fn], ny = t.y + fy[fn];
if (nx > 0 && ny > 0 && nx <= n && ny <= m && dis[nx][ny][fn] > tmp)
dis[nx][ny][fn] = tmp, q.push({nx, ny, fn, tmp});
}
}
cout << ans << "\n";
}
return 0;
}