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.
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 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
题意:
给出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
- HDU 3045 Picnic Cows【dp斜率优化】
- HDU 3045 Picnic Cows 斜率优化DP
- HDU 3045 Picnic Cows (斜率优化DP)
- hdu 3045 Picnic Cows(斜率优化dp)
- hdu 3045 Picnic Cows 斜率优化dp
- HDU-3045 Picnic Cows 【DP+斜率优化】
- HDU-3045 Picnic Cows 斜率优化DP
- HDU 3045 Picnic Cows(斜率优化DP)
- dp斜率优化 Hdu 3045(Picnic Cows)题解
- HDU - 3045 Picnic Cows(斜率优化)
- hdu 3045 Picnic Cows 斜率优化 dp中有间隔限定
- HDOJ 3045 Picnic Cows(斜率优化DP)
- hdu3045 Picnic Cows(斜率优化DP)
- [hdu3045] Picnic Cows DP斜率优化
- hdu3045 Picnic Cows 斜率优化dp
- 【HDU3045】【斜率优化DP】Picnic Cows题解
- hdu 2829 Lawrence, hdu 3045 Picnic Cows,hdu 3480 Division , 斜率dp
- HDU3045——Picnic Cows(斜率优化DP)
- C++函数重载与extern "C"
- 笔记:linux内存管理机制
- Linux的shell提取文件名和目录名的方法
- 5908 Abelian Period
- (HDU 1754)I Hate It 线段树区间查询入门,单点修改
- hdu 3045 Picnic Cows 斜率优化dp
- 嵌入式工程师过的这小半辈子
- BZOJ 1911
- High "gc buffer busy acquire" wait after Upgrade to 11.2 (文档 ID 2047582.1)
- 外挂的本质是什么
- perl加载模块,曾经走过的弯路。
- 敌兵布阵 树状数组入门
- 机器学习之工程师入门路线
- linux/mac上使用nginx