https://www.luogu.org/problemnew/show/P3648

同bzoj3675

这题斜率优化+滚动数组就可以了qwq

因为我是在飞机上瞎bb的式子,所以可能会和别的题解的式子不一样(反正 A 题了对吧)

推的过程什么的都写在最底下的注释里了,大家可以选择先查看最底下的过程(您是神犇可以不用看这篇博客)

但这篇题解不知为什么在bzoj上过不了,请dalao们赐教

#include <bits/stdc++.h>
using namespace std; template <typename T>
inline void read(T &f) {
f = 0; T fu = 1; char c = getchar();
while (c < '0' || c > '9') {if (c == '-') fu = -1; c = getchar();}
while (c >= '0' && c <= '9') {f = (f << 3) + (f << 1) + (c & 15); c = getchar();}
f *= fu;
} typedef long long ll;
const int N = 100000 + 10;
const int M = 200 + 10; ll f[2][N], s[N], a[N];
int fr[M][N], q[N], ans[N], head, tail;
int n, m; bool pd(int a, int b, int c, int d) {
// g(b, c) >= s[n] - s[d]
// (f[b] - f[c]) / (s[b] - s[c]) >= s[n] - s[d]
if(f[a][b] - f[a][c] <= (s[b] - s[c]) * (s[n] - s[d])) return 1;
return 0;
} bool pd2(int a, int b, int c, int d) {
// g(b, c) < g(c, d)
// (f[b] - f[c]) / (s[b] - s[c]) < (f[c] - f[d]) / (s[c] - s[d])
// (f[b] - f[c]) * (s[c] - s[d]) < (f[c] - f[d]) * (s[b] - s[c])
if((f[a][b] - f[a][c]) * (s[c] - s[d]) < (f[a][c] - f[a][d]) * (s[b] - s[c])) return 1;
return 0;
} int main() {
read(n); read(m);
for(int i = 1; i <= n; i++) {
read(a[i]);
s[i] = s[i - 1] + a[i];
}
for(int i = 1; i <= m; i++) {
head = 0, tail = 0; q[0] = i - 1;
for(int j = i; j <= n; j++) {
// 保证队列里有两个元素
while(head + 1 <= tail) {
if(pd((i & 1) ^ 1, q[head], q[head + 1], j)) head++;
else break;
}
f[i & 1][j] = f[(i & 1) ^ 1][q[head]] + (s[n] - s[j]) * (s[j] - s[q[head]]);
fr[i][j] = q[head];
while(head + 1 <= tail) {
if(pd2((i & 1) ^ 1, q[tail - 1], q[tail], j)) tail--;
else break;
}
q[++tail] = j;
}
}
ll maxn = -1; int num;
for(int i = n; i >= m; i--) if(f[m & 1][i] > maxn) maxn = f[m & 1][i], num = i;
printf("%lld\n", maxn);
for(int i = m; i >= 1; i--) {
ans[i] = num;
num = fr[i][num];
}
for(int i = 1; i < m; i++) printf("%d ", ans[i]);
printf("%d\n", ans[m]);
return 0;
} // f[i] 表示取到了第 i 个数获得的最大价值
// f[i] = f[j] + (s[n] - s[i]) * (s[i] - s[j])
// f[i] = f[j] + s[n] * s[i] - s[n] * s[j] - s[i] * s[i] + s[i] * s[j]
// 假设 j 比 k 优, j < k
// f[j] + s[n] * s[i] - s[n] * s[j] - s[i] * s[i] + s[i] * s[j] > f[k] + s[n] * s[i] - s[n] * s[k] - s[i] * s[i] + s[i] * s[k]
// f[j] - s[n] * s[j] + s[i] * s[j] > f[k] - s[n] * s[k] + s[i] * s[k]
// f[j] - s[j] * (s[n] - s[i]) > f[k] - s[k] * (s[n] - s[i])
// f[j] - f[k] > (s[j] - s[k]) * (s[n] - s[i])
// (f[j] - f[k]) / (s[j] - s[k]) < s[n] - s[i]
// 令 g(i, j) 表示 i 和 j 的斜率
// i < j < k
// if g(i, j) < g(j, k)
// if g(i, j) < s[n] - s[x] j 比 i 差
// if g(i, j) >= s[n] - s[x] i 比 j 差, j 比 k 差
// 所以 j 就被扔掉了 QAQ
// 令 a, b 为队首两点
// if g(a, b) >= s[n] - s[i]
// a 比 b 差
// 当 i 上升时, s[n] - s[i] 单调不增
// a 永远比 b 差
// 飞机上没有博客只能自己推式子好痛苦啊 QAQ

最新文章

  1. Windows 10 密钥分享
  2. 平衡二叉树AVL
  3. Linux驱动学习之常用的模块操作命令
  4. 【分块打表】bzoj3798 特殊的质数
  5. Gartner报告:多数CIO还未对数字化做好准备
  6. ED/EP系列7《指令速查表》
  7. 李洪强iOS开发之下载
  8. JavaScript 对象JavaScript 对象
  9. FLAnimatedImage -ios gif图片加载框架介绍
  10. 复位windows网络参数的方法
  11. python 元祖字典集合
  12. LeetCode9 回文数
  13. 课外知识----ini
  14. 网页前端 html js 相关
  15. centos7.2环境nginx+mysql+php-fpm+svn配置walle自动化部署系统详解
  16. linux有趣的命令screen
  17. You Don&#39;t Know JS: this &amp; Object Prototypes( 第4章 Mixing &quot;Class&quot; Objects)
  18. windform 重绘Treeview &quot;+-&quot;号图标
  19. 阅读&lt;构建之法&gt;第三10、11、12章并提出问题
  20. MYSQL 慢日志

热门文章

  1. 使用springboot写一个简单的测试用例
  2. curl 命令参数
  3. Mysql问题随记
  4. random和os模块
  5. 【光速使用开源框架系列】数据库框架OrmLite
  6. solr第二天 京东案例
  7. CMake代码示例
  8. c语言实践 1/1-1/2+1/3-1/4+...
  9. 25.AVG 函数
  10. lambda,map,filter,reduce