【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 ...
随机推荐
- 昆石VOS2009 VOS3000无漏洞去后门电脑管理客户端大全
注意:您的VOS是什么版本的请使用什么版本的管理客户端,否则无法登陆! ** VOS2009的: ** VOS2009-client-v2.1.2.0.exe 点击这里下载: VOS2009-clie ...
- bzoj 1706: [usaco2007 Nov]relays 奶牛接力跑【矩阵乘法+Floyd】
唔不知道怎么说--大概核心是把矩阵快速幂的乘法部分变成了Floyd一样的东西,非常之神 首先把点离散一下,最多有200个,然后建立邻接矩阵,a[u][v]为(u,v)之间的距离,没路就是inf 然后注 ...
- lodop 打印
使用Lodop打印: 一.在官网下载http://www.lodop.net/download.html 若是安装还是提示未安装,就按转这个 二.准备两个js 三.需要在页面最上面加入 <htm ...
- Eclipse/STS 在线安装阿里java代码规约插件
1.打开Idea的在线安装插件界面,通过“Help”-->“Install New Software...” 进入 2. 在 “Work with” 栏输入插件包的下载地址:https://p3 ...
- 服务器出现 nginx 502 Bad Gateway
发生原因1.PHP FastCGI进程数不够用 当网站并发访问巨大时,php fastcgi的进程数不有一定的保障,因为cgi是单线程多进程工作的,也就是说cgi需要处理完一个页面后再继续下一个页面. ...
- O(1)的快速乘
那么 有位神仙已经说了O(1)的算法(当然不是我) 这是一种骚操作 直接放代码了啊 inline LL mul(LL a,LL b,LL Mod){ LL lf = a * ( b >> ...
- 300 Longest Increasing Subsequence 最长上升子序列
给出一个无序的整形数组,找到最长上升子序列的长度.例如,给出 [10, 9, 2, 5, 3, 7, 101, 18],最长的上升子序列是 [2, 3, 7, 101],因此它的长度是4.因为可能会有 ...
- [转]从数据到代码——基于T4的代码生成方式
本文转自:http://www.cnblogs.com/artech/archive/2010/10/23/1859529.html 在之前写一篇文章<从数据到代码>(上篇.下篇)中,我通 ...
- WCF学习笔记(1)-一个完整的例子
一.开发环境 IDE:VS2013 OS:Win10 IIS:IIS 10 二.开发流程 1.项目结构 2.添加一个WCF程序 3.删除系统自动生成的两个文件IService1.cs和Service1 ...
- 学习RFT之:TestObject.find方法的了解与使用
第一部分:了解TestObject.find 一.TestObject.find方法的作用 1.测试过程中动态的找到测试对象(控件.标签等),使我们的测试用例不再依赖RFT自带的对象地图(Object ...