HDU 3045 Picnic Cows
$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 ;
}
HDU 3045 Picnic Cows的更多相关文章
- hdu 3045 Picnic Cows(斜率优化DP)
题目链接:hdu 3045 Picnic Cows 题意: 有n个奶牛分别有对应的兴趣值,现在对奶牛分组,每组成员不少于t, 在每组中所有的成员兴趣值要减少到一致,问总共最少需要减少的兴趣值是多少. ...
- HDU 3045 Picnic Cows(斜率优化DP)
Picnic Cows Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 3045 - Picnic Cows - [斜率DP]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3045 It’s summer vocation now. After tedious milking, ...
- HDU 3045 picnic cows(斜率DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3045 题目大意:有n个数,可以把n个数分成若干组,每组不得小于m个数,每组的价值=除了该组最小值以外每 ...
- HDU3045 Picnic Cows (斜率DP优化)(数形结合)
转自PomeCat: "DP的斜率优化--对不必要的状态量进行抛弃,对不优的状态量进行搁置,使得在常数时间内找到最优解成为可能.斜率优化依靠的是数形结合的思想,通过将每个阶段和状态的答案反映 ...
- HDU3045 Picnic Cows —— 斜率优化DP
题目链接:https://vjudge.net/problem/HDU-3045 Picnic Cows Time Limit: 8000/4000 MS (Java/Others) Memor ...
- HDU 3045 DP 斜率优化 Picnic Cows
题意:将n个数分成若干组,每组数字的个数不少于t个,要把每组的数字减小到这组最小值,求所有数字减少的最小值. 先将这n个数从小到大排个序,可以想到一组里面的数一定是排序后相邻的. 设d(i)表示前i个 ...
- POJ 2182 / HDU 2711 Lost Cows(平衡树)
Description N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacular di ...
- HDU3045 Picnic Cows
题面 HDU vjudge 题解 将权值排序,则分组一定是连续的 设$f[i]$表示前$i$头牛的最小代价,则($a[i]$为$i$的权值): $$ f[i] = f[j - 1] + sum[i] ...
随机推荐
- css引入特殊字体
http://www.fontsquirrel.com/tools/webfont-generator ttf格式的字体转换成其他格式的字体 css引入特殊字体建议只是用英文字体,中 ...
- [Leetcode] Interger to roman 整数转成罗马数字
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- mybatis学习(七)——resultType解析
resultType是sql映射文件中定义返回值类型,返回值有基本类型,对象类型,List类型,Map类型等.现总结一下再解释 总结: resultType: 1.基本类型 :resultType= ...
- 安卓recyclerview的基本使用
1.先在布局文件中写 <android.support.v7.widget.RecyclerView android:id="@+id/my_recycler_view" a ...
- jquery实现通用结构折叠面板效果
效果截图: 说明:可以任意添加多个类似结构样式,点击标题栏图片对应隐藏.显示. jquery代码: 思路一:基本方法 <script src="http://apps.bdimg.co ...
- 转载:Java中的String与常量池
转载自http://developer.51cto.com/art/201106/266454.htm.感觉总结的不错,自己收藏一下. string是java中的字符串.String类是不可变的,对S ...
- js页面 读身份证
硬件是 神思读卡器, 需要安装这个东西的驱动. 在web页面读取的话, 下载cab的包 进行注册, <OBJECT classid="clsid:F1317711-6BDE-4658- ...
- [洛谷P2127] 序列排序
洛谷题目链接:序列排序 题目描述 小C有一个N个数的整数序列,这个序列的中的数两两不同.小C每次可以交换序列中的任意两个数,代价为这两个数之和.小C希望将整个序列升序排序,问小C需要的最小代价是多少? ...
- Flume的安装,配置及使用
1,上传jar包 2,解压 3,改名 4,更改配置文件 将template文件重镜像 root@Ubuntu-1:/usr/local/apache-flume/conf# cat flume-env ...
- IDEA无法编译java8的lambda表达式提示Error:(16, 48) java: -source 1.5 中不支持 lambda 表达式
在idea中新建了一个java8的项目,但是写lambda表达式提示语法错误,提示如下错误信息: Error:(16, 48) java: -source 1.5 中不支持 lambda 表达式 (请 ...