POJ 3709 K-Anonymous Sequence(单调队列)

题目链接:http://poj.org/problem?id=3709

题意:给定一个不下降数列,一个K,将数列分成若干段,每段的数字个数不小于K,每段的代价是这段内每个数字减去这段中最小数字之和。求一种分法使得总代价最小?

思路:dp[i]=min(dp[j]+sum[i]-sum[j]-(i-j)*a[j+1])。那么对于两个j1,j2,若j1<j2但是j2比j1更优,那么可得:(dp[j1]-sum[j1]+a[j1+1]*j1)-(dp[j2]-sum[j2]+a[j2+1]*j2)>=i*(a[j1+1]-a[j2+1]),所以每次比较head和head+1两个元素,若head没有head+1好则删掉。我们设dy(j1,j2)=(dp[j1]-sum[j1]+a[j1+1]*j1)-(dp[j2]-sum[j2]+a[j2+1]*j2),dx(j1,j2)=(a[j1+1]-a[j2+1]),那么上面的式子等价于dy(j1,j2)/dx(j1,j2)<=i。其实队列中维护的数字要保持对于某两个相邻元素(x,y)若x比y优,则x必须比y之后的都优。那么我们怎么样做到这一点呢?就是更新队尾。对于队尾的三个元素x,y,z,若dy(x,y)/dx(x,y)>=dy(y,z)/dx(y,z),此时删掉y。因为若dy(x,y)/dx(x,y)<=i,则dy(y,z)/dx(y,z)<=i,也就是y比x优的时候z就比y优,此时必须删去y。若不删去,后面的i若出现dy(x,y)/dx(x,y)>i时我们认为x比y好,但是dy(y,z)/dx(y,z)和i的关系是不确定的,假如dy(y,z)/dx(y,z)<=i,即z比y优,那么我们怎么知道x和z哪个更优呢?因为y在中间隔开了x和z,x和z不能直接比较。

#include <iostream>

#include <stdio.h>

#include <string.h>

#include <algorithm>

#include <cmath>

#include <vector>

#include <queue>

#include <set>

#include <stack>

#include <string>

#include <map>





#define max(x,y) ((x)>(y)?(x):(y))

#define min(x,y) ((x)<(y)?(x):(y))

#define abs(x) ((x)>=0?(x):-(x))

#define i64 long long

#define u32 unsigned int

#define u64 unsigned long long

#define clr(x,y) memset(x,y,sizeof(x))

#define CLR(x) x.clear()

#define ph(x) push(x)

#define pb(x) push_back(x)

#define Len(x) x.length()

#define SZ(x) x.size()

#define PI acos(-1.0)

#define sqr(x) ((x)*(x))

#define MP(x,y) make_pair(x,y)

#define EPS 1e-9



#define FOR0(i,x) for(i=0;i<x;i++)

#define FOR1(i,x) for(i=1;i<=x;i++)

#define FOR(i,a,b) for(i=a;i<=b;i++)

#define FORL0(i,a) for(i=a;i>=0;i--)

#define FORL1(i,a) for(i=a;i>=1;i--)

#define FORL(i,a,b)for(i=a;i>=b;i--)





#define rush() int CC;for(scanf("%d",&CC);CC--;)

#define Rush(n)  while(scanf("%d",&n)!=-1)

using namespace std;





void RD(int &x){scanf("%d",&x);}

void RD(u32 &x){scanf("%u",&x);}

void RD(i64 &x){scanf("%I64d",&x);}

void RD(double &x){scanf("%lf",&x);}

void RD(int &x,int &y){scanf("%d%d",&x,&y);}

void RD(u32 &x,u32 &y){scanf("%u%u",&x,&y);}

void RD(double &x,double &y){scanf("%lf%lf",&x,&y);}

void RD(int &x,int &y,int &z){scanf("%d%d%d",&x,&y,&z);}

void RD(int &x,int &y,int &z,int &t){scanf("%d%d%d%d",&x,&y,&z,&t);}

void RD(u32 &x,u32 &y,u32 &z){scanf("%u%u%u",&x,&y,&z);}

void RD(double &x,double &y,double &z){scanf("%lf%lf%lf",&x,&y,&z);}

void RD(char &x){x=getchar();}

void RD(char *s){scanf("%s",s);}

void RD(string &s){cin>>s;}





void PR(int x) {printf("%d\n",x);}

void PR(int x,int y) {printf("%d %d\n",x,y);}

void PR(int x,int y,int z) {printf("%d %d %d\n",x,y,z);}

void PR(i64 x) {printf("%lld\n",x);}

void PR(u32 x) {printf("%u\n",x);}

void PR(double x) {printf("%.5lf\n",x);}

void PR(char x) {printf("%c\n",x);}

void PR(char *x) {printf("%s\n",x);}

void PR(string x) {cout<<x<<endl;}





const int INF=1000000000;

const int N=500005;



int n,k,Q[N],head,tail;

i64 sum[N],dp[N],a[N];



i64 dy(int j1,int j2)

{

	return (dp[j1]-sum[j1]+a[j1+1]*j1)-(dp[j2]-sum[j2]+a[j2+1]*j2);

}



i64 dx(int j1,int j2)

{

	return a[j1+1]-a[j2+1];

}



void DP()

{

	int i,j,x,y,z;



	head=tail=0;

	FOR1(i,n)

	{

		while(head<tail&&dy(Q[head],Q[head+1])>=i*dx(Q[head],Q[head+1])) head++;

		j=Q[head];

		dp[i]=dp[j]+sum[i]-sum[j]-a[j+1]*(i-j);

		if(i-k+1>=k)

		{

			z=i-k+1;

			while(head<tail)

			{

				x=Q[tail-1];

				y=Q[tail];

				if(dy(x,y)*dx(y,z)>=dy(y,z)*dx(x,y)) tail--;

				else break;

			}

			Q[++tail]=z;

		}

	}

	PR(dp[n]);

}



int main()

{

    rush()

    {

        RD(n,k);

        int i;

        FOR1(i,n) RD(a[i]),sum[i]=sum[i-1]+a[i];

        DP();

    }

	return 0;

}

  

你可能感兴趣的:(sequence)