【总结】 AtCoder Beginner Contest 431
Alternate_ · · 个人记录
前言
AC * 4。最后5分钟死活调不对F,赛后3分钟调对了。
A. Robot Balance
给定两个正整数
a 和b ,求:使b \ge a ,b 要增加的最小值。
思路:
直接作差,注意一下一开始
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned int UINT;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
#define x first
#define y second
int h, b;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> h >> b;
cout << max(h - b, 0) << '\n';
return 0;
}
B. Robot Weight
给定
n 个部件,第i 个部件的重量为w_i 。共q 次操作,每次给定一个编号p ,若第p 个部件未被使用,则将其使用;反之将其拆卸。每次操作后,输出所有使用部件的重量和。
思路:
直接模拟,记录一下每个部件是否被使用即可。时间复杂度
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned int UINT;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
#define x first
#define y second
const int N = 105;
int x; // 初始重量
int n, w[N];
int q;
bool flag[N]; // 记录每个部件是否被使用
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> x >> n;
for (int i = 1; i <= n; i ++ ) cin >> w[i];
cin >> q;
while (q -- )
{
int p; cin >> p;
if (!flag[p])
{
flag[p] = true;
x += w[p];
}
else
{
flag[p] = false;
x -= w[p];
}
cout << x << '\n';
}
return 0;
}
C.Robot Factory
给定两个元素个数为
n 的序列A = [a_1, a_2, \dots, a_n] 和B = [b_1, b_2, \dots, a_n] 。问:能否从中找出k 对元素且每对元素中的b \ge a 。注意:k 对元素不能重叠,即每个元素最多只能被归入一对元素中。 其中,$$
思路:
考虑贪心:将
正确性证明:
对于每一个a 如果与更大的b 归为一对,那么 更大a 可能会找不到匹配的b ,而且也浪费了与a 相匹配的b ,并不会对答案产生更大的贡献,所以该贪心策略必然最优。
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned int UINT;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
#define x first
#define y second
const int N = 2e5 + 5;
int n, m;
int k;
int a[N], b[N];
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m >> k;
for (int i = 1; i <= n; i ++ ) cin >> a[i];
for (int i = 1; i <= m; i ++ ) cin >> b[i];
sort(a + 1, a + n + 1);
sort(b + 1, b + m + 1);
for (int i = 1, j = 1; i <= n && j <= m && k; i ++ , j ++ )
{
while (j <= m && a[i] > b[j]) j ++ ; // 找到第一个大于等于当前 a[i] 的 b[j]。
if (j <= m) k -- ; // 如果找得到,那么对数加1
}
if (k <= 0) cout << "Yes";
else cout << "No";
return 0;
}
D. Robot Customize
给定
n 个部件,第i 个部件的重量为w_i ,其连接到头部的收益为a_i ,连接到身体的收益为b_i 。求:将每个部件都连接到头部或身体,且满足连接到头部的部件总重量 小于等于 连接到身体的部件总重量,可以获得的最大收益。
其中,1 \le n, w_i \le 500 。
思路:
考虑dp。
注意:
-
- 空间复杂度可能过高,要用滚动数组。
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned int UINT;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
#define x first
#define y second
const int N = 505;
int n, w[N];
int a[N], b[N];
LL sumw; // 偏移量
LL dp[2][N * N * 2];
LL res;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i ++ )
{
cin >> w[i] >> a[i] >> b[i];
sumw += w[i];
}
memset(dp[0], -0x7f, sizeof dp[0]);
dp[0][sumw] = 0;
for (int i = 1; i <= n; i ++ )
{
memset(dp[i & 1], -0x7f, sizeof dp[i & 1]);
for (int j = 0; j <= sumw * 2; j ++ )
{
if (j + w[i] <= sumw * 2) dp[i & 1][j + w[i]] = max(dp[i & 1][j + w[i]], dp[i & 1 ^ 1][j] + a[i]);
if (j - w[i] >= 0) dp[i & 1][j - w[i]] = max(dp[i & 1][j - w[i]], dp[i & 1 ^ 1][j] + b[i]);
}
}
for (int j = 0; j <= sumw; j ++ )
res = max(res, dp[n & 1][j]);
cout << res << '\n';
return 0;
}