【枚举】Stone Pile

1005. Stone Pile

Time Limit: 2.0 second
Memory Limit: 16 MB
You have a number of stones with known weights W 1, …, Wn. Write a program that will rearrange the stones into two piles such that weight difference between the piles is minimal.

Input

Input contains the number of stones N (1 ≤ N ≤ 20) and weights of the stones W 1, …, Wn (integers, 1 ≤ Wi ≤ 100000) delimited by white spaces.

Output

Your program should output a number representing the minimal possible weight difference between stone piles.

Sample

inputoutput
5
5 8 13 27 14
3
Problem Source: USU Championship 1997
Tags: problem for beginners   (
hide tags for unsolved problems

)


枚举。。一开始还想用矩阵乘法。。

当想到用矩阵乘法没有什么优势之后,还是放弃了,用枚举。。二进制表示

#include <cstdio>
long n;
long w[30];

int main()
{
    scanf("%ld",&n);
    for (long i=1;i<n+1;i++)
    {
        scanf("%ld",w+i);
    }
    long ans = 0x7f7f7f7f;
    for (long i=0;i<(1<<n)+1;i++)
    {
        long met = i;
        long cnt = 0;
        for (long j=1;j<n+1;j++)
        {
            if (met&1) cnt+=w[j];
            else cnt-=w[j];
            met >>= 1;
        }
        if (cnt < 0) cnt = -cnt;
        if (ans > cnt) ans = cnt;
    }
    printf("%ld",ans);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值