数据需要加强
查看原帖
数据需要加强
324562
GoodCoder666楼主2024/6/14 20:21

用一些 trick 可以轻松 O(n2)O(n^2) 过:162133631

#include <cstdio>
#define maxn 50005
using namespace std;

using LL = long long;

const LL INF = 1e18l;
inline void setmin(LL& x, const LL& y) { if(y < x) x = y; }
inline LL sqr(const LL& x) { return x * x; }

LL s[maxn], f[maxn];

int main()
{
    int n, l;
    scanf("%d%d", &n, &l);
    for(int i=1; i<=n; i++)
    {
        int x;
        scanf("%d", &x);
        s[i] = s[i - 1] + x + 1;
    }
    l ++;
    for(int i=1; i<=n; i++)
    {
        LL t = s[i] - l, f1 = INF, f2 = INF;
        int j = 0;
        for(; j+1<i; j+=2)
        {
            setmin(f1, f[j] + sqr(t - s[j]));
            setmin(f2, f[j + 1] + sqr(t - s[j + 1]));
        }
        f[i] = f1 < f2? f1: f2;
        if(j + 1 == i)
            setmin(f[i], f[j] + sqr(t - s[j]));
    }
    printf("%lld\n", f[n]);
    return 0;
}
2024/6/14 20:21
加载中...