码迷,mamicode.com
首页 > 其他好文 > 详细

HDU3045 Picnic Cows(斜率优化DP)

时间:2016-01-10 21:14:55      阅读:388      评论:0      收藏:0      [点我收藏+]

标签:

 

 

 

             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
 

 

【思路】

       斜率优化+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 }

 

HDU3045 Picnic Cows(斜率优化DP)

标签:

原文地址:http://www.cnblogs.com/lidaxin/p/5119043.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!