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 题解:其实这题就是求当前大小的数到下一个大小的数直接有多少个数,这时候可以利用数据结构来查询它们之间有 ...
随机推荐
- unity面试准备
最近有换工作的打算 所以上网看下面试题 自己做下总结 Q:ArrayList 和 List区别 A: 1:List大家都知道初始化的时候需要定义其类型,例如 List<int> listT ...
- c#设计模式系列:状态模式(State pattern)
引言 我们在编程的时候,有时候会遇到,一个对象的行为动作会由对象的状态来决定的,也就是对象的行为是由状态来决定,如果对象的状态很多,那么也会由很多不同的行为,这时候我们一班会 if –else if- ...
- ViewController关闭自身返回前一个View
点击按钮号响应的函数 @IBAction func onBack(sender : AnyObject) { self.dismissViewControllerAnimated(true, comp ...
- JavaScript 用new创建对象的过程
在JavaScript中创建自定义对象都需要用new运算符,那么创建对象的过程是什么样的呢? 例如现在有如下构造函数: function Person(name) { this.name = n ...
- hdu Surround the Trees
题目链接:戳我 凸包模板 #include<iostream> #include<cstdio> #include<cstring> #include<alg ...
- 树状数组套trie 模板
求区间排名,第K大,单点修改,区间前驱,区间后驱. 时间复杂度O(logn^3) #include<iostream> #include<cstdio> #include< ...
- 二十五、MongoDB 索引 和 explain 的使用
一.索引基础 索引是对数据库表中一列或多列的值进行排序的一种结构,可以让我们查询数据库变得更快.MongoDB 的索引几乎与传统的关系型数据库一模一样,这其中也包括一些基本的查询优化技巧.创建索引的命 ...
- 用户画像,知乎Live总结
ttps://www.zhihu.com/lives/889189116527403008/messages 用户画像两层含义:单个标签:用户的分布 标签体系要与时俱进,如果标签被下游强依赖,则不轻易 ...
- 在Java中如何优雅地判空
判空灾难 作为搬砖党的一族们,我们对判空一定再熟悉不过了,不要跟我说你很少进行判空,除非你喜欢NullPointerException. 不过NullPointerException对于很多猿们来 ...
- 解决wordcloud的一个error:Microsoft Visual C++ 9.0 is required. Get it from http://aka.ms/vcpython27
环境: 操作系统:Windows 7 64位 语言:Python 2.7.13 |Anaconda 4.4.0 (64-bit)| (default, May 11 2017, 13:17:26) w ...