Codeforces 660F Bear and Bowling 4 斜率优化 (看题解)

Bear and Bowling 4

这也能斜率优化。。。

max[ i ] = a[ i ] - a[ j ] - j * (sum[ i ] - sum[ j ])然后就能斜率优化啦, 我咋没想到, 我好菜啊。

斜率优化最重要的是转换成前缀形式, 我TM又忘了。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long using namespace std; const int N = 2e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); int n, que[N], be = , ed = ;
LL val[N], a[N], sum[N], ans; long double calc(int k, int j) {
return ((a[j] - (long double)j * sum[j]) - (a[k] - (long double)k * sum[k])) / (j - k);
} int main() {
scanf("%d", &n);
for(int i = ; i <= n; i++) {
scanf("%lld", &val[i]);
a[i] = a[i - ] + i * val[i];
sum[i] = sum[i - ] + val[i];
ans = max(ans, a[i]);
}
que[++ed] = ;
for(int i = ; i <= n; i++) {
int low = be, high = ed - , p = -;
while(low <= high) {
int mid = low + high >> ;
if(calc(que[mid], que[mid + ]) < -sum[i]) p = mid, low = mid + ;
else high = mid - ;
}
int j = (p == -) ? que[be] : que[p + ];
ans = max(ans, a[i] -a[j] - j * (sum[i] - sum[j]));
while(ed > be && calc(que[ed-], que[ed]) > calc(que[ed], i)) ed--;
que[++ed] = i;
}
printf("%lld\n", ans);
return ;
} /*
*/
上一篇:【spring-boot】spring aop 面向切面编程初接触--切点表达式


下一篇:FreeSWITCH 1.2.5.3 Step by Step Install