其他分享
首页 > 其他分享> > 【HDU】3045Picnic Cows野餐牛(斜率优化)

【HDU】3045Picnic Cows野餐牛(斜率优化)

作者:互联网

【HDU】3045Picnic Cows野餐牛(斜率优化)

Picnic Cows

Time Limit: 8000/4000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)

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

翻译

野餐牛

时间限制:8000/4000 MS(Java /其他)
内存限制:32768/32768 K(Java /其他)

问题描述

现在是暑假。挤奶之后,母牛很累,希望度假。因此,卡罗来纳州农民考虑在河边野餐。但是有一个问题,并不是所有的母牛都认为这是个好主意!有些母牛喜欢在西湖游泳,有些母牛喜欢在香格里拉吃晚饭,而另一些则想做点别的事情。但是为了方便管理,卡罗来纳州强迫所有奶牛去野餐!
农夫卡罗来纳州将她的N头(1 <N≤400000)头母牛带到目的地,但是她发现每头母牛对这项活动的兴趣程度都不一样,以至于他们都失去了兴趣。因此,她必须将它们分组到不同的团队中,以确保每头母牛都能进入满意的团队。考虑到安全性,她要求每个团队中的牛群不少于T(1 <T≤N)。由于每头母牛对这种野餐都有自己的兴趣度,因此我们将该兴趣度的单位测量为“ Moo〜”。同一个团队中的母牛应该将他们的Moo〜降低到该团队中Moo〜最低的那个人–这不是民主行动!因此,卡罗来纳州希望将减少的总Moo〜s最小化,并将N头母牛分为几队。
例如,卡罗来纳州有7头奶牛去野餐,他们的Moo〜是’8 5 6 2 1 7 6’,每队至少有3头奶牛。因此最好的解决方案是让第2、4、5头母牛成群(减少(2-1)+(5-1)Moo〜)和第1、3、6、7头母牛成群(减少( (7-6)+(8-6))Moo〜),答案是8。

输入

输入包含多个案例。
对于每个测试用例,第一行都有两个整数N,T表示母牛的数量和安全基准行的数量。
在n个数字之后,描述N头母牛的Moo〜,第一个是母牛1,第二个是母牛2,依此类推。

输出

每个测试用例一行,包含一个整数,表示将N头母牛分组给几支球队的总减少Moo〜s的最小值。

样本输入

7 3
8 5 6 2 1 7 6

样本输出

8

思路

先dp做
s前缀和
a母牛的Moo〜
先将a从小到大排序
f [ i ] = m i n ( f [ i ] + s [ i ] − s [ j ] − a [ j + 1 ] ∗ ( i − j ) ) 1 < = j < = i − t + 1 f[i]=min(f[i]+s[i]-s[j]-a[j+1]*(i-j)) 1<=j<=i-t+1 f[i]=min(f[i]+s[i]−s[j]−a[j+1]∗(i−j))1<=j<=i−t+1
因为*(i-j)不能单调队列优化,所以我们考虑斜率优化。
设对i来说,从k断开比j优,则有:
f [ j ] − f [ k ] + s [ k ] − s [ j ] + j ∗ a [ k + 1 ] < = i ∗ ( a [ j + 1 ] − a [ k + 1 ] ) f[j]-f[k]+s[k]-s[j]+j*a[k+1]<=i*(a[j+1]-a[k+1]) f[j]−f[k]+s[k]−s[j]+j∗a[k+1]<=i∗(a[j+1]−a[k+1])

代码

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
long long a[400010],f[400010],sum[400010],que[400010],l,r;
long long js(long long x,long long y)
{
	return f[x]-sum[x]+x*a[x+1]-(f[y]-sum[y]+y*a[y+1]);
}
int main()
{
	long long n,m,i,tem;
	while(scanf("%lld%lld",&n,&m)==2)
	{
		for(i=1;i<=n;i++)
			scanf("%lld",&a[i]);
		sort(a+1,a+n+1);
		for(sum[0]=0,i=1;i<=n;i++)
			sum[i]=sum[i-1]+a[i];
		memset(f,0,sizeof(f));
		for(l=r=0,que[0]=0,i=1;i<=n;i++)
		{
			for(;l<r&&js(que[l+1],que[l])<=i*(a[que[l+1]+1]-a[que[l]+1]);l++);//出队,删除不符合条件的数 
			f[i]=f[que[l]]+(sum[i]-sum[que[l]])-a[que[l]+1]*(i-que[l]);
			tem=i-m+1;
			if(tem>=m)
			{
				for(;l<r&&js(tem,que[r])*(a[que[r]+1]-a[que[r-1]+1])<=js(que[r],que[r-1])*(a[tem+1]-a[que[r]+1]);r--);
				que[++r]=tem;//入队 
			}
		}
		printf("%lld\n",f[n]);
	}
	return 0;
}

标签:3045Picnic,HDU,long,罗来纳,cows,Moo,Cows,野餐,母牛
来源: https://blog.csdn.net/weixin_46975572/article/details/114949489