HDU3045 Picnic Cows
Posted 松子茶
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU3045 Picnic Cows相关的知识,希望对你有一定的参考价值。
Picnic Cows
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2367 Accepted Submission(s): 740
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 ZJNURecommend
gaojie本题也是斜率优化DP。首先 sort 从小到大排序。然后 dp[i] 前 i 个数分隔得到的最小值。则 dp[i]= mindp[j]+(sum[i]-sum[j])-a[j+1](i-j) k<=j<i-t+1.之后就是斜率优化了。
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN=400010;
int q[MAXN];
long long sum[MAXN];
long long a[MAXN];
long long dp[MAXN];
long long getDP(int i,int j)
return dp[j]+(sum[i]-sum[j])-a[j+1]*(i-j);
long long getUP(int j,int k)//得到 yj-yk k<j
return dp[j]-sum[j]+j*a[j+1]-(dp[k]-sum[k]+k*a[k+1]);
long long getDOWN(int j,int k)//得到 xj-xk k<j
return a[j+1]-a[k+1];
int main()
int n,t;
int head,tail;
while(scanf("%d%d",&n,&t)==2)
dp[0]=sum[0]=0;
a[0]=0;
for(int i=1;i<=n;i++)
scanf("%I64d",&a[i]);
sort(a+1,a+n+1);
for(int i=1;i<=n;i++)
sum[i]=sum[i-1]+a[i];
head=tail=0;
q[tail++]=0;
for(int i=1;i<=n;i++)
while(head+1<tail && getUP(q[head+1],q[head])<=i*getDOWN(q[head+1],q[head]))
head++;
dp[i]=getDP(i,q[head]);
int j=i-t+1;
if(j<t)continue;//注意这个条件
while(head+1<tail && getUP(j,q[tail-1])*getDOWN(q[tail-1],q[tail-2])<=getUP(q[tail-1],q[tail-2])*getDOWN(j,q[tail-1]))
tail--;
q[tail++]=j;
printf("%I64d\\n",dp[n]);
return 0;
关于程序算法艺术与实践更多讨论与交流,敬请关注本博客和新浪微博songzi_tea.
以上是关于HDU3045 Picnic Cows的主要内容,如果未能解决你的问题,请参考以下文章
HDU3045 Picnic Cows (斜率DP优化)(数形结合)
POJ3045--Cow Acrobats(theory proving)
poj1259The Picnic & hdu6219 Empty Convex Polygon(17沈阳区域赛C)最大空凸包