UVA 562 Dividing coins

原创 2015年11月18日 21:45:20

UVA 562 Dividing coins

传送门

题意:给你n个硬币,让你分成两堆,使得两堆的价值差最小。

分析:简单的01背包,转换一下,就是给你总价值的一半的背包空间,让你使背包里的价值尽可能大。有个小优化,就是总价值的一半都没有最大面值的硬币大时,直接就是最大面值*2-总价值。代码如下

#include<stdio.h>
#include<string.h>
#define mm(a) memset(a,0,sizeof(a))
#define ss(a) scanf("%d",&a)
#define For(i,q,s) for(int i=q;i<=s;i++)
#define For1(i,q,s) for(int i=q;i>=s;i--)
int n,a[105],f[25005],t,num,maxn,ans;
int main()
{
  ss(t);
  while(t--)
  {
    maxn=-1;
    mm(f);
    ans=num=0;
    ss(n);
    For(i,1,n)
    {
      ss(a[i]);
      num+=a[i];
      if(maxn<a[i])maxn=a[i];
    }
    if((num>>1)<=maxn)printf("%d\n",(maxn<<1)-num);
    else
    {
      For(i,1,n)
        For1(j,(num>>1),a[i])
          if(f[j]<f[j-a[i]]+a[i])
            f[j]=f[j-a[i]]+a[i];
      ans=num-(f[num>>1]<<1);
      printf("%d\n",ans);
    }
  }
  return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

UVa 562 - Dividing coins 背包

Dividing coins  It's commonly known that the Dutch have invented copper-wire. Two Dutch...
  • cyendra
  • cyendra
  • 2013年04月29日 16:44
  • 624

【UVA-562】Dividing coins(背包)

DescriptionIt’s commonly known that the Dutch have invented copper-wire. Two Dutch men were fighting...

0-1背包-uva562-Dividing coins

Dividing coins

uva 562 Dividing coins (01背包)

uva 562 Dividing coins It's commonly known that the Dutch have invented copper-wire. Two D...

UVA 562 Dividing coins(01背包+平衡问题) @未理解

UVA  562 - Dividing coins 题目大意:给出n个硬币的面值, 要求将这n个硬币分成两堆(不要求个数平均), 尽量使得两堆硬币的和之差的绝对值最小, 病输出最小值的绝对...

UVALive5583 UVA562 Dividing coins

Regionals 1996 >> Europe - Northwestern 问题链接:UVALive5583 UVA562 Dividing coins。 问题简述:输入测试用例数n,每个测试用...

UVA 562 Dividing coins(01背包)

Dividing coins  It's commonly known that the Dutch have invented copper-wire. Two Dutch men...

UVA - 562 - Dividing coins (01背包)

UVA - 562 Dividing coins Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Forma...

UVA 562 - Dividing coins(01背包)

It's commonly known that the Dutch have invented copper-wire. Two Dutch men were fighting over a nic...

UVA 562 - Dividing coins

UVA 562 - Dividing coinsIt’s commonly known that the Dutch have invented copper-wire. Two Dutch men ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:UVA 562 Dividing coins
举报原因:
原因补充:

(最多只允许输入30个字)