【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 ...
随机推荐
- js实现页面的全屏与退出
1.在data里面定义: data(){ return{ isScreen:false, } } //是否显示全屏 fullScreen(event){ this.isScreen = !this.i ...
- [App Store Connect帮助]六、测试 Beta 版本(3.1)管理测试员:添加内部测试员
您可以添加至多 25 个内部测试员(您的 App Store Connect 用户)使用“TestFlight Beta 版测试”来测试您的 App.在您上传了至少一个构建版本之后,才可添加测试员. ...
- [Swift]通天遁地Swift
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- $CF241D\ Numbers$
problem 题目大意: 给你n个数和p,都小于50000要求留下若干个数字,使得剩下的数字异或为0,并且从左到右串联起来可以被p整除,求一种这样的方案. 搜索 #include <bits/ ...
- jQuery——表单应用(4)
HTML: <!--复选框应用--> <!DOCTYPE html> <html> <head> <meta charset="UTF- ...
- vue中引入swiper插件
这里我们使用npm的方式安装swiper插件. 1.npm install vue-awesome-swiper --save 2.在main.js文件中引入文件 import Vue from 'v ...
- view 的继承关系
view 的继承关系 如果一个view 有多个子view. 甚至是多成次的 子view. 只需要把第一级别的view(父view) 授权给用户,就可以访问view了,子view 不需要授权.
- 认识 jQuery (第一笔 点出重点)
1.我们先来讨论一下 jQuery 是什么? 要认识jQuery(简写jQ)首先要知道JavaScript(简写 js),因为jq就是一堆js的函数,是普通的js,只不过jq应用广泛,形成了行业标准[ ...
- 【工具】Webpack
远程仓库建立 码云创建组织项目 git clone ssh 切换到主分支mmall-fe后git remote add origin ssh git pull origin master把master ...
- Java Servlet JSP编程(一)
最近想学学java编程,java现在的应用还是挺广泛的,有必要学习一下. # index.jsp <%@ page language="java" contentType=& ...