【Codeforces 988D】Points and Powers of Two
【链接】 我是链接,点我呀:)
【题意】
让你从一个集合中找出来一个子集
使得这个子集中任意两个数相减的绝对值是2^的整数次幂
且集合的大小最大
【题解】
考虑子集的个数为4个或4个以上
那么我们找到最小的4个a[1],a[2],a[3],a[4]
显然
dis(1,2)=2^a
dis(2,3)=2^b
dis(1,3)=dis(1,2)+dis(2,3) = 2^c
因为2^a+2^b=2^c
所以可以推出来a=b
也即dis(1,2)=dis(2,3)
同理对于a[2],a[3],a[4]
用同样的方法可以得到
dis(2,3)=dis(3,4)
那么dis(1,4)=dis(1,2)*3=2^x*3
显然不是2的整数幂
因此不存在大小大于等于4的集合满足题意。
所以只要考虑集合大小为3以及为2的了
大小为3的话,只要枚举中间那个数字,根据上面的推论dis(1,2)=dis(2,3)
则枚举2^j
看看x-2^j和x+2^j是否存在就好
大小为2就更简单啦
大小为1就随便输出一个数字就好
【代码】
import java.io.*;
import java.util.*;
public class Main {
static InputReader in;
static PrintWriter out;
public static void main(String[] args) throws IOException{
//InputStream ins = new FileInputStream("E:\\rush.txt");
InputStream ins = System.in;
in = new InputReader(ins);
out = new PrintWriter(System.out);
//code start from here
new Task().solve(in, out);
out.close();
}
static int N = (int)2e5;
static class Task{
int n;
int x[] = new int[N+10];
TreeMap<Integer,Integer> dic = new TreeMap<Integer,Integer>();
public void solve(InputReader in,PrintWriter out) {
n = in.nextInt();
for (int i = 1;i <= n;i++) {
x[i] = in.nextInt();
dic.put(x[i], 1);
}
for (int x:dic.keySet()) {
int cur = 1;
for (int j = 0;j <= 30;j++) {
int xl = x - cur,xr = x + cur;
if (dic.containsKey(xl) && dic.containsKey(xr)) {
out.println(3);
out.print(xl+" "+x+" "+xr);
return;
}
cur = cur * 2;
}
}
for (int x:dic.keySet()) {
int cur = 1;
for (int j = 0;j <= 30;j++) {
int xr = x + cur;
if (dic.containsKey(xr)) {
out.println(2);
out.print(x+" "+xr);
return;
}
cur = cur * 2;
}
}
out.println(1);
out.println(x[1]);
}
}
static class InputReader{
public BufferedReader br;
public StringTokenizer tokenizer;
public InputReader(InputStream ins) {
br = new BufferedReader(new InputStreamReader(ins));
tokenizer = null;
}
public String next(){
while (tokenizer==null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(br.readLine());
}catch(IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
【Codeforces 988D】Points and Powers of Two的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【26.67%】【codeforces 596C】Wilbur and Points
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【32.22%】【codeforces 602B】Approximating a Constant Range
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【33.10%】【codeforces 604C】Alternative Thinking
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【51.27%】【codeforces 604A】Uncowed Forces
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【25.33%】【codeforces 552D】Vanya and Triangles
time limit per test4 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...
- 【26.09%】【codeforces 579C】A Problem about Polyline
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 761B】Dasha and friends
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【30.43%】【codeforces 746C】Tram
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
随机推荐
- [C++ 多线程] 学习前瞻
1 多线程是什么? 1.1 多线程的概念? 说起多线程,那么就不得不说什么是线程,而说起线程,又不得不说什么是进程. 进程可以简单的理解为一个可以独立运行的程序单位,它是线程的集合,进程就是有一个或多 ...
- O - Combinations (组合数学)
Description Computing the exact number of ways that N things can be taken M at a time can be a great ...
- msxml3.dll 错误 '800c0005' 系统错误: -2146697211。
asp网站 因为这个问题,困扰自己好多次,还重装过两次服务器系统,非常的麻烦,这次终于找到了问题所在,记录下来,方便以后查看. 服务器症状: 1.服务器上的IE浏览器不能访问外网: 2.set htt ...
- RabbitMQ指南之四:路由(Routing)和直连交换机(Direct Exchange)
在上一章中,我们构建了一个简单的日志系统,我们可以把消息广播给很多的消费者.在本章中我们将增加一个特性:我们可以订阅这些信息中的一些信息.例如,我们希望只将error级别的错误存储到硬盘中,同时可以将 ...
- Objective-C设计模式——生成器Builder(对象创建)
生成器 生成器,也成为建造者模式,同样是创建对象时的设计模式.该模式下有一个Director(指挥者),客户端知道该类引用用来创建产品.还有一个Builder(建造者),建造者知道具体创建对象的细节. ...
- vue-devtools是vue浏览器调试工具
开vue官网在vue-生态系统-工具可以看到vue-devtools这个工具: vue-devtools是一款基于chrome游览器的插件,用于调试vue应用,这可以极大地提高我们的调试效率.接下来我 ...
- Android 仿 新闻阅读器 菜单弹出效果(附源码DEMO)
这一系列博文都是:(android高仿系列)今日头条 --新闻阅读器 (一) 开发中碰到问题之后实现的,觉得可能有的开发者用的到或则希望独立成一个小功能DEMO,所以就放出来这么一个DEMO. 原本觉 ...
- 学习RFT之:TestObject.find方法的了解与使用
第一部分:了解TestObject.find 一.TestObject.find方法的作用 1.测试过程中动态的找到测试对象(控件.标签等),使我们的测试用例不再依赖RFT自带的对象地图(Object ...
- jsp学习笔记 - 内置对象 application
---恢复内容开始--- 1.application一般用this.getServletContext()替代 2.appllication有一个非常有用的函数 getRealPath(),获取绝对路 ...
- java多线程(线程通信-等待换新机制-代码优化)
等待唤醒机制涉及方法: wait():让线程处于冻结状态,被wait的线程会被存储到线程池中. noticfy():唤醒同一个线程池中一个线程(任意也可能是当前wait的线程) notifyAll() ...