zoukankan      html  css  js  c++  java
  • HDU3578 Greedy Tino

    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;
    }
    

      

  • 相关阅读:
    《大型网站技术架构》学习笔记-03案例篇
    SpringBoot详细研究-05微服务与拾遗
    04证券市场典型违法违规行为及法律责任
    《大型网站技术架构》学习笔记-02架构篇
    DES & 3DES 加密算法
    技术的阐述能力
    python des ecb 加密 demo
    linux c 笔记-4 工程项目阅读推荐
    linux c 笔记-3 c语言基础知识
    linux c 笔记-2 Hello World & main函数
  • 原文地址:https://www.cnblogs.com/Fatedayt/p/2366129.html
Copyright © 2011-2022 走看看