【HDU】3045Picnic Cows (slope optimization)

【HDU】3045Picnic Cows (slope optimization)

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

translation

Picnic cow

Time limit: 8000/4000 MS (Java/Other)
Memory limit: 32768/32768 K (Java/Other)

Problem Description

It's summer vacation. After milking, the cow is very tired and hopes to take a vacation. Therefore, Carolina farmers consider picnics by the river. But there is a problem, not all cows think this is a good idea! Some cows like to swim in the West Lake, some cows like to have dinner in Shangri-La, while others want to do something else. But in order to facilitate management, Carolina forced all the cows to go on a picnic!
The farmer Carolina took her N (1 <N≤400000) cows to the destination, but she found that each cow had a different degree of interest in this activity, so that they all lost interest. Therefore, she must group them into different teams to ensure that each cow can enter a satisfactory team. Considering safety, she requires that the herd of each team is not less than T (1 <T ≤ N). Since each cow has its own interest in this kind of picnic, we measure the unit of interest as "Moo~". Cows in the same team should lower their Moo~ to the lowest person in the team Moo~ this is not a democratic action! Therefore, Carolina hopes to minimize the total reduction of Moo~s and divide the N cows into several teams.
For example, Carolina has 7 cows going on a picnic, their Moo~ is '8 5 6 2 1 7 6', and each team has at least 3 cows. Therefore, the best solution is to group the second, fourth, and fifth cows in groups (reducing (2-1) + (5-1) Moo~) and the first, third, sixth, and seventh cows in groups (reducing ((7 -6)+(8-6))Moo~), the answer is 8.

enter

The input contains multiple cases.
For each test case, the first row has two integers N, T represents the number of cows and the number of safety benchmark rows.
After n numbers, describe Moo~ of N cows, the first is cow 1, the second is cow 2, and so on.

Output

One line of each test case contains an integer, which represents the minimum total reduction of Moo~s for grouping N cows into several teams.

Sample input

7 3
8 5 6 2 1 7 6

Sample output

8

Ideas

First do dp as
s prefix and
a cow's Moo~
First sort a from small to large
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]*(ij)) 1< =j<=i-t+1f[i]=m i n ( f [ i ]+s[i]s[j]a[j+1](ij))1<=j<=it+1
Because * (ij) is not monotonous queue optimization, so we consider the slope optimization.
Suppose that for i, the disconnection from k is better than j, then:
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]+ja[k+1]<=i(a[j+1]a[k+1])

Code

#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;
}

Guess you like

Origin blog.csdn.net/weixin_46975572/article/details/114949489