2015弱校联萌十一大决战之背水一战 D. Divide 二进制思维题

http://www.bnuoj.com/v3/contest_show.php?cid=6869#problem/D

Alice and Bob has found a island of treasure in byteland! They find N kinds of treasures on the island, and each kind of treasure has a certain number, and as in byteland, the value of each treasure will be a power of 2, such as 1,2,4,8 ...

Now the only problem is how to divide treasures fairly, they need to divide the treasures into two parts, and the value of each part is the sum of all treasures' in this part, they want to make the difference between the value of two parts as small as possible, can you help them?

Input


First line of the input is a single integer T(1 <= T <= 20), indicating there are T test cases.

For each test case, the first line contain one integer N(2 <= N <= 10^5), indicate the different kinds of treasures.

Then N line followed, each line will have follow two integer ai(0 <= ai <= 10^5) and xi(0 <= xi <= 10^9), indicate there are xi i-th treasures, and the value of each one is 2^ai.

Output

For each case, you should output a single line, first output "Case #t: ", where t indicating the case number between 1 and T, then a string with only '0' and '1' followed, indicate the minimum difference in binary representation, find more details in samples.

Sample Input

3
2
0 2
2 1
4
0 1
1 1
2 1
3 1
4
0 2
1 1
2 1
3 1

Sample Output

Case #1: 10
Case #2: 1
Case #3: 0

/**
2015弱校联萌十一大决战之背水一战  D. Divide  二进制思维题
题目大意:给定写物品每个有2^a[i]重,每种x[i]个,问将二者平均分成两份,二者的最小差的二进制表示
解题思路:数据如果小点,就是一个背包的问题了。首先从低位往高位处理,jin[i]表示第i位有没有前一位的进位,num[i]表示
          进位后i位置的数,我们找到最前一个该位为1,且并无前一位进位的j,那么2^j,为一份,其前面的所有为一份,做差
          就可以了。因为如果该位为1但有进位的话,完全可以看出改位为0,前一位为2,则可以分成两份的。
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn=100010;
int n,a[maxn],jin[maxn];
LL num[maxn],x[maxn];
int main()
{
    int T,tt=0;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        int maxx=-1;
        memset(num,0,sizeof(num));
        for(int i=0;i<n;i++)scanf("%d%lld",&a[i],&x[i]),maxx=max(maxx,a[i]),num[a[i]]+=x[i];
//        printf(">>>before");
//        for(int i=0;i<=maxx;i++)printf("%lld",num[i]);
//        printf("\n");
        memset(jin,0,sizeof(jin));
        for(int i=0;i<=maxx;i++)
        {
            if(num[i]/2)jin[i+1]=1;
            num[i+1]+=num[i]/2;
            num[i]%=2;
        }
//        printf(">>>");
//        for(int i=0;i<=maxx;i++)printf("%lld",num[i]);
//        printf("\n");
        bool flag=0;
        for(int i=maxx;i>=0;i--)
        {
            if(num[i]&&jin[i]==0)
            {
                maxx=i;
                flag=1;
                break;
            }
        }
        printf("Case #%d: ",++tt);
        if(flag==0)
        {
            puts("0");
            continue;
        }
        int i;
        for(i=0;i<maxx;i++)
        {
            if(num[i]==1)
            {
                num[i]=1;
                for(int j=i+1;j<maxx;j++)num[j]^=1;
                break;
            }
        }
        num[maxx]=(i==maxx);
        for(int i=maxx;i>=0;i--)
        {
            if(num[i]==1)
            {
                for(int j=i;j>=0;j--)printf("%lld",num[j]);
                break;
            }
        }
        printf("\n");
    }
    return 0;
}


你可能感兴趣的:(2015弱校联萌十一大决战之背水一战 D. Divide 二进制思维题)