第十次训练课总结(2025.7.17)
Wonderful_Anser · · 个人记录
P1887 乘积最大3
题目分析
给定
输出要求
输出字典序最小的一种方案。
解题思路
要求乘积最大,当然要尽可能接近,最多差1最多差1最多差1(重要)。
我们可以先把
注意
字典序是从小到大!
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
int y = n % m;
int x = n / m;
for (int i = 1; i <= m - y; i++)
cout << x << ' ';
for (int i = 1; i <= y; i++)
cout << x + 1 << ' ';
return 0;
}
P12176 [蓝桥杯 2025 省 Python B] 书架还原
题目分析
有
要注意的点
他可以挑选书架上任意两本书,交换它们的位置。
数据很重要。
这代表如果我们使用
解题思路
与冒泡排序相似,每次找到当前未排好序的最小值,与自己值的位置上的值互换位置。
如果找最小值位置的操作每次都遍历数组一遍,时间明显挡不住。因此进行预处理,不仅记录每个位置的值,还记录每个值的位置。
这样的话,每次进行交换操作的时候,两种记录方式的值都要改变。
代码
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
int a[maxn], m[maxn];
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
m[a[i]] = i;
}
int ans = 0;
for (int i = 1; i <= n; i++)
if (a[i] != i) {
m[a[i]] = m[i];
swap(a[i], a[m[i]]);
ans++;
}
cout << ans;
return 0;
}
B4006 [GESP202406 四级] 宝箱
题目分析
给定
要求计算出小杨能够带走宝箱的总价值最大是多少。
思路
发现我们的物品的价值取值范围在0 ~ 1000之间
那么我们可以考虑枚举
我们就可获取所有价值在
实现方法
用一个桶统计每个价值的物品她的总价值和是多少,因为如果我选择了一个价值为a的物品,那么所有价值为a的物品我会全部拿掉
代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int a[1005], sum[1005];
int main() {
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
a[x] += x;
}
for (int i = 1; i <= 1000; i++)
sum[i] = sum[i - 1] + a[i];
int ans = 0;
for (int x = 1; x <= 1000; x++) {
int y = max(1, x - k);
ans = max(sum[x] - sum[y - 1], ans);
}
cout << ans;
return 0;
}
P1454 圣诞夜的极光
题目分析
给定一个
注意
·曼哈顿距离在2以内算同一个整体
·是计算 #的,不是-
思路
使用bfs求连通块
在这里我们的联通与之前不一样
这里是只要曼哈顿距离在2以内,那么就可以联通
因此(x, y)联通的点有
(x - 1, y), (x - 2, y), (x + 1, y), (x + 2, y), (x, y - 1), (x, y - 2), (x, y + 1), (x, y + 2)
(x - 1, y - 1), (x - 1, y + 1), (x + 1, y + 1), (x + 1, y - 1)
是一个十二联通
代码
#include<bits/stdc++.h>
using namespace std;
char a[101][101];
int b[101][101], n, m, d[12][2] = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}, {-2, 0}, {0, -2}, {0, 2}, {2, 0}};
struct f {
int x, y;
};
void bfs(int x, int y) {
queue <f> q;
q.push({x, y});
b[x][y] = 1;
while (!q.empty()) {
f t = q.front();
for (int k = 0; k < 12; k++) {
int nx = t.x + d[k][0], ny = t.y + d[k][1];
if (nx < 1 || nx > n || ny < 1 || ny > m)
continue;
if (a[nx][ny] == '#' && b[nx][ny] == 0) {
b[nx][ny] = 1;
q.push({nx, ny});
}
}
q.pop();
}
}
int main() {
int ans = 0;
cin >> n >> m;
for (int i = 1; i <= n; i++)
scanf("%s", a[i] + 1);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (a[i][j] == '#' && !b[i][j])
bfs(i, j), ans++;
cout << ans;
return 0;
}
P2655 2038年问题
题目分析
题目给出是哪一年的那一个月的哪一天的哪一个小时的哪一秒开始计算
要求计算最多存储到哪一天的哪一秒
解题思路
c语言用来存储时间的二进制变量一共有k位
那么能表示的最大值 = (1 << k - 1) - 1
因为最高位为符号位,因此能表示的最大值是
能记录从开始时间一共过去
代码步骤
1、总共有
2、计算出我一共过去了多少分钟
3、计算出我一共过去了多少个小时
4、计算出我一共过去了多少天
.
.
代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int m[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
ll jz, ye, mo, da, ho, mi, se;
void c() {
if (ye % 400 == 0) m[2] = 29;
else if (ye % 100 != 0 && ye % 4 == 0) m[2] = 29;
else m[2] = 28;
}
int main() {
int t;
cin >> t;
while (t--) {
cin >> jz >> ye >> mo >> da >> ho >> mi >> se;
ll tot = (1ll << (jz - 1)) - 1;
se += tot;
mi += se / 60, se %= 60;
ho += mi / 60, mi %= 60;
da += ho / 24, ho %= 24;
if (mo == 2)
c();
while (da > m[mo]) {
da -= m[mo];
mo++;
if (mo > 12)
ye++, mo = 1;
if(mo == 2)
c();
}
cout << ye << ' ' << mo << ' ' << da << ' ' << ho << ' ' << mi << ' ' << se << '\n';
}
return 0;
}