HDU3045 Picnic Cows
题面
题解
将权值排序,则分组一定是连续的
设$f[i]$表示前$i$头牛的最小代价,则($a[i]$为$i$的权值):
$$ f[i] = f[j - 1] + sum[i] - sum[j - 1] - (i - j + 1) * a[j] $$
套上斜率优化的板子即可。
#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
#define RG register
#define file(x) freopen(#x".in", "r", stdin);freopen(#x".out", "w", stdout);
#define clear(x, y) memset(x, y, sizeof(x))
const int maxn(5e5 + 10);
int n, t, q[maxn];
long long sum[maxn], a[maxn], f[maxn];
inline long long x(int i) { return a[i]; }
inline long long y(int i) { return f[i - 1] - sum[i - 1] + (i - 1) * a[i]; }
inline bool check(int i, int j, int k)
{
return (y(i) - y(j)) * (x(j) - x(k)) <= (y(j) - y(k)) * (x(i) - x(j));
}
int main()
{
while(~scanf("%d%d", &n, &t))
{
for(RG int i = 1; i <= n; i++) scanf("%lld", a + i);
std::sort(a + 1, a + n + 1);
for(RG int i = 1; i <= n; i++) sum[i] = sum[i - 1] + a[i];
int S = t << 1, head = 1, tail = 0;
for(RG int i = t; i < S; i++) f[i] = sum[i] - i * a[1];
for(RG int i = S; i <= n; i++)
{
while(head < tail && check(i - t + 1, q[tail], q[tail - 1])) --tail;
q[++tail] = i - t + 1;
while(head < tail && (y(q[head + 1]) - y(q[head]))
<= (x(q[head + 1]) - x(q[head])) * i) ++head;
f[i] = f[q[head] - 1] + sum[i] - sum[q[head] - 1] -
(i - q[head] + 1) * a[q[head]];
}
printf("%lld\n", f[n]);
}
return 0;
}
HDU3045 Picnic Cows的更多相关文章
- HDU3045 Picnic Cows —— 斜率优化DP
题目链接:https://vjudge.net/problem/HDU-3045 Picnic Cows Time Limit: 8000/4000 MS (Java/Others) Memor ...
- HDU3045 Picnic Cows (斜率DP优化)(数形结合)
转自PomeCat: "DP的斜率优化--对不必要的状态量进行抛弃,对不优的状态量进行搁置,使得在常数时间内找到最优解成为可能.斜率优化依靠的是数形结合的思想,通过将每个阶段和状态的答案反映 ...
- 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)
题目链接:hdu 3045 Picnic Cows 题意: 有n个奶牛分别有对应的兴趣值,现在对奶牛分组,每组成员不少于t, 在每组中所有的成员兴趣值要减少到一致,问总共最少需要减少的兴趣值是多少. ...
- 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个数,每组的价值=除了该组最小值以外每 ...
- 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]$为答案. 然后斜率优化就可以 ...
- HDU 3045 DP 斜率优化 Picnic Cows
题意:将n个数分成若干组,每组数字的个数不少于t个,要把每组的数字减小到这组最小值,求所有数字减少的最小值. 先将这n个数从小到大排个序,可以想到一组里面的数一定是排序后相邻的. 设d(i)表示前i个 ...
- POJ2018 Best Cow Fences —— 斜率优化DP
题目链接:https://vjudge.net/problem/POJ-2018 Best Cow Fences Time Limit: 1000MS Memory Limit: 30000K T ...
随机推荐
- 使用Reflector反编译并提取源代码
Reflector是一个强大的.net 反编译工具,有时我们不止需要反编译源代码,更需要提取源代码. Reflector本身不自带提取源代码功能,不过可以借助插件Reflector.FileDisas ...
- vue的项目
vue的项目打开也是非常具有解耦性的 最重要的就是src目录了 我们的入口在main中 main是你的实例化vue app中就是我们的每一块田地是我们的vue实例对这个的操作 ,index因为是 ...
- 使用keychain永久存储数据
使用keychain永久存储数据 https://github.com/soffes/sskeychain keychain当然还是使用开源的好:),keychain是干啥用的?这个,baidu一下你 ...
- WDS使用捕获映像制作企业自定义映像
来源:http://www.07net01.com/linux/WDSshiyongbuhuoyingxiangzhizuoqiyezidingyiyingxiang_545749_137448761 ...
- jquery ztree的案例,附源代码
播客:http://itindex.net/detail/46094-jquery-ztree-%E7%A8%8B%E5%BA%8F 源代码: http://download.csdn.net/d ...
- K8S Deployment 命令
创建 Deployment kubectl create -f https://kubernetes.io/docs/user-guide/nginx-deployment.yaml --record ...
- September 18th 2017 Week 38th Monday
The only person you should try to be better than is the person you were yesterday. 你唯一应该试着去超越的人,是昨天的 ...
- September 16th 2017 Week 37th Saturday
We are all in the gutter, but some of us are looking at the stars. 身处艰难,但仍有人仰望星空. When standing on t ...
- ZT 安卓手机的安全性 prepare for Q
如何增强安卓手机的安全性?安卓的安全性太低了!!! 众所周知,安卓手机是非常容易破解的,刷过机的人都知道,不管你之前在手机怎么设置密码,只要进入recovery清空使用记录,手机就会恢复出厂设置,到时 ...
- django 错误之 OSError: mysql_config not found
pip 导入包时出现如下错误 Complete output from command python setup.py egg_info: /bin/: mysql_config: not found ...