Codeforces 949E Binary Cards
Description
给出一个长度为 \(n\) 的数组,求使得用最少数量的 \(2^k\) 或 \(-2^k\) 的数,使得数组中的每一个元素都可以被你选出的 \(2\) 的次幂表示
题面
Solution
注意到两个性质:
1.一个数不会用两次,举个例子:用两个 \(2\),不如用 \(2,4\) 范围广
2.一个数不会既用 \(2^k\) 又用 \(-2^k\),显然用 \(-2^k,2^{k+1}\) 或者 \(2^k,-2^{k+1}\) 更优
这样就可以依次考虑每一位了:
如果所有的数都不含有这一位,那么就直接把所有的数除以 \(2\)
如果存在数含有这一位,那么用 \(-2^k\) 或者 \(2^k\) 把含有这一位的数都给去掉,然后再把所有的数除以 \(2\)
对于第二种情况我们直接搜索一下就好了
这样复杂度有些问题,但是我们把数去重之后,第 \(k\) 层的数就最多只有 \(\frac{max(A[i])}{2^{k}}\)
复杂度就变成了分治的复杂度了
#include <bits/stdc++.h>
using namespace std;
const int N=100010;
int a[N],b[21][N],top=0,st[N],ans[N],anslen=N;
inline void dfs(int t,int n){
if(t>20 || top>=anslen)return ;
if(n==1 && !b[t][1]){
if(top<anslen){
anslen=top;
for(int i=1;i<=top;i++)ans[i]=st[i];
}
return ;
}
bool flag=1;
for(int i=1;i<=n;i++)if(b[t][i]&1){flag=0;break;}
if(flag){
for(int i=1;i<=n;i++)b[t+1][i]=b[t][i]>>1;
n=unique(b[t+1]+1,b[t+1]+n+1)-b[t+1]-1;
dfs(t+1,n);
return ;
}
for(int w=-1;w<=1;w+=2){
for(int i=1;i<=n;i++)
if(b[t][i]&1)b[t+1][i]=(b[t][i]+w)>>1;
else b[t+1][i]=b[t][i]>>1;
st[++top]=-w*(1<<t);
int tmp=unique(b[t+1]+1,b[t+1]+n+1)-b[t+1]-1;
dfs(t+1,tmp);
top--;
}
}
int main()
{
freopen("pp.in","r",stdin);
freopen("pp.out","w",stdout);
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
sort(a+1,a+n+1);
n=unique(a+1,a+n+1)-a-1;
for(int i=1;i<=n;i++)b[0][i]=a[i];
dfs(0,n);
printf("%d\n",anslen);
for(int i=1;i<=anslen;i++)printf("%d ",ans[i]);
return 0;
}
Codeforces 949E Binary Cards的更多相关文章
- CodeForces 1251B --- Binary Palindromes
[CodeForces 1251B --- Binary Palindromes] Description A palindrome is a string t which reads the sam ...
- codeforces 830 B Cards Sorting
B. Cards Sorting http://codeforces.com/problemset/problem/830/B Vasily has a deck of cards consisti ...
- Codeforces Gym 100418K Cards 暴力打表
CardsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.action? ...
- Codeforces #662C Binary Table
听说这是一道$ Tourist$现场没出的题 Codeforces #662C 题意: 给定$n*m的 01$矩阵,可以任意反转一行/列($0$变$1$,$1$变$0$),求最少$ 1$的数量 $ n ...
- Codeforces 838A - Binary Blocks(二维前缀和+容斥)
838A - Binary Blocks 思路:求一下前缀和,然后就能很快算出每一小正方块中1的个数了,0的个数等于k*k减去1的个数,两个的最小值就是要加进答案的值. 代码: #include< ...
- Codeforces 626 B. Cards (8VC Venture Cup 2016-Elimination Round)
B. Cards time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- codeforces 701A A. Cards(水题)
题目链接: A. Cards 题意: 问两个数的和相同,怎么组合; AC代码: #include <iostream> #include <cstdio> #include & ...
- Codeforces Gym 100418K Cards 组合数学
CardsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.action? ...
- codeforces 830 B. Cards Sorting(线段树)
题目链接:http://codeforces.com/contest/830/problem/B 题解:其实这题就是求当前大小的数到下一个大小的数直接有多少个数,这时候可以利用数据结构来查询它们之间有 ...
随机推荐
- C#生成二维码(加源码)
使用工具: Visual Studio(VS) 2013 第一步: 要用到一个类:QRCodeEncoder 这个类要添加一个动态库:ThoughtWorks.QRCode.dll(项目中有带) 然后 ...
- 列表的操作,元组,range; enumerate
一.列表: 1. 什么是列表 列表是一个可变的数据类型 ,列表由[]来表示, 每一项元素使用逗号隔开. 列表什么都能装. 能装对象的对象. 列表可以装大量的数据 2. 列表的索引和切片 列表和字符串一 ...
- jqury属性操作,特殊效果
一. 常用属性操作 1.html() 取出或设置html内容 // 取出html内容 var $htm = $('#div1').html(); // 设置html内容 $('#div1').html ...
- jxl获取excel中的合并的单元格(主要是方法介绍)
Range[] rangeCells = sheet.getMergedCells();// 返回sheet中合并的单元格数组 for (Range r : rangeCells) {//对数组遍历拿 ...
- easyui里面的加载tree的两种方式
第一种: 使用EasyUI中Tree 符合EasyUI中Tree的Json格式,我们先看一下,格式是如何的 [{ "id":1, "text":"My ...
- NOIP模拟赛
T1 1.聪明的小偷 (thief.pas/c/cpp) [问题描述] 从前有一个收藏家收藏了许多相同的硬币,并且将它们放在了n个排成一排的口袋里,每个口袋里都装了一定数量的硬币. 这些硬币价值不菲, ...
- [开源JVM] yvm - 自制Java虚拟机
中文 | English | | | YVM是用C++写的一个Java虚拟机,现在支持Java大部分功能,以及一个基于标记清除算法的并发垃圾回收器. 不过还有很多bug等待修复. 感兴趣的朋友pull ...
- Beginning and Ending the Speech
Beginning and Ending the Speech Just as musical plays need appropriate beginnings and endings, so do ...
- 调试K3网页版需要注意的问题
1.BIN目录下不能存放类名相同的多个文件,即使修改了dll名称也不可以,必须保持唯一性,多余的备份到其他目录 2.引用的标准dll集合需要与当前运行程序的标准dll集合保持一致,可以通过修改引用路径 ...
- FPGA基础学习(9) -- 复位设计
目录 1. 常见问题 2. 常见的复位方式 3. 合理的复位设计 3.1 复位电平 3.2 异步复位同步化 3.3 恰到好处的复位 4. 补充 4.1 所谓的上电初始化 参考文献 一开始接触到FPGA ...