2022.5.5 AcWing每日一题
签到 细节处理
答案只可能在 n - 2 个中间节点和 n - 1 个中间节点的中点之间,所以直接枚举这些结果是否条件即可。
但是需要考虑到中点可能是小数,注意 int 和 double 类型的转换。
#include <bits/stdc++.h>
using namespace std;
// 签到
// 不允许在端点处进行折叠
// 至少要保证选取位置的左右两个最近断定能对上
// 所以,答案一定在 n-2 个中间节点和 n-1 个节点中点之间。
const int N = 1e4 + 10;
int n, l;
vector<double> node;
int st[N];
int ans = 0;
vector<double> check_area;
bool check(double x) {
// cout << "X:" << x << endl;
for (int i = 0; i < node.size(); i++) {
if (2 * x < node[i])
break;
if (2 * x > node[i] + l)
continue;
if (!st[(int)(2 * x - node[i])]) {
return false;
}
}
return true;
}
int main() {
scanf("%d %d", &n, &l);
for (int i = 1; i <= n; i++) {
double tmp;
scanf("%lf", &tmp);
node.push_back(tmp);
st[(int)tmp] = 1;
}
sort(node.begin(), node.end());
for (int i = 0; i < node.size(); i++) {
if (i != 0 && i != node.size() - 1) {
check_area.push_back(node[i]);
}
if (i != 0) {
check_area.push_back((node[i] + node[i - 1]) / 2.0);
}
}
// for (int i = 0; i < check_area.size(); i++) {
// cout << check_area[i] << endl;
// }
for (int i = 0; i < check_area.size(); i++) {
if (check(check_area[i])) {
ans++;
}
}
printf("%d\n", ans);
return 0;
}
本文作者:superPG
本文链接:https://www.cnblogs.com/superPG/p/16224576.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步