题意:将n个数分成若干组,每组数字的个数不少于t个,要把每组的数字减小到这组最小值,求所有数字减少的最小值。

先将这n个数从小到大排个序,可以想到一组里面的数一定是排序后相邻的。

设d(i)表示前i个数分完组以后减少的最小值,考虑j~i为一组,则有状态转移方程

还是一样的处理方法,设k < j ≤ i - t,且j~i为一组的值比k~i为一组的值更优。

则有不等式:

化简,把i分离出来,整理成斜率的形式:

写到这里就应该很清楚地能够看出来X和Y的表达式了。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; typedef long long LL; const int maxn = + ; int n, t; LL a[maxn], sum[maxn];
LL d[maxn]; int head, tail;
int Q[maxn]; LL inline Y(int x) { return d[x-] - sum[x-] + a[x] * (x - ); } LL inline DY(int p, int q) { return Y(q) - Y(p); } LL inline DX(int p, int q) { return a[q] - a[p]; } int main()
{
while(scanf("%d%d", &n, &t) == )
{
for(int i = ; i <= n; i++) scanf("%I64d", a + i);
sort(a + , a + + n);
for(int i = ; i <= n; i++) sum[i] = sum[i-] + a[i]; memset(d, , sizeof(d));
for(int i = t; i < * t && i <= n; i++) d[i] = sum[i] - a[] * i; head = tail = ;
Q[tail++] = ;
for(int i = t * ; i <= n; i++)
{
while(head + < tail && DY(Q[tail-], i-t+) * DX(Q[tail-], Q[tail-]) <= DY(Q[tail-], Q[tail-]) * DX(Q[tail-], i-t+)) tail--;
Q[tail++] = i - t + ;
while(head + < tail && DY(Q[head], Q[head+]) <= DX(Q[head], Q[head+]) * i) head++;
d[i] = d[Q[head]-] + sum[i] - sum[Q[head]-] - (i-Q[head]+) * a[Q[head]];
} printf("%I64d\n", d[n]);
} return ;
}

代码君

最新文章

  1. 解决apache 443端口被占用
  2. LeetCode 219 Contains Duplicate II
  3. jquery .attr()
  4. 设计模式总结篇系列:观察者模式(Observer)
  5. sqlserver 视图能否有变量
  6. php安装redis扩展连接redis服务器
  7. iOS开发——多线程OC篇&amp;多线程详解
  8. iOS开发——友盟分享
  9. 让DIV垂直居中的几种办法
  10. shell 变量自增(转)
  11. 【Hexo】Hexo+Github构建个人博客 (三):添加皮肤主题
  12. Tensorflow使用Cmake在Windows下生成VisualStudio工程并编译
  13. 王之泰201771010131《面向对象程序设计(java)》第十七周学习总结
  14. C语言格式化%整理
  15. Liferay7 BPM门户开发之1:Liferay7开发环境准备
  16. google-protobuf安装详解
  17. 分析VoltDB内存数据库
  18. Android使用百度定位API时获取的地址信息为null
  19. 什么是API测试
  20. GO学习笔记 - Go 只有一种循环结构—— for 循环。

热门文章

  1. Elasticsearch之安装
  2. dubbo源码阅读之集群(故障处理策略)
  3. this的三个要点
  4. vue分环境打包配置方法一
  5. SVN中的check out与export的区别
  6. Failed to configure a DataSource: &#39;url&#39; attribute is not specified and no embedded datasource could be configured.Reason: Failed to determine a suitable driver class
  7. Bellman-Ford与SPFA
  8. 树状数组 简单题 cf 961E
  9. hash 散列表
  10. Mysql 5.7在Linux上部署及远程访问