链接:http://acm.hdu.edu.cn/showproblem.php?pid=3045
Picnic Cows
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2070 Accepted Submission(s): 635
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.
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.
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
题意:
有n头牛,选若干个集合,每个集合中至少要有T头牛。 要求把集合中 欢乐值高的 降到全一样。问最少要降多少。
做法:
分组肯定接近的数分一起,所以可以先排个序。
然后 dp[i]表示 前i头牛 需要最少降多少欢乐值,使其达到要求。
dp[i]=min{dp[j]+sum[i]-sum[j]-(i-j)*num[j+1];
可以推出:
y(j)=dp[j]-sum[j]+j*(num[j+1])
x(j)=num[j+1]
在这种集合有个数要求的斜率优化中,在优化第i个时,入队是 入 i-k+1 个,使得 下一个 (i+1)-(i-k+1)=k ,那么 i+1 和队列里 所有的下标的差都>=k了
这题还要注意的是,dp[i] i<kk&&i!=0 时, dp是没有意义的,所以 不入队。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <malloc.h>
#include <ctype.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#include <stack>
#include <queue>
#include <vector>
#include <deque>
#include <set>
#include <map>
typedef __int64 LL;
LL sum[500100];
LL num[500100];
int que[500100];
LL dp[500000];
LL y(int j)
{
return dp[j]-sum[j]+num[j+1]*j;
}
LL x(int j)
{
return num[j+1];
}
int main()
{
int n,kk;
while(scanf("%d%d",&n,&kk)!=EOF)
{
sum[0]=0;
for(int i=1;i<=n;i++)
{
scanf("%I64d",num+i);
//sum[i]=sum[i-1]+num[i];
}
sort(num+1,num+1+n);
for(int i=1;i<=n;i++)
{
sum[i]=sum[i-1]+num[i];
//printf("%d ",num[i]);
}
// cout<<endl;
int tou=0,wei=0;
que[wei++]=0;
dp[0]=0;
// memset(dp,0,sizeof dp);
for(int i=kk;i<=n;i++)
{
while(wei-tou>=2)
{
int a=que[tou];
int b=que[tou+1];
if((y(b)-y(a))<=i*(x(b)-x(a)))
tou++;
else
break;
}
int nw=que[tou];
dp[i]=dp[nw]+sum[i]-sum[nw]-(i-nw)*num[nw+1];
// printf("%d jjj %d %d\n",i,nw,dp[i]);
int j=i-kk+1;//当前要加入的
if(j<kk)
continue; //前面的 牛的数量不到要求 dp 不成立 所以不入队
while(wei-tou>=2)
{
int a=que[wei-2];
int b=que[wei-1];
int c=j;
if((x(b)-x(a))*(y(c)-y(b))<=(y(b)-y(a))*(x(c)-x(b)))
wei--;
else
break;
}
que[wei++]=j;
}
printf("%I64d\n",dp[n]);
//cout<<dp[n]<<endl;
//cout<<ans<<endl;
}
return 0;
}