首页 > ACM题库 > HDU-杭电 > HDU 3578-Greedy Tino-动态规划-[解题报告]HOJ
2014
11-27

HDU 3578-Greedy Tino-动态规划-[解题报告]HOJ

Greedy Tino

问题描述 :

  Tino wrote a long long story. BUT! in Chinese…
  So I have to tell you the problem directly and discard his long long story. That is tino want to carry some oranges with "Carrying pole", and he must make two side of the Carrying pole are the same weight. Each orange have its’ weight. So greedy tino want to know the maximum weight he can carry.

输入:

The first line of input contains a number t, which means there are t cases of the test data.
  for each test case, the first line contain a number n, indicate the number of oranges.
  the second line contains n numbers, Wi, indicate the weight of each orange
  n is between 1 and 100, inclusive. Wi is between 0 and 2000, inclusive. the sum of Wi is equal or less than 2000.

输出:

The first line of input contains a number t, which means there are t cases of the test data.
  for each test case, the first line contain a number n, indicate the number of oranges.
  the second line contains n numbers, Wi, indicate the weight of each orange
  n is between 1 and 100, inclusive. Wi is between 0 and 2000, inclusive. the sum of Wi is equal or less than 2000.

样例输入:

1
5
1 2 3 4 5

样例输出:

Case 1: 7

HDU3578 Greedy Tino

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 862    Accepted Submission(s): 295

Problem Description
  Tino wrote a long long story. BUT! in Chinese…
  So I have to tell you the problem directly and discard his long long story. That is tino want to carry some oranges with “Carrying pole”, and he must make two side of the Carrying pole are the same weight. Each orange have its’ weight. So greedy tino want to know the maximum weight he can carry.
 

 

Input
The first line of input contains a number t, which means there are t cases of the test data.
  for each test case, the first line contain a number n, indicate the number of oranges.
  the second line contains n numbers, Wi, indicate the weight of each orange
  n is between 1 and 100, inclusive. Wi is between 0 and 2000, inclusive. the sum of Wi is equal or less than 2000.
 

 

Output
For each test case, output the maximum weight in one side of Carrying pole. If you can’t carry any orange, output -1. Output format is shown in Sample Output.

 

 

Sample Input
1
5
1 2 3 4 5
 

 

Sample Output
Case 1: 7
 

 

Author
Tino
******************************************************
题目大意:n个物品有不同的w值,将部分物品分成等重的两部分,然后取走其中一份,问这个重量最大是多少。
解题思路:提供一个和网上不太一样的思路。dp[i],i表示左边重量-右边重量的值,dp存的是当前差值状态下,左右两边之和的最大值。状态非常容易转移。
注意点:w值是[0,2000],当有一个物品的w==0时,就不可能输出-1,这是这道题目坑爹的地方。dp下标因为是差值,所以要增加一个2000的偏移量。
#include <map>
#include <stack>
#include <queue>
#include <math.h>
#include <vector>
#include <string>
#include <fstream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define N 4005
#define M
#define E
#define INF 0x3f3f3f3f
#define eps 1e-8
#define LL long long
#define D(a) ((a)*(a))
using namespace std;

int dp[N],n,a[N],b[N];

void re(void)
{
	scanf("%d",&n);
}

void run(void)
{
	memset(dp,-1,sizeof(dp));
	dp[2000]=0;
	int flag=0;
	for(int i=0;i<n;i++)
	{
	    int v;
	    scanf("%d",&v);
	    if(v==0)flag++;
	    memset(a,-1,sizeof(a));
	    memset(b,-1,sizeof(b));
	    for(int i=0;i<=4000;i++)
	        if(~dp[i])
                a[i-v]=max(dp[i-v],dp[i]+v),
                b[i+v]=max(dp[i+v],dp[i]+v);
        for(int i=0;i<=4000;i++)
            dp[i]=max(dp[i],max(a[i],b[i]));
	}
	if(dp[2000]==0&&flag<1)printf("-1\n");
	else printf("%d\n",dp[2000]/2);
}

int main()
{
	int ncase;
	scanf("%d",&ncase);
	for(int i=1;i<=ncase;i++)
	{
		re();
		printf("Case %d: ",i);
		run();
	}
	return 0;
}

  

参考:http://www.cnblogs.com/Fatedayt/archive/2012/02/24/2366129.html


  1. 可以根据二叉排序树的定义进行严格的排序树创建和后序遍历操作。如果形成的排序树相同,其树的前、中、后序遍历是相同的,但在此处不能使用中序遍历,因为,中序遍历的结果就是排序的结果。经在九度测试,运行时间90ms,比楼主的要快。

  2. #include <cstdio>
    #include <cstring>

    const int MAXSIZE=256;
    //char store[MAXSIZE];
    char str1[MAXSIZE];
    /*
    void init(char *store) {
    int i;
    store['A']=’V', store['B']=’W',store['C']=’X',store['D']=’Y',store['E']=’Z';
    for(i=’F';i<=’Z';++i) store =i-5;
    }
    */
    int main() {
    //freopen("input.txt","r",stdin);
    //init(store);
    char *p;
    while(fgets(str1,MAXSIZE,stdin) && strcmp(str1,"STARTn")==0) {
    if(p=fgets(str1,MAXSIZE,stdin)) {
    for(;*p;++p) {
    //*p=store[*p]
    if(*p<’A’ || *p>’Z') continue;
    if(*p>’E') *p=*p-5;
    else *p=*p+21;
    }
    printf("%s",str1);
    }
    fgets(str1,MAXSIZE,stdin);
    }
    return 0;
    }

  3. I like your publish. It is great to see you verbalize from the coronary heart and clarity on this essential subject matter can be easily noticed.