周五下午J组集训总结
B4350 [信息与未来 2025] 美味水果
题目描述
Dr. X 收到了一份礼物:
- 每天,Dr. X 可以选择吃掉一个水果,并记录下该天吃掉的水果的好吃程度。
- 没有被吃掉的每个水果,好吃程度将在第二天变为
y = \lfloor\sqrt x\rfloor ,即 “开根号取整”:y 是满足y^2 ≤ x 的最大整数。
请计算,在所有可能的吃水果顺序中,Dr. X 最多能获得多少好吃程度的总和。
输入格式
输入包含两行:第一行,一个整数
输出格式
输出一个整数,表示能够获得的好吃程度总和的最大值。
数据范围
对于
对于
错误代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, ans;
int a[1000007];
int read() {
int r = 0, f = 1; char c = getchar();
while ((c < '0' || c > '9') && (c ^ '-')) c = getchar();
if (c == '-') f = -1, c = getchar();
while (c >= '0' && c <= '9') r = r * 10 + c - '0', c = getchar();
return r * f;
}
bool cmp(int x, int y) {
return x > y;
}
signed main() {
n = read();
for (int i = 1; i <= n; i++) a[i] = read();
sort(a + 1, a + 1 + n, cmp);
for (int i = 1; i <= n; i++) {
ans += a[i];
for (int j = i + 1; j <= n; j++) a[j] = sqrt(a[j]);
}
cout << ans;
return 0;
}
错误原因
超时
正确思路
-
首先我们可以运用到贪心把所有水果按从大到小的顺序排序
-
但仅靠贪心肯定会超时, 而我们可以想到一直开根很快就会变成
1 , 那具体是几次呢? -
由于水果的美味度不可能超过
10 ^ 9 , 我们可以来看一下开几次根就会变成1 第一次:
31622 第二次:
177 第三次:
13 第四次:
3 第五次:
1
所以所以水果在第五次时美味度都会变成
正确代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, ans;
int a[1000007];
int read() {
int r = 0, f = 1; char c = getchar();
while ((c < '0' || c > '9') && (c ^ '-')) c = getchar();
if (c == '-') f = -1, c = getchar();
while (c >= '0' && c <= '9') r = r * 10 + c - '0', c = getchar();
return r * f;
}
bool cmp(int x, int y) {
return x > y;
}
signed main() {
n = read();
for (int i = 1; i <= n; i++) a[i] = read();
sort(a + 1, a + 1 + n, cmp);
for (int i = 1; i <= n; i++) {
ans += a[i];
if (i == 5) for (int j = i + 1; j <= n; j++) a[j] = 1;
if (i < 5) for (int j = i + 1; j <= n; j++) a[j] = sqrt(a[j]);
}
cout << ans;
return 0;
}
P13817 「LDOI R3」球球碰撞
题目背景
我以爲走下去是一種默契。
你卻說你需要離開,需要一些空間呼吸。
题目描述
一条一维的无限长轨道上,给定
你需要设置每一颗弹珠的位置和初始方向,使得:
- 初始时任意两颗弹珠不在同一位置。
- 启动装置后,不允许某一时刻,存在
x(x\ge3) 颗弹珠同时在同一位置碰撞。
你需要最大化启动装置后发生的碰撞总数,你只需要求出该值即可,不需要给出具体每个弹珠的位置和初始方向。
启动装置后,若任意两颗弹珠运动到同一位置,即称发生了一次无能量损失的碰撞,这两个弹珠会交换运动方向和速度。
例如,速度为每秒
输入格式
本题有多组测试数据。
第一行,一行一个整数
接下来包含
第一行包含一个正整数
第二行包含
输出格式
对于每组数据:输出一行包含一个非负整数,表示最大的碰撞数。
说明/提示
【数据范围和约定】
本题使用了子任务依赖。
对于所有测试数据,保证:
::cute-table{tuack}
| 特殊性质 | 分值 | 子任务依赖 | |||
|---|---|---|---|---|---|
| 样例 | |||||
| 无 | |||||
| 有 | |||||
| 无 | |||||
| 有 | |||||
| 无 | |||||
| 无 |
特殊性质:所有的
错误代码
时间不够, 去想第
正确思路
-
宏观上看,两颗弹珠碰撞相当于相互穿过对方而不发生碰撞
-
我们先考虑特殊性质:所有的
v_i 互不相同。我们可以发现,该情况下碰撞数最大为
\displaystyle \frac{n(n + 1)}{2} -
可以发现,方向相同,速度大的一定能追上速度小的,于是,我们可以将速度不同的按速度降序排列,并朝向一个方向, 而速度相同的, 就一半一半地朝不同方向于是 x个速度相同的小球会减少
正确代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
int t, n;
int v[1000007];
signed main() {
cin >> t;
while (t--) {
cin >> n;
for (int i = 1; i <= n; i++) cin >> v[i];
sort(v + 1, v + 1 + n);
int ans = n * (n - 1) / 2;
int cnt = 1, last = v[1];
v[n + 1] = -1e9;
for (int i = 2; i <= n + 1; i++) {
if (v[i] == last) cnt++;
else {
int sum = cnt / 2;
ans = ans - (cnt * (cnt - 1) / 2 - sum * (cnt - sum));
cnt = 1;
last = v[i];
}
}
cout << ans << endl;
}
return 0;
}
P1250 种树
题目背景
一条街的一边有几座房子,因为环保原因居民想要在路边种些树。
题目描述
路边的地区被分割成块,并被编号成
每个居民都想在门前种些树,并指定了三个号码
居民们想种树的各自区域可以交叉。你的任务是求出能满足所有要求的最少的树的数量。
输入格式
输入的第一行是一个整数,代表区域的个数
输入的第二行是一个整数,代表房子个数
第
输出格式
输出一行一个整数,代表最少的树木个数。
说明/提示
数据规模与约定
对于
错误代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, h, jin, ans;
bool vis[1000007];
struct node {
int s, t, c;
}a[1000007];
int read() {
int r = 0, f = 1; char c = getchar();
while ((c < '0' || c > '9') && (c ^ '-')) c = getchar();
if (c == '-') f = -1, c = getchar();
while (c >= '0' && c <= '9') r = r * 10 + c - '0', c = getchar();
return r * f;
}
bool cmp(node x, node y) {
return x.t < y.t;
}
signed main() {
n = read(), h = read();
for (int i = 1; i <= h; i++) a[i].s = read(), a[i].t = read(), a[i].c = read();
sort(a + 1, a + 1 + h, cmp);
for (int i = 1; i <= h; i++) {
jin = 0;
for (int j = a[i].s; j <= a[i].t; j++) {
if (vis[j] == true) jin++;
else break;
}
if (jin >= a[i].c) continue;
for (int j = a[i].t; j >= a[i].t - a[i].c + jin; j--) {
if (vis[j] == false) ans++, vis[j] = true;
}
}
cout << ans;
return 0;
}
错误原因
粗心
正确思路
-
我们可以可以用贪心做
-
想要种树种得少,就要一棵树在多个区间同时出现。 所以,在重叠部分种尽可能多的树即可。 然而重叠部分一定在区间的尾部。
-
所以先对
t_i 进行排序,然后依次在区间的尾部从前往后种树直到满足要求,对于下一个区间,看看差多少树,就在结尾补多少。 -
于是贪心的思想就很容易出来了:
- 按结束位置排序
- 对每个区间一次处理, 从前往后扫描区间,统计已有的树的个数
- 若已选点超过要求个数,则
continue - 否则从后往前,添加缺少的覆盖点
- 输出ans
正确代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, h, jin, ans;
bool vis[1000007];
struct node {
int s, t, c;
}a[1000007];
int read() {
int r = 0, f = 1; char c = getchar();
while ((c < '0' || c > '9') && (c ^ '-')) c = getchar();
if (c == '-') f = -1, c = getchar();
while (c >= '0' && c <= '9') r = r * 10 + c - '0', c = getchar();
return r * f;
}
bool cmp(node x, node y) {
return x.t < y.t;
}
signed main() {
n = read(), h = read();
for (int i = 1; i <= h; i++) a[i].s = read(), a[i].t = read(), a[i].c = read();
sort(a + 1, a + 1 + h, cmp);
for (int i = 1; i <= h; i++) {
jin = 0;
for (int j = a[i].s; j <= a[i].t; j++) if (vis[j] == true) jin++;
if (jin >= a[i].c) continue;
for (int j = a[i].t; j >= a[i].s; j--) {
if (vis[j] == false) {
vis[j] = true;
jin++; ans++;
if (jin == a[i].c) break;
}
}
}
cout << ans;
return 0;
}