【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 ...
随机推荐
- mysql workbench的简单使用
注意:即使server没有开启,也可以通过workbench进来编辑页面. 1.再开始程序找到mysql workbench,点击运行 2.t添加管理员链接mysql-------------前提是你 ...
- J20170528-ts
断片 片断 くどい 啰嗦 アノテーション 注释 annotation
- vue-cli 升级至 webpack 4 指北
时至今日(2018-7-11),vue-cli 任然未稳定支持至webpack4,所以我自己也来创建一个 vue 初始化模板 不过大致的原因我也能猜到,因为很多插件仍然是一个不稳定的点,比如我在创建中 ...
- 1.2打印ASCII码
描述 输入一个除空格以外的可见字符(保证在函数scanf中可使用格式说明符%c读入),输出其ASCII码. 输入一个除空格以外的可见字符.输出一个十进制整数,即该字符的ASCII码.样例输入 A 样例 ...
- 二分搜索 POJ 3273 Monthly Expense
题目传送门 /* 题意:分成m个集合,使最大的集合值(求和)最小 二分搜索:二分集合大小,判断能否有m个集合. */ #include <cstdio> #include <algo ...
- bnu 51640 Training Plan DP
https://www.bnuoj.com/bnuoj/problem_show.php?pid=51640 dp[i][j]表示前j个数,分成了i组,最小需要多少精力. 那么,求解订票dp[i][j ...
- nginx connect failed (110- Connection timed out) 问题排查
首先排查 ping 下 nginx 与代理服务是否ping 的通,带端口的,telnet 下端口号是否是通的,本次遇到问题为 telnet 发现有台服务器不通,原因是端口未开放
- Sql2008事务日志已满处理
处理方式: USE [master] GO ALTER DATABASE gzl SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE gzl SET ...
- SpringBoot 2.x (1):手动创建项目与自动创建项目
SpringBoot 2.x基于Spring Framework 5.x 环境需求如下: JDK1.8或以上 Maven3.2或以上 这里我使用的是Eclipse,IDEA这个工具很强大,但不习惯它 ...
- 关于FLASK WEB开发8d 数据库迁移的问题
首先, 第一步,要删除data-dev.sqlite这个数据库 第二步,进行下面的重建 暂时的解决办法是: python manage.py shell In [2]: from app import ...