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 题解:其实这题就是求当前大小的数到下一个大小的数直接有多少个数,这时候可以利用数据结构来查询它们之间有 ...
随机推荐
- RabbitMQ与.net core(四) 消息的优先级 与 死信队列
1.消息的优先级 假如现在有个需求,我们需要让一些优先级最高的通知推送到客户端,我们可以使用redis的sortedset,也可以使用我们今天要说的rabbit的消息优先级属性 Producer代码 ...
- Android 虚拟机访问本地服务器的地址
http://10.0.2.2:8080/test/ 比如 localhost:8080 为 tomcat 地址,然后 test 是 tomcat 目录下 webapps 的子目录. 10.0.2.2 ...
- php swoole扩展安装
一波三折. 首先下载swoole安装包(由于我这里php是7,所以说应该去官网下载最新的swoole包,否则会发生意想不到的错误) wget https://github.com/swoole/swo ...
- Blocks to Cubes
Bholu the Pandit on this New Year wanted to divide his Cuboidal Packaging block into cubes. But he l ...
- Weekly Contest 121
984. String Without AAA or BBB Given two integers A and B, return any string S such that: S has leng ...
- 三,Smarty模板技术/引擎——变量操作(2)
1, 变量的分类 ① 从PHP中分配的变量,比如a.php跳转到b.php时候,可以在a.php中分配变量,b.tpl中直接调用.a.php中代码,$smarty->assign(‘str’,’ ...
- JMessage Android 端开发详解
目前越来越多的应用会需要集成即时通讯功能,这里就为大家详细讲一下如何通过集成 JMessage 来为你的 App 增加即时通讯功能. 首先,一个最基础的 IM 应用会需要有哪些功能? 用户注册 / 登 ...
- 各种 Python 库/模块/工具
1 算法 1.1 字符串处理 re 正则表达式的标准库. StringIO / cStringIO 以读写文件的方式来操作字符串(有点类似于内存文件). cStringIO 是 C 语言实现的,提供高 ...
- JAVA 集合随笔
JAVA中提供了现成操纵数据的集合,使得我们在开发中基本不用自己动手实现复杂的数据结构,来保存和操纵数据. 所以当我们有了基本的数据结构知识,会合理利用JAVA提供的集合就好啦. JDK1.8;(仅列 ...
- TCP/IP——何时用UDP代替TCP
UDP和TCP UDP和TCP都有其自身的特点,不同的应用场景和要求需要使用不同的协议来传输,那么何时我们可以用UDP代替TCP呢. UDP 的优点 UDP支持广播和多播,事实上如果应用程序使用广播或 ...