码迷,mamicode.com
首页 > 其他好文 > 详细

简单dp总结

时间:2015-03-13 22:20:03      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:

1.poj 1160

Post Office
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 16455   Accepted: 8916

Description

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. 

Input

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.

Output

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

Sample Input

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

Sample Output

9

dp【i】[j]=dp[k][j-1]+sum[k+1][i];
注意sum数组可以进行优化
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
#define INF 0x7fffffff
int v,p,dp[310][31],a[310],sum[310][310];
int main()
{
    while(scanf("%d%d",&v,&p)!=EOF)
    {
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=v;i++)
            scanf("%d",&a[i]);
        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)/2];
            }
        }
        for(int i=1;i<=v;i++)
        {
            dp[i][1]=sum[1][i];
        }
        for(int j=2;j<=p;j++)
        {
            for(int i=j+1;i<=v;i++)
            {
                dp[i][j]=INF;
                for(int k=j;k<=i;k++)
                    dp[i][j]=min(dp[i][j],dp[k][j-1]+sum[k+1][i]);
            }
        }
        printf("%d\n",dp[v][p]);
    }
    return 0;
}

  2.poj 1050

To the Max
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 42088   Accepted: 22375

Description

Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1*1 or greater located within the whole array. The sum of a rectangle is the sum of all the elements in that rectangle. In this problem the sub-rectangle with the largest sum is referred to as the maximal sub-rectangle. 
As an example, the maximal sub-rectangle of the array: 

0 -2 -7 0 
9 2 -6 2 
-4 1 -4 1 
-1 8 0 -2 
is in the lower left corner: 

9 2 
-4 1 
-1 8 
and has a sum of 15. 

Input

The input consists of an N * N array of integers. The input begins with a single positive integer N on a line by itself, indicating the size of the square two-dimensional array. This is followed by N^2 integers separated by whitespace (spaces and newlines). These are the N^2 integers of the array, presented in row-major order. That is, all numbers in the first row, left to right, then all numbers in the second row, left to right, etc. N may be as large as 100. The numbers in the array will be in the range [-127,127].

Output

Output the sum of the maximal sub-rectangle.

Sample Input

4
0 -2 -7 0 9 2 -6 2
-4 1 -4  1 -1

8  0 -2

Sample Output

15

相当于最大连续子序列和而已,水。。。。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<cstdlib>
using namespace std;
int a[101][101],n,cal[101][101],sum,maxx;
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        sum=0,maxx=0;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                scanf("%d",&a[i][j]);
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                cal[i][j]=cal[i][j-1]+a[i][j];
            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=i+1;j<=n;j++)
            {
                sum=0;
                for(int k=1;k<=n;k++)
                {
                    sum+=cal[k][j]-cal[k][i-1];
                    if(sum>maxx)
                        maxx=sum;
                    else if(sum<0)
                        sum=0;
                }
            }
        }
        printf("%d\n",maxx);
    }
    return 0;
}

  

简单dp总结

标签:

原文地址:http://www.cnblogs.com/a972290869/p/4336021.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!