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 ...
随机推荐
- 【gp数据库】OLTP和OLAP区别详解
原来一直使用Oracle,新公司使用greenplum后发现系统的并发性差很多,后来才了解因为Oracle属于OLTP类型,而gp数据库属于OLAP类型的.具体了解如下: 数据库系统一般分为两种类型, ...
- jQuery 中bind(),live(),delegate(),on() 区别
on()来改写通过 .bind(), .live(), .delegate()所注册的事件 /* The jQuery .bind(), .live(), and .delegate() method ...
- swift版的GCD封装
swift版的GCD封装 说明 本人针对swift封装了GCD,包括GCDQueue,GCDGroup,GCDTimer以及GCDSemaphore,使用较为便利. 源码 https://github ...
- [翻译] CotEditor
CotEditor https://github.com/coteditor/CotEditor CotEditor is a lightweight plain-text editor for OS ...
- UIImageView的animationImages动画
UIImageView的animationImages动画 UIImageView的animationImages,只有在做非常规动画的时候才有优势,比方说下图中左侧动画.如果用来做下图中的右侧动画, ...
- 铁乐学python_day05-作业
1,有如下变量(tu是个元祖),请实现要求的功能 tu = ("alex", [11, 22, {"k1": 'v1', "k2": [&q ...
- centos7.4 nfs-2.3.2
http://www.linuxfromscratch.org/blfs/view/svn/basicnet/libtirpc.html 注释:安装环境centos7.4; 安装完软件成后会升级系 ...
- 【Alpha】Daily Scrum Meeting——blog2
团队成员 吴吉键 201421122007(组长) 魏修祺 201421122008 孙劲林 201421122022 1. 会议当天照片 忘记拍了!(没有拍照片的习惯,没有第四人拍照) 2. 每个人 ...
- Java 持久化发展历程
- 【2016蓝桥杯省赛】试题C++ B组试题
一. 煤球数目 作答:171700 #include <iostream> using namespace std; int main() { ,x=; ;i<=;i++){ ...