标签:font ash php blank pen space gre panel 优化
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 16213 Accepted Submission(s):
4992
#include<cstdio> #include<cstring> using namespace std; const int MAXN = 1e6 + 10, INF = 1e9 + 10; int N, M, a[MAXN], s[MAXN], f[MAXN], q[MAXN]; int X(int x) { return s[x]; } int Y(int x) { return f[x] + s[x] * s[x]; } int check(int x, int y, int i) { return (Y(y) - Y(x)) <= (X(y) - X(x)) * 2 * s[i]; } int check2(int xx1, int yy1, int xx2, int yy2) { return ((Y(yy1) - Y(xx1)) * (X(yy2) - X(xx2))) >= ((Y(yy2) - Y(xx2)) * (X(yy1) - X(xx1))); } /*int slope(int x, int y) { return (Y(y) - Y(x)) / (X(y) - X(x)); }*/ int main() { //freopen("a.in", "r", stdin); while(scanf("%d %d", &N, &M) != EOF) { memset(f, 0, sizeof(f)); for(int i = 1; i <= N; i++) scanf("%d", &a[i]), s[i] = s[i - 1] + a[i]; int h = 0, t = 0; for(int i = 1; i <= N; i++) { // if(h < t) printf("%lf %lf\n", slope(q[h], q[h + 1]), (double)2 * s[i]); while(h < t && check(q[h], q[h + 1], i)) h++; f[i] = (f[q[h]] + (s[i] - s[q[h]]) * (s[i] - s[q[h]]) + M); while(h < t && check2(q[t - 1], q[t], q[t], i)) t--; q[++t] = i; } printf("%d\n", f[N]); } return 0; }
标签:font ash php blank pen space gre panel 优化
原文地址:https://www.cnblogs.com/zwfymqz/p/9161510.html