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

这题是道二进制想法题,给你n种2^a[i]次,num[i]件的物品,问你怎样分配为两堆物品能使两堆的差值最小。这里我们可以把读入的每件物品都用二进制储存起来,用num[i]表示二进制第i位上的数,用jinwei[i]表示这一位是否由前一位进位得到。

然后从高位到低位循环,直到没有进位的且为1的那位退出,那么最小的差值即为当前的位数所表示的数减去其低位加起来的总数。为什么这样是对的呢,因为如果是0或者2,那么一定可以平分,如果是1,且是由进位得到的,那么这个进位可以表示为2个较低位的数。而且2^i>2^(i-1)+2^(i-2)+2^(i-3)+...+2^0,所以这样差值一定是最小的。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define ll long long
#define inf 0x7fffffff
#define maxn 106000
struct node{
ll num,v;
}a[maxn],b[maxn]; ll jinwei[maxn],num[maxn],ans[maxn]; int main()
{
ll n,m,i,j,T,tot,cnt,cas=0;
scanf("%lld",&T);
while(T--)
{
memset(num,0,sizeof(num));
memset(jinwei,0,sizeof(jinwei));
memset(ans,0,sizeof(ans));
scanf("%lld",&n);
for(i=1;i<=n;i++){
scanf("%lld%lld",&a[i].v,&a[i].num);
num[a[i].v+1]+=a[i].num;
}
tot=0;
for(i=1;i<=105000;i++){
if(num[i]==0)continue;
if(num[i]==1){
tot=i;continue;
}
tot=i;
if(num[i]%2==0){
num[i+1]+=num[i]/2;jinwei[i+1]=1;
num[i]=0;
}
else{
num[i+1]+=num[i]/2;jinwei[i+1]=1;
num[i]=1;
}
}
j=10000000000;
for(i=tot;i>=1;i--){
if(num[i]==0)continue;
if(num[i]==1){
if(jinwei[i]==1)continue;
else{
j=i;break;
}
}
}
if(j==10000000000){
cas++;
printf("Case #%lld: 0\n",cas);continue;
}
if(j==1){
cas++;
printf("Case #%lld: 1\n",cas);continue;
}
for(i=j-1;i>=1;i--){
ans[i]=1-num[i];
}
ans[1]++;
tot=0;
for(i=1;i<=105000;i++){
if(ans[i]==0)continue;
if(ans[i]==1){
tot=i;continue;
}
tot=i;
if(ans[i]%2==0){
ans[i+1]+=ans[i]/2;
ans[i]=0;
}
else{
ans[i+1]+=ans[i]/2;
ans[i]=1;
}
}
cas++;
printf("Case #%lld: ",cas);
for(i=tot;i>=1;i--){
printf("%lld",ans[i]);
}
printf("\n");
}
return 0;
}

bnuoj24252 Divide的更多相关文章

  1. [LeetCode] Divide Two Integers 两数相除

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

  2. Pairwise Sum and Divide 51nod

      1305 Pairwise Sum and Divide 题目来源: HackerRank 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题  收藏  关注 有这样 ...

  3. Conquer and Divide经典例子之Strassen算法解决大型矩阵的相乘

    在通过汉诺塔问题理解递归的精髓中我讲解了怎么把一个复杂的问题一步步recursively划分了成简单显而易见的小问题.其实这个解决问题的思路就是算法中常用的divide and conquer, 这篇 ...

  4. UVA - 10375 Choose and divide[唯一分解定理]

    UVA - 10375 Choose and divide Choose and divide Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  5. [leetcode] 29. divide two integers

    这道题目一直不会做,因为要考虑的corner case 太多. 1. divisor equals 0. 2. dividend equals 0. 3. Is the result negative ...

  6. Leetcode Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. 不用乘.除.求余操作,返回两整数相除的结果,结 ...

  7. uva10375 Choose and Divide(唯一分解定理)

    uva10375 Choose and Divide(唯一分解定理) 题意: 已知C(m,n)=m! / (n!*(m-n!)),输入整数p,q,r,s(p>=q,r>=s,p,q,r,s ...

  8. 51nod1305 Pairwise Sum and Divide

    题目链接:51nod 1305 Pairwise Sum and Divide 看完题我想都没想就直接暴力做了,AC后突然就反应过来了... Floor( (a+b)/(a*b) )=Floor( ( ...

  9. leetcode-【中等题】Divide Two Integers

    题目 Divide two integers without using multiplication, division and mod operator. If it is overflow, r ...

随机推荐

  1. MongoDB Sharding(二) -- 搭建分片集群

    在上一篇文章中,我们基本了解了分片的概念,本文将着手实践,进行分片集群的搭建 首先我们再来了解一下分片集群的架构,分片集群由三部分构成: mongos:查询路由,在客户端程序和分片之间提供接口.本次实 ...

  2. 洛谷P1198 [JSOI2008]最大数(线段树/单调栈)

    题目链接: https://www.luogu.org/problemnew/show/P1198 题目描述 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询 ...

  3. windows下的:开始→运行→命令

    开始→运行→命令 集锦                          winver---------检查Windows版本wmimgmt.msc----打开windows管理体系结构(WMI)wu ...

  4. 求得二叉搜索树的第k小的元素

    求得二叉搜索树的第k小的元素 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 须知:二叉搜索树,又叫二叉排序树,二叉查找树.特点是:左子树的所有元素都小于等 ...

  5. 给HTML页面设置自己的icon

    原因: 不知道为什么,SpringBoot中自动设置icon失效了. 解决方法: 在head标签中添加自己想要使用的icon图片.后缀使用图片格式,不要使用.ico. <link href=&q ...

  6. 细数JS中实用且强大的操作符&运算符

    目录 1,前言 2,代码+应用 2.1,短路运算符 || 2.2,短路运算符 && 2.3,零合并操作符 ?? 2.4,可选链操作符 ?. 2.5,位运算符 & 和 | 2.6 ...

  7. 面试必问:如何实现Redis分布式锁

    摘要:今天我们来聊聊分布式锁这块知识,具体的来看看Redis分布式锁的实现原理. 一.写在前面 现在面试,一般都会聊聊分布式系统这块的东西.通常面试官都会从服务框架(Spring Cloud.Dubb ...

  8. 并发编程常用工具类(二) SymaPhore实现线程池

    1.symaPhore简介 symaphore(信号量)用来控制同时访问某个资源的线程数量,一般用在并发流量控制.个人对它的理解相当于是接待室每次只能接待固定数量的人,当达到最高接待数的时候,其他人就 ...

  9. MySQL设计之Schema与数据类型优化

    一.数据类型优化 1.更小通常更好 应该尽量使用可以正确存储数据的最小数据类型,更小的数据类型通常更快,因为它们占用更少的磁盘.内存和CPU缓存,并且处理时需要的CPU周期更少,但是要确保没有低估需要 ...

  10. vscode 远程开发安装

    1 首先windows 有ssh  程序 2 没有的话就直接使用git  bash 登录到远程服务器开发机上 3 再开一个Git bash  执行 ssh-keygen.exe   生成秘钥    然 ...