Simple DP Summary

Source: Internet
Author: User
Tags cmath

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 a integer axis, and the position of each village are identified with a single integer Coordi Nate. There is no villages in the same position. The distance between and positions is the absolute value of the difference of the their integer coordinates.

Post Offices is built in some and not necessarily all of the villages. A Village and the post office in it has the same position. For building the post offices, their positions should is chosen so, the total sum of all distances between each villag E and its nearest post office is minimum.

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

Input

Your program was to read from standard input. The first line contains integers:the first was the number of villages V, 1 <= v <=, and the second is the Nu Mber of Post offices p, 1 <= p <=, p <= v. The second line contains V integers in increasing order. These V integers is the positions of the villages. For each position x it holds 1 <= x <= 10000.

Output

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

Sample Input

10 51 2 3 6 7 9 11 22 44 50

Sample Output

9

DP "I" [j]=dp[k][j-1]+sum[k+1][i];
Note that the sum array can be optimized
 #include <iostream> #include <cstdio> #include <cstring># include<string> #include <cstdlib> #include <algorithm> #include <cmath>using namespace std;#        Define INF 0x7fffffffint 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 o R greater located within the whole array. The sum of a rectangle is the sum of the "the elements in" that rectangle. The problem the sub-rectangle with the largest sum are 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:

7 |
-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 in a line by itself, indicating the size of the square two-dimensional a Rray. This was followed by n^2 integers separated by whitespace (spaces and newlines). These is the n^2 integers of the array, presented in Row-major order. That's, all numbers on the first row, left-to-right, then all numbers-second row, left-to-right, etc. N may be as large as 100. The numbers in the array would be in the range [-127,127].

Output

Output the sum of the maximal sub-rectangle.

Sample Input

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

Sample Output

15

Equivalent to the largest continuous sub-sequence and just, water ....
#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;}

  

Simple DP Summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.