$dp$,斜率优化。

设$dp[i]$表示$1$至$i$位置的最小费用,则$dp[i]=min(dp[j]+s[i]-s[j]-(i-j)*x[j+1])$,$dp[n]$为答案。

然后斜率优化就可以了。

得到了两个教训:

①如果可以从$dp[0]$推过来,那么队列中一开始就压入$0$,不要忘记了。

②$check2$中,要压入哪个位置就判断哪个位置。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<ctime>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c = getchar();
x = ;
while(!isdigit(c)) c = getchar();
while(isdigit(c))
{
x = x * + c - '';
c = getchar();
}
} int n,t;
long long x[],dp[],s[];
int q[],f1,f2; bool delete1(int a,int b,int c)
{
if(dp[b]-s[b]-x[b+]*(c-b)<=dp[a]-s[a]-x[a+]*(c-a)) return ;
return ;
} bool delete2(int a,int b,int c)
{
if(
((dp[c]-s[c]+x[c+]*c)-(dp[b]-s[b]+x[b+]*b))*(x[b+]-x[a+]) <=
((dp[b]-s[b]+x[b+]*b)-(dp[a]-s[a]+x[a+]*a))*(x[c+]-x[b+])
) return ;
return ;
} int main()
{
while(~scanf("%d%d",&n,&t))
{
for(int i=;i<=n;i++) scanf("%lld",&x[i]);
sort(x+,x++n); for(int i=;i<=n;i++) s[i]=s[i-]+x[i]; for(int i=t;i<*t;i++) dp[i]=s[i]-x[]*i; f1=f2=; q[]=; f2++; q[f2]=t; for(int i=*t;i<=n;i++)
{
while()
{
if(f2-f1+<) break;
if(delete1(q[f1],q[f1+],i)) f1++;
else break;
} dp[i]=dp[q[f1]]+s[i]-s[q[f1]]-x[q[f1]+]*(i-q[f1]); while()
{
if(f2-f1+<) break;
if(delete2(q[f2-],q[f2],i-t+)) f2--;
else break;
} f2++; q[f2]=i-t+;
}
printf("%lld\n",dp[n]);
}
return ;
}

最新文章

  1. SQLite学习笔记(十二)&amp;&amp;虚拟机指令
  2. http 报文 - 转
  3. mysql Can&#39;t connect to local MySQL server through socket &#39;/var/lib/mysql/mysql.sock&#39; (2)
  4. Mysql创建函数时报错
  5. 简单3d RPG游戏 之 004 攻击(一)
  6. HDOJ 1879
  7. java报错排解
  8. 字符串的一些常用方法 string
  9. maven介绍
  10. eclipse 运行 mapreduce程序报错 No job jar file set. User classes may not be found. See JobConf(Class) or JobConf#setJar(String).
  11. list与Set、Map区别及适用场景
  12. vue element-ui 实现点击查看审核记录
  13. [转]关于Infobright的数据导入
  14. android之Activity.startManagingCursor方法详解
  15. The user specified as a definer (&amp;#39;root&amp;#39;@&amp;#39;%&amp;#39;) does not exist
  16. CI框架定义判断POST GET AJAX
  17. BZOJ1179_APIO2009_抢掠计划_C++
  18. 让Mac支持lrzsz
  19. Mac OS访问Windows共享文件夹
  20. Python手记

热门文章

  1. 【学习笔记】Learning OpenCV3——Ch8 working with video
  2. http缓存知多少
  3. 自己模拟实现一下Google的赛马Doodle
  4. 使用JMeter进行一次简单的带json数据的post请求测试
  5. 移动端去掉a标签点击时出现的背景
  6. SpringMVC异常报406 (Not Acceptable)的解决办法
  7. 哈希Hash在字符串中的应用_C++
  8. bzoj 1042 DP+容斥原理
  9. bzoj 1001 平面图转对偶图 最短路求图最小割
  10. python常用模块补充hashlib configparser logging,subprocess模块