dp水题记录


本文最后更新于:2017年9月20日 晚上

~ 对于我鬼畜的dp技巧无语。

NOIP快来了,复习一下。

前面说的很对,是时候水水dp了。

9月13日

让我么从openjudge水起!

1.[poj2479]Maximum sum

Description

Given a set of n integers: A={a1, a2,…, an}, we define a function d(A) as below:

Your task is to calculate d(A).

Input

The input consists of T(<=30) test cases. The number of test cases (T) is given in the first line of the input.
Each test case contains two lines. The first line is an integer n(2<=n<=50000). The second line contains n integers: a1, a2, …, an. (|ai| <= 10000).There is an empty line after each case.

Output

Print exactly one line for each test case. The line should contain the integer d(A).

Sample Input

1

10
1 -1 2 2 3 -3 4 -4 5 -5

Sample Output

13

Hint

In the sample, we choose {2,2,3,-3,4} and {5}, then we can get the answer.

Huge input,scanf is recommended.

Source

POJ Contest,Author:Mathematica@ZSU

思路

两段不相交连续子区间最大和(dp)

Code:

#include<iostream>
#include<cstdio>
using namespace std;
const int INF=~0U>>1;
int n,a;
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		scanf("%d",&a);
		int f1=a,f2=a,mx=a,ans=-INF;
		for(int i=2;i<=n;i++)
		{
			scanf("%d",&a);
			f1=(f1>0?f1:0)+a;//当前子串和
			f2=max(mx,f2)+a;//当前两段子串最大和
			mx=max(f1,mx);//当前子串最大和
			ans=max(ans,f2);//两段子串最大和
		}
		printf("%d\n",ans);
	}
	return 0;
}

!!不是从openjudge开始吗?

这题openjudge也有,只不过poj是不是更高大上呢?

2.[openjudge162]post office

描述

There is a straight highway with villages alongside the highway. The highway is represented as an integer axis, and the position of each village is identified with a single integer coordinate. There are no two villages in the same position. The distance between two positions is the absolute value of the difference of their integer coordinates.

Post offices will be built in some, but not necessarily all of the villages. A village and the post office in it have the same position. For building the post offices, their positions should be chosen so that the total sum of all distances between each village and its nearest post office is minimum.

You are to write a program which, given the positions of the villages and the number of post offices, computes the least possible sum of all distances between each village and its nearest post office.

输入

Your program is to read from standard input. The first line contains two integers: the first is the number of villages V, 1 <= V <= 300, and the second is the number of post offices P, 1 <= P <= 30, P <= V. The second line contains V integers in increasing order. These V integers are the positions of the villages. For each position X it holds that 1 <= X <= 10000.

输出

The first line contains one integer S, which is the sum of all distances between each village and its nearest post office.

样例输入

10 5
1 2 3 6 7 9 11 22 44 50

样例输出

9

来源

IOI 2000

思路

dp,用f[i][j]表示前i个村庄修j个邮局的最小距离,sum[i][j]表示第i个村庄到第j个村庄修1个邮局的最小距离,

方程为$f[i][j]=min(f[i][j],f[k][j-1]+sum[k+1][i])(边界f[i][1]=sum[1][i])$

和$sum[i][j]=sum[i][j-1]+pos[j]-pos[i+j>>1]$

注意

注意初始化的值&&i,j内外层位置

Code:

#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=3e2+10,maxp=32,INF=~0U>>3;
int v,p,a[maxn],f[maxn][maxp]={0},sum[maxn][maxn]={0};
int main()
{
	scanf("%d%d",&v,&p);
	for(int i=1;i<=v;i++)
	{
		scanf("%d",&a[i]);
		for(int j=i+1;j<=v;j++) sum[i][j]=INF;
		for(int j=1;j<=p;j++) f[i][j]=INF;
	}
	for(int i=1;i<=v;i++)
	{
		for(int j=i+1;j<=v;j++)
		{
			sum[i][j]=sum[i][j-1]+a[j]-a[i+j>>1];
		}
	}
	for(int j=1;j<=p;j++)
	{
		for(int i=1;i<=v;i++)
		{
			if(j==1) {f[i][j]=sum[1][i];continue;}
			for(int k=1;k<i;k++)
			{
				f[i][j]=min(f[i][j],f[k][j-1]+sum[k+1][i]);
			}
		}
	}
	printf("%d\n",f[v][p]);
	return 0;
}

文章作者: SpaceSkyNet
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 SpaceSkyNet !
  目录
评论