第十二次训练课总结(2025.7.19)
Wonderful_Anser · · 个人记录
B2066 救援
题目分析
模拟,直接按照题目编写程序
思路
首先,大本营距离每个房顶的距离就是
注意
-
这个
x 坐标和y 坐标不一定是整数!否则只能得40 分(x 和y 要开double ) -
输出的
ceil() 函数外面要加上int() ,也就是int(ceil(结果)) ,因为ceil() 是double 类型,容易输出小数点、-n.e+5 等不能输出的东西,所以要强转成int ,不然只有40 分
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
double ans = 0;
for (int i = 1; i <= n; i++) {
double x, y; //注意是 double 类型
int cnt;
cin >> x >> y >> cnt;
double a = sqrt(x * x + y * y);
ans += a / 50 + cnt + a / 50 + cnt * 0.5; //船往返的时间总和
}
cout << int(ceil(ans)); //向上取整函输出
return 0;
}
P7584 [COCI 2012/2013 #1] F7
题目分析
题目给定
按照每个回合,第一名会得到
思路
这里列举方案要用递推,不然会爆
我们可以先
但是这样会爆
代码
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 5;
int a[maxn];
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
sort(a + 1, a + 1 + n); //从小到大一遍排序
int ma = 0; //初始化一个很小的数
for (int i = 1; i <= n; i++)
ma = max(a[i] + n - i + 1, ma); //尽可能使最大值最小
int cnt = 0;
for (int i = 1; i <= n; i++)
if (a[i] + n >= ma)
cnt++; //超过最大值,cnt++
cout << cnt; //输出
return 0;
}
P12341 [蓝桥杯 2025 省 A/Python B 第二场] 消消乐
题目分析
题目给定字符串
游戏初始时给出一个长度为
思路
要考虑留下最多的字符,那么要操作最少的次数
而我们想要操作最少的次数,那么越前面的A应该消除最后的BP1747 好奇怪的游戏
P7622 [AHOI2021初中组] 坑
那么越前面的
如果最后的
也就是前面的去匹配后面的
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int l = 0, r = s.size() - 1, c = 0;
while(l < r) {
while (l < r && s[l] != 'A') l++;
while (l < r && s[r] != 'B') r--;
if (l < r) c++, l++, r--;
}
cout << s.size() - 2 * c;
}
P1747 好奇怪的游戏
思路
题意说是可以走田也可以走日,那么就说明有12个方向
int dx[12] = {2, -2, 2, -2, 1, 1, -1, -1, 2, 2, -2, -2}; //横坐标偏移量
int dy[12] = {2, 2, -2, -2, 2, -2, 2, -2, 1, -1, 1, -1}; //纵坐标偏移量
然后就是按照偏移量写
代码
#include <bits/stdc++.h>
using namespace std;
int dx[12] = {2, -2, 2, -2, 1, 1, -1, -1, 2, 2, -2, -2}; //横坐标偏移量
int dy[12] = {2, 2, -2, -2, 2, -2, 2, -2, 1, -1, 1, -1}; //纵坐标偏移量
int vis[45][45]; //存储走到哪个点需要几步
struct f{ //坐标结构体
int x, y;
};
void bfs(int x, int y) { //bfs
for (int i = 1; i <= 40; i++)
for (int j = 1; j <= 40; j++)
vis[i][j] = -1; //初始化(这里设为负一是因为起点只需要0步就能走到,这里如果设为0,就不能分辨这个点走没走过了)
queue <f> q; //定义队列(用处:存储待处理的点,别名:待处理区域)
q.push({x, y}); //将起点加入队列
vis[x][y] = 0; //起点只需要0步就能走到
while (!q.empty()) {
f t = q.front(); //将队列中现在要处理的点的x、y值赋给t
q.pop(); //现在在处理这个点,将这个点移出待处理区域
for (int i = 0; i < 12; i++) {
int nx = t.x + dx[i], ny = t.y + dy[i]; //计算从[t.x][t.y]这个点开始一步可以走到哪些点
if (nx < 1 || nx > 40 || ny < 1 || ny > 40 || vis[nx][ny] != -1) //判断是否要continue
continue;
q.push({nx, ny}); //将这个点加入队列
vis[nx][ny] = vis[t.x][t.y] + 1; //计算这个点要几步走到
}
}
}
int main() {
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2; //输入
bfs(x1, y1); //白马的bfs
cout << vis[1][1] << '\n'; //输出白马要走几步
bfs(x2, y2); //黑马的bfs
cout << vis[1][1]; //输出黑马要走几步
return 0;
}
P7622 [AHOI2021初中组] 坑
思路
1、我们要将所有的跳蚤控制跳到坑中
2、我们每次控制只能控制所有的跳蚤一起往左、或者往右跳一步
3、每只跳蚤要么跳到他左边最近的坑中或者跳到右边最近的坑中
(1)先往右跳一步
(2)往左跳两步
(3)往右跳一步
可以发现我们不可能反复横跳
那么只有四种方案
-
只往右跳
- 如果只往右,那么找出每一只跳蚤右边最近的坑,求出所有跳蚤右边最近的坑的距离的最大值
-
只往左跳
- 如果只往左,那么找出每一只跳蚤往左边最近的坑,然后所有距离左边的坑的最大值就是答案
-
先往右跳,再往左跳
- 先往右其实想让某一些跳蚤跳到右边的坑,剩下的都是跳到左边的坑
-
先往左跳,再往右跳
- 先往左其实想让某一些跳蚤跳到右边的坑,剩下的都是跳到右边的坑
我将所有的跳蚤按照离左边坑的距离从小到大排序
接下来我们可以枚举一下,到哪些跳蚤跳到左边的坑中,那么些跳到右边的坑中
代码
#include <bits/stdc++.h>
#define ll long long
#define inf 1e16
using namespace std;
const int maxn = 2e5 + 5;
ll a[maxn], b[maxn], n, m;
ll f[maxn];
struct node{
ll l, r;
} e[maxn];
bool cmp(node u, node v) {
return u.l < v.l;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= m; i++)
cin >> b[i];
sort(a + 1, a + 1 + n); // 跳蚤
sort(b + 1, b + 1 + m); // 坑
// 二分之前先排序
for (int i = 1; i <= n; i++) {
e[i].l = inf, e[i].r = inf;
int r = upper_bound(b + 1, b + 1 + m, a[i]) - b; // upper_bound() 找的是第一个大于的
if (r != m + 1)
e[i].r = b[r] - a[i];
int l = lower_bound(b + 1, b + 1 + m, a[i]) - b - 1; // lower_bound() 找的是第一个大于等于的
if (l != 0)
e[i].l = a[i] - b[l];
}
sort(e + 1, e + 1 + n, cmp); // 按照离左边坑的距离从小到大排序
for (int i = n; i >= 1; i--)
f[i] = max(f[i + 1], e[i].r);
ll ans = min(f[1], e[n].l); // 只往跳那么只要第n只跳进了,其他都跳进去了,因此只需要跳e[n].l
for (int i = 1; i < n; i++) // 枚举前i只跳到左边,i + 1跳到右边坑
ans = min(min(e[i].l * 2 + f[i + 1], e[i].l + 2 * f[i + 1]), ans);
cout << ans;
return 0;
}