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的更多相关文章

  1. CodeForces 1251B --- Binary Palindromes

    [CodeForces 1251B --- Binary Palindromes] Description A palindrome is a string t which reads the sam ...

  2. codeforces 830 B Cards Sorting

    B. Cards Sorting  http://codeforces.com/problemset/problem/830/B Vasily has a deck of cards consisti ...

  3. Codeforces Gym 100418K Cards 暴力打表

    CardsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.action? ...

  4. Codeforces #662C Binary Table

    听说这是一道$ Tourist$现场没出的题 Codeforces #662C 题意: 给定$n*m的 01$矩阵,可以任意反转一行/列($0$变$1$,$1$变$0$),求最少$ 1$的数量 $ n ...

  5. Codeforces 838A - Binary Blocks(二维前缀和+容斥)

    838A - Binary Blocks 思路:求一下前缀和,然后就能很快算出每一小正方块中1的个数了,0的个数等于k*k减去1的个数,两个的最小值就是要加进答案的值. 代码: #include< ...

  6. 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 ...

  7. codeforces 701A A. Cards(水题)

    题目链接: A. Cards 题意: 问两个数的和相同,怎么组合; AC代码: #include <iostream> #include <cstdio> #include & ...

  8. Codeforces Gym 100418K Cards 组合数学

    CardsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.action? ...

  9. codeforces 830 B. Cards Sorting(线段树)

    题目链接:http://codeforces.com/contest/830/problem/B 题解:其实这题就是求当前大小的数到下一个大小的数直接有多少个数,这时候可以利用数据结构来查询它们之间有 ...

随机推荐

  1. db事务级别及锁

    相关sql 事务A BEGIN TRANSACTION --执行修改 获取排他锁 UPDATE Product SET Price = 10 WHERE Id = 1 --阶段2 UPDATE Pro ...

  2. APIO2012 派遣dispatching | 左偏树

    题目链接:戳我 就是尽可能地选取排名小的,加起来就可以了.然后我们考虑利用一个大根堆,一个一个合并,如果超过派遣的钱,我们就把费用最大的那个忍者丢出队列. 左偏树,作为一个十分优秀的可并堆,我们这道题 ...

  3. Elasticsearch学习(2) windows环境下Elasticsearch同步mysql数据库

    在上一章中,我们已经能够通过spring boot来使用Elasticsearch,但是由于我们习惯性的将数据写入mysql,所以为了解决这个问题,Elasticsearch为我们提供了一个插件log ...

  4. css3导航鼠标经过移动、缩放、转动、拉长、拉伸

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. js项目

    https://bbs.csdn.net/topics/320029234 文本实现悬挂缩进 牛逼牛逼 这是真的牛逼 p:first-letter {         margin-left: -2e ...

  6. 快速启动工具Rulers 4.1

    Rulers 4.1 Release 360云盘 https://yunpan.cn/cSbq5nx9GVwrJ 访问密码 0532 百度云 http://pan.baidu.com/s/1czCNR ...

  7. React-Native 工程添加推送功能 (iOS 篇)

    推送已经是是手机应用的基本功能,如果自己实现一套推送系统费时费力,所有一般我们会使用第三方的推送服务,这里我使用「极光推送」作为集成推送的例子,因为有现成的 react native 插件 jpush ...

  8. nginx高性能WEB服务器系列之三版本升级

    nginx系列友情链接:nginx高性能WEB服务器系列之一简介及安装https://www.cnblogs.com/maxtgood/p/9597596.htmlnginx高性能WEB服务器系列之二 ...

  9. flask.abort

    abort(status) status:标注状态码,和异常 抛出异常后会终止当前函数 使用errorhandler装饰器捕获abort异常 @app.errorhandler(500) def in ...

  10. Hibernate框架基础

    Hibernate框架基础 Hibernate框架 ORM概念 O, Object 对象 R, Realtion 关系 (关系型数据库: MySQL, Oracle…) M,Mapping 映射 OR ...