题目链接
这道题的数据是真的大,如果你的得分是88分,有可能就需要对精度做一个优化了!
首先,考虑一点:
上下两式子相等。
所以,不需要考虑划分的先后顺序问题,最后的值只跟点的位置有关。
所以,dp方程就自己推了,我只是记录一下我错的那个点的问题:
考虑到值可能很大,所以很容易想到的是开一个long double来进行维护,但是可能想象不到的是,我们对于原本是整数的,可能用long long先存下来会更好一些。
然后,在求斜率的过程中,我们将整数部分用long long 记录就可以拿满分了。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <unordered_map>
#include <unordered_set>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define pii pair<int, int>
#define MP(a, b) make_pair(a, b)
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int maxN = 1e5 + 7;
int N, K, op, fa[maxN][205] = {0};
ll a[maxN], s[maxN], dp[maxN][2] = {0};
inline long double Y(int i) { return 1. * (s[i] * s[i] - dp[i][op]); }
inline long double X(int i) { return 1. * s[i]; }
inline long double Slope(int i, int j)
{
if(s[i] == s[j]) return Y(j) > Y(i) ? (long double)1e18 : (long double)-1e18;
ll dy = (s[j] * s[j] - dp[j][op]) - (s[i] * s[i] - dp[i][op]), dx = (s[j] - s[i]);
return 1. * dy / dx;
}
int que[maxN], top, tail;
int main()
{
scanf("%d%d", &N, &K);
for(int i=1; i<=N; i++) { scanf("%lld", &a[i]); s[i] = s[i - 1] + a[i]; }
op = 0;
for(int tim = 1; tim <= K; tim ++ , op ^= 1)
{
top = tail = 0;
que[tail++] = tim;
for(int i = tim + 1, j; i <= N; i ++)
{
long double kk = 1. * s[i];
while(tail - top >= 2 && Slope(que[top], que[top + 1]) <= kk) top++;
j = que[top];
fa[i][tim] = j;
dp[i][op ^ 1] = s[i] * s[j] - s[j] * s[j] + dp[j][op];
while(tail - top >= 2 && Slope(que[tail - 1], i) <= Slope(que[tail - 2], que[tail - 1])) tail--;
que[tail++] = i;
}
}
printf("%lld\n", dp[N][op]);
vector<int> ans;
int now = N;
while(K >= 1)
{
ans.push_back(fa[now][K]);
now = fa[now][K];
K--;
}
while(!ans.empty())
{
printf("%d", ans.back());
ans.pop_back();
printf(ans.empty() ? "\n" : " ");
}
return 0;
}