[luoguP10217/联合省选 2024] 季风
题意
给定 和 个整数 。
找到最小的非负整数 ,使得存在 个实数 满足以下条件,或报告不存在这样的 :
- ;
- ;
- 。
特别地, 时,认为 和 均为 。
sol
第三条限制等价于 ,也就是
注意到可以拆分为不同周期:设 ,变为 ,可以枚举每一个 ,解这个绝对值不等式,求出最小的 的正整数解,取最小的 即为答案。
代码
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long LL;
const int N = 100005;
const LL INF = 9e18;
const double eps = 1e-8;
LL sx[N], sy[N];
int n, k, X, Y;
int T;
double l, r;
int dx[] = {1, 1, -1, -1}, dy[] = {1, -1, 1, -1};
void calc_range(double x, double y){
l = max(l, x), r = min(r, y);
}
int main(){
scanf("%d", &T);
for (int cs = 1; cs <= T; cs ++ ) {
scanf("%d%d%d%d", &n, &k, &X, &Y);
for (int i = 1; i <= n; i ++ ) {
scanf("%lld%lld", &sx[i], &sy[i]);
sx[i] += sx[i - 1], sy[i] += sy[i - 1];
}
LL ans = 9e18;
for (int i = 0; i < n; i ++ ) {
l = -INF, r = INF;
for (int j = 0; j < 4; j ++ ){
LL coeff = dx[j] * sx[n] + dy[j] * sy[n] + (LL) n * k;
LL lft = dx[j] * sx[i] + dy[j] * sy[i] + (LL) i * k;
LL div1 = dx[j] * X + dy[j] * Y - lft;
if (coeff == 0) {
if (div1 <= 0) continue;
calc_range(INF, -INF);
break;
}
else if (coeff < 0) calc_range(-INF, 1.0 * div1 / coeff);
else calc_range(ceil(1.0 * div1 / coeff), INF);
}
calc_range(0, INF);
if (l - r > eps) continue;
ans = min(ans, (LL) ceil(l) * n + i);
}
printf("%lld\n", ans >= INF ? -1 : ans);
}
return 0;
}
蒟蒻犯的若至错误
- 计算时直接取整数,导致出现两个非 解时取到不合法解
- eps 过大
分类:
题解 / 2024赛时
标签:
数学
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!