HDU3045 Picnic Cows(斜率优化DP)

             Picnic Cows

                    Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
                          Total Submission(s): 2192    Accepted Submission(s): 675

Problem Description

It’s summer vocation now. After tedious milking, cows are tired and
wish to take a holiday. So Farmer Carolina considers having a picnic
beside the river. But there is a problem, not all the cows consider it’s
a good idea! Some cows like to swim in West Lake, some prefer to have a
dinner in Shangri-la ,and others want to do something different. But in
order to manage expediently, Carolina coerces all cows to have a
picnic!
Farmer Carolina takes her N (1<N≤400000) cows to the
destination, but she finds every cow’s degree of interest in this
activity is so different that they all loss their interests. So she has
to group them to different teams to make sure that every cow can go to a
satisfied team. Considering about the security, she demands that there
must be no less than T(1<T≤N)cows in every team. As every cow has its
own interest degree of this picnic, we measure this interest degree’s
unit as “Moo~”. Cows in the same team should reduce their Moo~ to the
one who has the lowest Moo~ in this team——It’s not a democratical
action! So Carolina wishes to minimize the TOTAL reduced Moo~s and
groups N cows into several teams.
For example, Carolina has 7 cows to
picnic and their Moo~ are ‘8 5 6 2 1 7 6’ and at least 3 cows in every
team. So the best solution is that cow No.2,4,5 in a team (reduce
(2-1)+(5-1) Moo~)and cow No.1,3,6,7 in a team (reduce ((7-6)+(8-6))
Moo~),the answer is 8.

Input

The input contains multiple cases.
For each test case, the first line has two integer N, T indicates the number of cows and amount of Safe-base line.
Following n numbers, describe the Moo~ of N cows , 1st is cow 1 , 2nd is cow 2, and so on.

Output

One
line for each test case, containing one integer means the minimum of
the TOTAL reduced Moo~s to group N cows to several teams.

Sample Input

7 3
8 5 6 2 1 7 6

Sample Output

8

Source

2009 Multi-University Training Contest 14 - Host by ZJNU

【思路】

斜率优化+DP

首先问一句Carolina和John什么关系 ?(;´Д`?)

不难设计出转移方程为:

f[i]=min{ f[j]+C[i]-C[j]+(i-j)*X[j+1] } T<=j<=i-T

其中C表示X的前缀和。

如果j>k且决策j优于决策k则有

f[j]-f[k]+C[k]-C[j]-k*X[k+1]+j*X[j+1]>i*(X[j+1]-X[k+1])

维护指定区间内的下凸包即可。

需要注意的是输入输出用int64,而且斜率部分不能用之前直接除的写法了,因为X有long long,可能误差会比较大,改用化除为乘的方法。

   坑了我好长时间 T_T

【代码】

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<iostream>
 4 #include<algorithm>
 5 using namespace std;
 6
 7 typedef long long LL;
 8 typedef long double LD;
 9 const int N = 500000+10;
10
11 int L,R,n,T,q[N]; LL A[N],C[N],f[N];
12 LL UP(int j,int k) {
13     return f[j]-C[j]+j*A[j+1]-(f[k]-C[k]+k*A[k+1]);
14 }
15 LL DN(int j,int k) {
16     return A[j+1]-A[k+1];
17 }
18 int main() {
19     //freopen("in.in","r",stdin);
20     //freopen("out.out","w",stdout);
21     while(scanf("%d%d",&n,&T)==2) {
22         for(int i=1;i<=n;i++) scanf("%I64d",&A[i]);
23         sort(A+1,A+n+1);
24         for(int i=1;i<=n;i++) C[i]=C[i-1]+A[i];
25         L=R=0;
26         for(int i=1;i<=n;i++) {
27             while(L<R && UP(q[L+1],q[L])<=i*DN(q[L+1],q[L])) L++;
28             int t=q[L],j;
29             f[i]=f[t]-C[t]+t*A[t+1]-i*A[t+1]+C[i];
30             if((j=i-T+1)>=T) {
31                 while(L<R && UP(j,q[R])*DN(q[R],q[R-1])<=UP(q[R],q[R-1])*DN(j,q[R])) R--;
32                 q[++R]=j;
33             }
34         }
35         printf("%I64d\n",f[n]);
36     }
37     return 0;
38 }
时间: 2024-08-24 16:39:03

HDU3045 Picnic Cows(斜率优化DP)的相关文章

hdu 3045 Picnic Cows(斜率优化DP)

题目链接:hdu 3045 Picnic Cows 题意: 有n个奶牛分别有对应的兴趣值,现在对奶牛分组,每组成员不少于t, 在每组中所有的成员兴趣值要减少到一致,问总共最少需要减少的兴趣值是多少. 题解: 分析:先对n个数进行排序,则可以分析出分组成员一定是连续的 dp[i]表示前i个数得到的最少值则:从j~i作为一组 dp[i]=dp[j-1]+sum[i]-sum[j-1]-(i-j+1)*s[j];//sum[i]表示前i个数的和=>dp[i]=dp[j-1]+sum[i]-sum[j-

【转】斜率优化DP和四边形不等式优化DP整理

当dp的状态转移方程dp[i]的状态i需要从前面(0~i-1)个状态找出最优子决策做转移时 我们常常需要双重循环 (一重循环跑状态 i,一重循环跑 i 的所有子状态)这样的时间复杂度是O(N^2)而 斜率优化或者四边形不等式优化后的DP 可以将时间复杂度缩减到O(N) O(N^2)可以优化到O(N) ,O(N^3)可以优化到O(N^2),依次类推 斜率优化DP和四边形不等式优化DP主要的原理就是利用斜率或者四边形不等式等数学方法 在所有要判断的子状态中迅速做出判断,所以这里的优化其实是省去了枚举

bzoj-4518 4518: [Sdoi2016]征途(斜率优化dp)

题目链接: 4518: [Sdoi2016]征途 Description Pine开始了从S地到T地的征途. 从S地到T地的路可以划分成n段,相邻两段路的分界点设有休息站. Pine计划用m天到达T地.除第m天外,每一天晚上Pine都必须在休息站过夜.所以,一段路必须在同一天中走完. Pine希望每一天走的路长度尽可能相近,所以他希望每一天走的路的长度的方差尽可能小. 帮助Pine求出最小方差是多少. 设方差是v,可以证明,v×m^2是一个整数.为了避免精度误差,输出结果时输出v×m^2. In

[bzoj 1911][Apio 2010]特别行动队(斜率优化DP)

题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1911 分析: 首先可以的到裸的方程f[i]=max{f[j]+a*(Si-Sj)^2+b*(Si-Sj)+c} 0<j<i 简化一下方程,我们知道对于一次项,最后结果肯定是b*Sn 所以可以写成f[i]=max{f[j]+a*(Si-Sj)^2+c} 0<j<i 我们不妨设0<x<y<i,且x比y优 即f[x]+a*(Si-Sx)^2+c>f[y]+a*

hdu 2993 MAX Average Problem (斜率优化dp入门)

MAX Average Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5855    Accepted Submission(s): 1456 Problem Description Consider a simple sequence which only contains positive integers as

hdu3507之斜率优化DP入门

Print Article Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 4780    Accepted Submission(s): 1437 Problem Description Zero has an old printer that doesn't work well sometimes. As it is antiqu

bzoj-1096 1096: [ZJOI2007]仓库建设(斜率优化dp)

题目链接: 1096: [ZJOI2007]仓库建设 Description L公司有N个工厂,由高到底分布在一座山上.如图所示,工厂1在山顶,工厂N在山脚.由于这座山处于高原内陆地区(干燥少雨),L公司一般把产品直接堆放在露天,以节省费用.突然有一天,L公司的总裁L先生接到气象部门的电话,被告知三天之后将有一场暴雨,于是L先生决定紧急在某些工厂建立一些仓库以免产品被淋坏.由于地形的不同,在不同工厂建立仓库的费用可能是不同的.第i个工厂目前已有成品Pi件,在第i个工厂位置建立仓库的费用是Ci.对

BZOJ 1096 [ZJOI2007]仓库建设 斜率优化dp

1096: [ZJOI2007]仓库建设 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/problem.php?id=1096 Description L公司有N个工厂,由高到底分布在一座山上.如图所示,工厂1在山顶,工厂N在山脚. 由于这座山处于高原内陆地区(干燥少雨),L公司一般把产品直接堆放在露天,以节省费用.突然有一天,L公司的总裁L先生接到气象部门的电话,被告知三天之后将有一场

HDU 2829 Lawrence (斜率优化DP或四边形不等式优化DP)

题意:给定 n 个数,要你将其分成m + 1组,要求每组数必须是连续的而且要求得到的价值最小.一组数的价值定义为该组内任意两个数乘积之和,如果某组中仅有一个数,那么该组数的价值为0. 析:DP状态方程很容易想出来,dp[i][j] 表示前 j 个数分成 i 组.但是复杂度是三次方的,肯定会超时,就要对其进行优化. 有两种方式,一种是斜率对其进行优化,是一个很简单的斜率优化 dp[i][j] = min{dp[i-1][k] - w[k] + sum[k]*sum[k] - sum[k]*sum[