[BZOJ1011][HNOI2008]遥远的行星(近似)

这道题我一直懵逼,后来看了题解中的一种玄学方法,看来我还是太弱
这种玄学方法的要点就是利用“只要结果的相对误差不超过5%即可”这一条件。
首先,定义一个常数 T ,在100左右。
对于第 i 个行星,令x=ai。可以看出,对第 i 个行星有贡献的是[1,x]范围内的行星。
xT 时,暴力统计。
x>T 时,把 [1,x] 分成 T 个长度尽可能平均的小区间(小区间过大会使误差变大)。
每一个小区间[l,r]的贡献的近似值就是 (Mirj=lMj)/(il+r2) (取中点作为分母)
由于 0.01<a0.35 得到,这个近似值与准确值的相对误差不超过5%。
代码:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
inline int read() {
    int res = 0; bool bo = 0; char c;
    while (((c = getchar()) < '0' || c > '9') && c != '-');
    if (c == '-') bo = 1; else res = c - 48;
    while ((c = getchar()) >= '0' && c <= '9')
        res = (res << 3) + (res << 1) + (c - 48);
    return bo ? ~res + 1 : res;
}
const double eps = 1e-8;
const int N = 1e5 + 5;
int n; double a, m[N], res[N], sum[N];
int main() {
    int i, j; n = read(); scanf("%lf", &a);
    for (i = 1; i <= n; i++) sum[i] = sum[i - 1] + (m[i] = read());
    for (i = 1; i <= n; i++) {
        int x = 1.0 * i * a + eps;
        if (!x) continue;
        if (x <= 100) for (j = 1; j <= x; j++)
            res[i] += m[i] * m[j] / (i - j);
        else {
            int tx = 1, ty, tz = x / 100, ta = x % 100;
            for (j = 1; j <= 100; j++) {
                ty = tx + tz - (j > ta);
                res[i] += (sum[ty] - sum[tx - 1]) * m[i] / (i - (tx + ty) / 2);
                tx = ty + 1;
            }
        }
    }
    for (i = 1; i <= n; i++) printf("%.6lf\n", res[i]);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值