hdu 3045 Picnic Cows 斜率优化dp

来源:互联网 发布:23端口 编辑:程序博客网 时间:2024/06/04 19:27



Picnic Cows

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


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 38 5 6 2 1 7 6
 

Sample Output
8
 

Source
2009 Multi-University Training Contest 14 - Host by ZJNU
 

Recommend
gaojie   |   We have carefully selected several similar problems for you:  3046 3049 3041 3048 3040 
 

Statistic | Submit | Discuss | Note


题意:

给出N(N<=400000)个数组成的序列,要求划分成若干组,使得每组至少有T个元素,每组中所有数要减去一定的值,使之与其中的最小数相等,问减去的数之和最小为多少。


解法:

斜率优化dp


图片暂时不能上传。





#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>#include<vector>using namespace std;#define all(x) (x).begin(), (x).end()#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)#define mes(a,x,s)  memset(a,x,(s)*sizeof a[0])#define mem(a,x)  memset(a,x,sizeof a)#define ysk(x)  (1<<(x))typedef long long ll;typedef pair<int, int> pii;const int INF =0x3f3f3f3f;const int maxn=400000    ;int N,T;ll a[maxn+10],dp[maxn+10],sum[maxn+10];int q[maxn+10];ll getY(int j){    return dp[j]-sum[j]+j*a[j+1];}ll getX(int j){    return a[j+1];}ll fun(int j1,int j2,int i){     return (getY(j1)-getY(j2) )- (a[j1+1]-a[j2+1])*i;}ll Cross(int j2,int j1,int i){     return (getX(j1)-getX(j2)  )*(getY(i)-getY(j1) )-(getX(i)-getX(j1))*(getY(j1)-getY(j2) )  ;}void solve(){    int rear=0,front=0;    dp[T]= sum[T]-T*a[1];    q[rear++]=0;    for(int i=T+1;i<=N;i++)    {        if(i-T>=T)        {            while( rear-2>=front&& Cross(q[rear-2],q[rear-1],i-T)<=0  ) rear--;            q[rear++]=i-T;        }        while(  rear-2>=front &&  fun( q[front+1],q[front],i )<=0   )  front++;        int j=q[front];        dp[i]=  dp[j]+(sum[i]-sum[j])-(i-j)*a[j+1];    }    printf("%lld\n",dp[N]);}int main(){   std::ios::sync_with_stdio(false);   while(cin>>N>>T)   {       for1(i,N)  cin>>a[i];       sort(a+1,a+1+N);       sum[0]=0;       for1(i,N)  sum[i]=sum[i-1]+a[i];       solve();   }   return 0;}/*3 11 2 39 31 1 1 2 2 2 3 3 34 20 0 0 17 30 1 2 2 2 2 4*/


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 南宁电动车牌被偷了怎么办 南宁电车车牌被偷了怎么办 电车车牌被偷了怎么办 上海电动车车牌被偷了怎么办 太子摩托车离合回的慢怎么办 指甲小月牙太少怎么办 牛仔裤用84泡了怎么办 蓝色的衣服晒红怎么办 厨房用的剪刀开合很紧怎么办 理发的剪刀钝了怎么办 小孩眼睛肿了怎么办才能消肿 柿子和螃蟹后要怎么办 柿子和螃蟹吃了怎么办 吃了没熟的虾怎么办 邻居小孩怕我家小狗怎么办 心里有一道坎过不去了怎么办 刚买的小狗怕人怎么办 一年级孩子字写不好怎么办 小狗三天没吃了怎么办 捡到一只流浪猫怎么办 仓鼠四肢红肿圈状怎么办 泰迪的鼻子干燥怎么办 小狗眼睛有白色浓稠物怎么办 流浪狗生了小狗怎么办 学生字写得很差怎么办 猫身上粘老鼠胶怎么办 抄东西抄的手疼怎么办 皮质物品被油性笔划了怎么办 在小区猫丢了怎么办 母猫把小猫丢了怎么办 小狗不吃东西没精神怎么办 小狗的鼻子烂了怎么办 狗老是在家拉尿怎么办 狗狗鼻子有点干怎么办 狗的鼻头不黑了怎么办 金毛鼻头不黑怎么办 金毛毛掉了不长怎么办 狗狗鼻子烂了怎么办 小比熊鼻子不黑怎么办 狗狗鼻子起皮怎么办 金鱼身子弯了是怎么办