UVA 562 Dividing coins (01背包基础)

【题目链接】:click here~~

代码:

/*
* Problem: UVA No.562
* Running time: 0MS
* Complier: C++
* Author: ACM_herongwei
* Create Time: 11:12 2015/9/9 星期三
* zeroonebags
* 将金币总价值的一半作为背包容量,然后zeronebags
*/
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#define CLR(c,v) (memset(c,v,sizeof(c)))
using namespace std;

template <typename _T>
inline _T Max(_T a,_T b)
{
    return (a>b)?(a):(b);
}
template <typename _T>
inline _T Maxx(_T a,_T b,_T c)
{
    return (a>Max(b,c))?(a):(Max(b,c));
}
const int N  =  1e5 + 10;

int dp[N];
int value[N];

int main()
{
    int Ncase;
    scanf("%d",&Ncase);
    while(Ncase--)
    {
        CLR(dp,0);
        int sum_cost=0, n_bags;
        scanf("%d",&n_bags);
        for(int i=0; i<n_bags; ++i)  // max:1000
        {
            scanf("%d",&value[i]);
            sum_cost+=value[i];
        }
        int mid_cost=sum_cost/2;
        for(int i=0; i<n_bags; ++i)
        {
            for(int j=mid_cost; j>=value[i]; --j)
            {
                if(dp[j]<=dp[j-value[i]]+value[i])
                {
                    dp[j]=dp[j-value[i]]+value[i];
                }
            }
        }
        printf("%d\n",sum_cost-2*dp[mid_cost]);
    }
    return 0;
}
/*
sample input
3
3
2 3 5
4
1 2 4 6
4
1 4 5 6
sample ouput
0
1
2
*/

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-08 18:18:41

UVA 562 Dividing coins (01背包基础)的相关文章

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d

UVA 562 Dividing coins (01背包)

//平分硬币问题 //对sum/2进行01背包,sum-2*dp[sum/2] #include <iostream> #include <cstring> #include <algorithm> using namespace std; int value[100000],dp[100000]; int main() { int n,m,sum,sum1; cin>>n; while(n--) { cin>>m; sum=0; for(int

uva 562 Dividing coins

Dividing coins It's commonly known that the Dutch have invented copper-wire. Two Dutch men were fighting over a nickel, which was made of copper. They were both so eager to get it and the fighting was so fierce, they stretched the coin to great lengt

UVA 562 Dividing coins 分硬币(01背包,简单变形)

题意:一袋硬币两人分,要么公平分,要么不公平,如果能公平分,输出0,否则输出分成两半的最小差距. 思路:将提供的整袋钱的总价取一半来进行01背包,如果能分出出来,就是最佳分法.否则背包容量为一半总价的包能装下的硬币总值就是其中一个人能分得的最多的钱了,总余下的钱减去这包硬币总值.(只需要稍微考虑一下总值是奇数/偶数的问题) 1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #includ

(背包dp)UVA - 562 Dividing coins

题意:有n个硬币,每个硬币有个价值,两个人分配硬币,要求最公平分配时候两人拿到的钱的差. 分析:很明显,两人拿到的钱的差越小越公平. 一开始想,一定是一人一半最公平,所以直接把总和sum/2,对着half跑01背包,但是WA了,修改后分别讨论奇偶,额外进行一次sum-half的01背包,也WA,仔细想想觉得有些漏洞. 所以,这题其实可以干脆直接跑sum的背包,不断更新ans=min(ans,sum-dp[j]*2)就行了.如果ans==inf,表示不能分,也就是1个,这时输出0. 代码: 1 #

UVA - 10163Storage Keepers(01背包)

题目大意:UVA - 10163Storage Keepers(01背包) 题目大意:现在有m个守店人,和n家店,每个守店人有个能力值,然后一个守护店的人可以守K家店,那么这些店能到的安全度就是Pi / K.店的安全度取决于守护它的人给的安全度中间最低的那个.这些店的最高安全度取决于最低安全度的那家店.现在问如何雇佣这些人使得店的安全度最高的情况下,费用最少. 解题思路: 安全度要求最高,那么就是要判断每个守店的人要不要雇佣,并且雇佣来之后让它守几家店(安全度).dp[i][j]表示:前面的i家

01背包基础 (杭电2602)

01背包问题: 有一个体积为V的背包,有n件物品,每件物品的体积,价值分别为w[i],p[i];要从n件物品中选些放入背包中,使背包里物品的总价值最大. 动态方程:c[i][j]=max(c[i-1][j],c[i-1][j-w[i]]+p[i]). 有关动态方程方面的代码: for (int i = 1; i <= n; i++) { for (int j = 1; j <= total_weight; j++) { if (w[i] > j) { c[i][j] = c[i-1][j

Jam&#39;s balance HDU - 5616 (01背包基础题)

Jim has a balance and N weights. (1≤N≤20) The balance can only tell whether things on different side are the same weight. Weights can be put on left side or right side arbitrarily. Please tell whether the balance can measure an object of weight M. In

uva 624 CD (01背包)

uva 624 CD You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music is on CDs. You need to have it on tapes so the problem to solve is: you have a tape N minutes long. How to choose tracks from CD to get most ou