2016 清华校赛 problem I && NYOJ 1289-ABS(正解dp!!!)

1289-ABS

内存限制:64MB 时间限制:1000ms Special Judge: No http://acm.nyist.me/problem/1289(NYIST新OJ)(数据已加强,网上贪心代码会wa)
题目:Mr.Ha is a famous scientist .He has just not got a kind of magic medicine called Entropy Cracker.The medicine was preserved in V bottles,and the i-th (1≤i≤N) bottle contains V liters of medicine.To make the magic available, he needs to put the medicine from all the bottles together.

Due to mixing medicine is a dangerous action, Mr.Ha takes a huge container with infinite volume and decides only to pour a whole bottle of medicine into the container each time.After adding a p liter`s bottle of medicine into the container with q liters of medicinein it,the resulted volume of medicine in the container will be |p-q| liters.

Initially the container is empty ,and Mr.Ha can put the bottles of medicine into the container by arbitrary order.Finally if there are R liters of medicine in the container ,Mr.Ha will be able to use the magic to increase the time for R seconds every day so that he can achieve more work! Help Mr.Ha to make an arrangement so that the resulted R is maximum.
输入:The first line contains an integer T,indicating the number of test cases. For each test case ,the first line contains an integer N 1≤N≤200,indicating the number of botters, and the second line contains V (|vi|<=500),indicating the volume of medicine in each bottle Attention the volume may be negative as a result of magic The sum of N in all test cases will not exceed 2000.
输出: For each test case , output the case number in the format of the format of the sample ,and an Integer ,the maximum seconds Mr.Ha will able to increase

输入样例:

3 
4 
1  2  2  9
1 
-1 
10 
1  3  0  0  0  1  2  7  3  7  

输出样例:

 8 
 1 
 6

来源 :2016清华acm校赛

题意:一个锅,现在有很多药水,每个药水都有一个权值(整数)。锅现在的权值为0,现在往锅a里倒药水b,a表示当前的锅的权值,b表示药水的权值。药水放入锅中,锅的权值变成了abs(a-b),现在让你将药水按照一定的顺序放到锅中,使得最终锅的权值最大,输出最大值。

思路:根据贪心:很容易想到所有的负数都最后放,0不用考虑,所有的正数怎么放才能使得结果最大?贪心:当然是最大的数字最后放,其他数字凑一个最小值。将这些其他数字分成两堆,使得这两堆的差最小即可。(01背包找一个最小的差值,这个类型的题,学过简单dp的应该都见过(http://acm.nyist.me/problem/325 比如说这道分西瓜的题)。这里就不细说了)

(网上有好多贪心代码,贪心是不能解决dp问题的,所以再怎么贪心,也有漏洞,所以dp才是正解)

代码:

#include <cstdio>
#include <cstring>
#include <cctype>
#include <stdlib.h>
#include <string>
#include <map>
#include <iostream>
#include <sstream>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
#include<list>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define inf 0x3f3f3f3f
typedef long long LL;
const int N=5e4+20;
int a[2050];
int dp[100005];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        int sum=0,ans=0;//ans存的是答案的一部分。sum存的是不含最大正数的正数和
        for(int i=1; i<=n; i++)
            scanf("%d",&a[i]);
        sort(a+1,a+n+1);
        int pos=0;
        for(int i=1; i<=n; i++)
        {
            if(a[i]>0&&!pos) pos=i;//pos找到第一个大于0的数字
            if(a[i]<0) ans-=a[i];//求负数和
            if(a[i]>0) sum+=a[i];//求正数和
        }
        if(a[n]>0)//最大的正数,最后放
        {
            sum-=a[n];
            ans+=a[n];
        }
        if(!pos||pos==n)//只有一个正数,或者是没有正数
        {
            printf("%d\n",ans);
            continue;
        }
        mem(dp,0);
        dp[0]=1;//dp[i]表示是否能凑成i这个值。
        for(int i=pos; i<=n-1; i++)//01背包这些数字:正数,但非最大正数。
            for(int j=sum/2; j>=a[i]; j--)
                dp[j]=max(dp[j],dp[j-a[i]]);
        for(int i=sum/2; i>=0; i--)//遍历找到离中间值最近的值
            if(dp[i])
            {
                sum=(sum-i)-i;//两堆值之差
                break;
            }
        printf("%d\n",ans-sum);
    }
}



猜你喜欢

转载自blog.csdn.net/xiangaccepted/article/details/80185128