HDU 3045 Picnic Cows
2024-08-28 00:29:52
$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 ;
}
最新文章
- SQLite学习笔记(十二)&;&;虚拟机指令
- http 报文 - 转
- mysql Can&#39;t connect to local MySQL server through socket &#39;/var/lib/mysql/mysql.sock&#39; (2)
- Mysql创建函数时报错
- 简单3d RPG游戏 之 004 攻击(一)
- HDOJ 1879
- java报错排解
- 字符串的一些常用方法 string
- maven介绍
- eclipse 运行 mapreduce程序报错 No job jar file set. User classes may not be found. See JobConf(Class) or JobConf#setJar(String).
- list与Set、Map区别及适用场景
- vue element-ui 实现点击查看审核记录
- [转]关于Infobright的数据导入
- android之Activity.startManagingCursor方法详解
- The user specified as a definer (&;#39;root&;#39;@&;#39;%&;#39;) does not exist
- CI框架定义判断POST GET AJAX
- BZOJ1179_APIO2009_抢掠计划_C++
- 让Mac支持lrzsz
- Mac OS访问Windows共享文件夹
- Python手记
热门文章
- 【学习笔记】Learning OpenCV3——Ch8 working with video
- http缓存知多少
- 自己模拟实现一下Google的赛马Doodle
- 使用JMeter进行一次简单的带json数据的post请求测试
- 移动端去掉a标签点击时出现的背景
- SpringMVC异常报406 (Not Acceptable)的解决办法
- 哈希Hash在字符串中的应用_C++
- bzoj 1042 DP+容斥原理
- bzoj 1001 平面图转对偶图 最短路求图最小割
- python常用模块补充hashlib configparser logging,subprocess模块