关于本题精度(WA#28)
查看原帖
关于本题精度(WA#28)
324632
Skeleton_Huo楼主2022/9/26 13:20

全开long long和long double会WA#28,只开其中一个是WA#28#29,所以是精度问题?要怎么解决。 代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>

#define int long long
#define double long double

namespace io {
    inline int read() {
        char ch = getchar(); int flag = 1, ans = 0;
        while (ch < '0' || ch > '9') { if (ch == '-') flag = -1; ch = getchar(); }
        while (ch >= '0' && ch <= '9') ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
        return flag * ans;
    }
    inline void write(long long x) {
        if (x < 0) putchar('-'), x = -x;
        if (x > 9) write(x / 10);
        putchar(x % 10 + '0');
    }
}

using namespace std;
using namespace io;

const int N = 100010;

int n, x, p, q, t[N];
double r;

double fmax(double a, double b) {
    if (a > b) return a;
    else return b;
}

signed main() {
    
    n = read(), x = read(), p = read(), q = read();
    r = p * 1.0 / q;

    for (int i = 1; i <= n; i++) t[i] = read();

    int i = 1, days = 1;
    bool flag;
    double sum = 0, times, need, last;
    while (i <= n) {
        times = 0, need = x * r * days, last = x - fmax(0, need - sum), flag = false;
        if (last != x) flag = true;
        while (i <= n && (last > t[i] || last == t[i] && flag)) {
            last -= t[i], times += t[i];
            i++;
        }
        days++;
        need = x * r * days;
        sum += x - times;
        times = 0;
        last = x - fmax(0, need - sum);
        if (last < t[i]) {
            int tmp = ceil((t[i] - last) / (x * (1 - r)));    // 连续睡觉天数
            sum += tmp * x;
            days += tmp;
        }
    }

    write(days - 1);

    // puts("");
    // system("pause");

    return 0;
}
2022/9/26 13:20
加载中...