累加器传送门:
http://blog.csdn.net/NOIAu/article/details/71775000
题目传送门:
https://vjudge.net/problem/HDU-3045
题目:
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 (N from 1 to 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(T from 1 to 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.
输入:
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.
输出:
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.
样例输入:
7 3
8 5 6 2 1 7 6
样例输出:
8
题目大意:
给出一个有N (1<=N<=400000)个正数的序列,要求把序列分成若干组(可以打乱顺序),每组的元素个数不能小于T (1<=T<=N)。每一组的代价是每个元素与最小元素的差之和,总代价是每个组的代价之和,求总代价的最小值
很容易写出状态转移方程
用dp[i]表示排了序之后的数据的前i个点都分好了组,所需要的最低代价,用cnt[i]表示前缀和
dp[i] = min(dp[j]+cnt[i] – cnt[j] – a[j+1]*(i-j))
将和j无关的东西提出来得到
dp[i] = min(dp[j]-cnt[j]+a[j+1]*j– a[j+1]*i) + cnt[i]
所以有
dp[i]=dp[j]-cnt[j]+a[j+1]*j– a[j+1]*i
令dp[i]为b,dp[j]-cnt[j]+a[j+1]*j为y,a[j+1]为x,i为k
原来的式子转化为
b=y-k*x;
移项得到
y=k*x+b;
所以这里用斜率优化维护点集(x,y)即可
斜率优化的讲解在这篇博客上,这里就不再讨论
http://blog.csdn.net/NOIAu/article/details/71774994
注意几点:
F:
由于题目原因,i和j至少相隔T,所以我们每次只需要入队i-T+1的就行了,入多了暂时也用不了,还有可能出错,不如不入
S:
在判断Tail的时候,我的while内是这样写的
head+1<tail
&&
gay(j,q[tail-1])*gax(q[tail-1],q[tail-2])
<=
gax(j,q[tail-1])*gay(q[tail-1],q[tail-2])
是用在那篇博客上写的向量的叉乘来判断的,为什么是
j,q[tail-1]和q[tail-1],q[tail-2]呢?
为什么不是q[tail]和q[tail-1]呢?
我们可以这样理解
对于这个P点来说,如果向量q[tail-1]P在向量q[tail-2]q[tail-1]的顺时针方向,由于这是一个凸包,每条边斜率满足单调性,所以显然,向量q[tail-1]P也一定在q[tail-1]q[tail]的顺时针方向,所以此时tail - - 没毛病,那么一直减到这个不成立的时候,也就是q[tail-1]P在q[tail-2][tail-1]的逆时针方向的时候P点一定是满足的点,并且会替代掉当前的q[tail]点,所以之后是q[tail++]=j,而不是q[++tail]=j,而x满足单调性,所以不可能出现在q[tail-1]和q[tail]的中间,所以自然,该算法成立,还有另一个原因是因为,注意到这个gay()函数和gax函数,是要输入i计算的时候涉及到i+1,所以如果算tail的话,哪里来的tail+1呢?所以这里也必须要这样计算
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define MAXN 400000+10
#define LL long long
using namespace std;
int q[MAXN];
int N,T;
LL dp[MAXN];
LL cnt[MAXN];
int head,tail;
LL a[MAXN];
LL gay(int j,int k){
return dp[j]-cnt[j]+j*a[j+1]-(dp[k]-cnt[k]+k*a[k+1]);
}
LL gax(int j,int k){
return a[j+1]-a[k+1];
}
LL gad(int i,int j)
{
return dp[j]+(cnt[i]-cnt[j])-a[j+1]*(i-j);
}
int main(){
freopen("in.in","r",stdin);
while(~scanf("%d%d",&N,&T)){
head=0,tail=0;
cnt[0]=dp[0]=a[0]=0;
for(int i=1;i<=N;i++) scanf("%I64d",&a[i]);
sort(a+1,a+N+1);
for(int i=1;i<=N;i++)cnt[i]=cnt[i-1]+a[i];
q[tail++]=0;
for(int i=1;i<=N;i++){
while(head+1<tail&&gay(q[head+1],q[head])-i*gax(q[head+1],q[head])<=0){
head++;
}
dp[i]=gad(i,q[head]);
int j=i-T+1;
if(j<T)continue;
while(head+1<tail&&gay(j,q[tail-1])*gax(q[tail-1],q[tail-2])<=gax(j,q[tail-1])*gay(q[tail-1],q[tail-2])){
tail--;
}
q[tail++]=j;
}
cout<<dp[N]<<endl;
}
return 0;
}