CS46 C 枚举二分
给你n*2个数其中n个数是原数减去了X值的数。问你满足条件的X值和原来的n个数。注意X为正整数。
X should be positive,没0的
思路很简单,一个数必定会对应一个数,那么枚举一个数和其他所有数的差值作为X,然后对每个数找到没有标记过的对应相差X的数,这个过程可以二分(不二分也一样过),标记。如果要找的数已经有标记了,由于拥有相同数存在的情况,那么往后遍历到继续找。
/** @Date : 2017-09-05 19:26:41
* @FileName: C.cpp
* @Platform: Windows
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version : $Id$
*/
#include <bits/stdc++.h>
#define LL long long
#define PII pair<int ,int>
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8; int n;
int a[2010];
int vis[2010];
int main()
{
while(cin >> n)
{
for(int i = 1; i <= n*2; i++)
scanf("%d", a + i);
sort(a + 1, a + n*2 + 1);
MMF(vis);
int ans = -1;
vector<int>q;
for(int l = 1; l <= n; l++)
{
int cnt = a[l + 1] - a[1];
if(cnt == 0)//尼玛不能0的
continue;
int flag = 0;
MMF(vis);
for(int i = 1; i <= n*2 - 1; i++)
{
if(vis[i])
continue;
int k = lower_bound(a + 1, a + n*2 + 1, a[i] + cnt) - a;
if(a[k] - a[i] != cnt)
{
flag = 1;
break;
}
while(k <= n*2 && vis[k] && a[k+1] == a[k])
k++;
if(k > n*2 || vis[k] || a[k] - cnt != a[i])
{
flag = 1;
break;
}
vis[k] = vis[i] = 1;
q.PB(a[k]);
}
if(flag == 0)
{
ans = abs(cnt);
break;
}
else
q.clear();
} cout << ans << endl;
if(ans != -1)
for(int i = 0; i < q.size(); i++)
printf("%d%s", q[i], i==q.size() - 1?"\n":" ");
}
return 0;
}
CS46 C 枚举二分的更多相关文章
- HDU4430 Yukari's Birthday(枚举+二分)
Yukari's Birthday HDU4430 就是枚举+二分: 注意处理怎样判断溢出...(因为题目只要10^12) 先前还以为要用到快速幂和等比数列的快速求和(但肯定会超__int64) 而 ...
- CSU OJ PID=1514: Packs 超大背包问题,折半枚举+二分查找。
1514: Packs Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 61 Solved: 4[Submit][Status][Web Board] ...
- 4 Values whose Sum is 0(枚举+二分)
The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute ...
- Codeforces Round #379 (Div. 2) C. Anton and Making Potions 枚举+二分
C. Anton and Making Potions 题目连接: http://codeforces.com/contest/734/problem/C Description Anton is p ...
- HDU 4282 A very hard mathematic problem --枚举+二分(或不加)
题意:问方程X^Z + Y^Z + XYZ = K (X<Y,Z>1)有多少个正整数解 (K<2^31) 解法:看K不大,而且不难看出 Z<=30, X<=sqrt(K) ...
- Subset---poj3977(折半枚举+二分查找)
题目链接:http://poj.org/problem?id=3977 给你n个数,找到一个子集,使得这个子集的和的绝对值是最小的,如果有多种情况,输出子集个数最少的: n<=35,|a[i]| ...
- hdu 5288 OO’s Sequence 枚举+二分
Problem Description OO has got a array A of size n ,defined a function f(l,r) represent the number o ...
- codeforces 613B B. Skills(枚举+二分+贪心)
题目链接: B. Skills time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- Codeforces gym101612 L.Little Difference(枚举+二分)
传送:http://codeforces.com/gym/101612 题意:给定一个数n(<=1e18),将n分解为若干个数的成绩.要求这些数两两之间的差值不能大于1. 分析: 若n==2^k ...
随机推荐
- “我爱淘”第二冲刺阶段Scrum站立会议6
完成任务: 完成学院分类的点击查看书籍功能,可以点击书的条目查看书的详细信息. 计划任务: 将书的详细信息进行完善,并且可以点击收藏以及已预订等功能. 遇到问题: 分类功能,根据不同学院,自动将数据库 ...
- jdbc 4.0
1.存储MySQL数据库的date.time.timestamp.datetime以及year类型数据 package com.rong.jielong; import java.sql.Connec ...
- python 将base64字符串还原为图片
今天弄验证码的时候发现,验证码的图片的src竟然是下面的这么一个一串字符串,吓到,好像不可以http请求的,第一次见,就好尴尬,去网上搜索了一下,说是: 这是Data URI scheme. data ...
- 团队作业05——测试与发布(alpha阶段)
测试 请根据团队项目中软件的需求文档.功能说明.系统设计和测试计划,写出软件的测试过程和测试结果,并回答下述问题. 在测试过程中总共发现了多少Bug?每个类别的Bug分别为多少个? 显示计算结果超过看 ...
- PHP用抛物线的模型实现微信红包生成算法的程序源码
<?php /* *Author:Kermit *Time:2015-8-26 *Note:红包生成随机算法 */ header("Content-type:text/html;cha ...
- 彻底解决Webpack打包慢的问题
转载 这几天写腾讯实习生 Mini 项目的时候用上了 React 全家桶,当然同时引入了 Webpack 作为打包工具.但是开发过程中遇到一个很棘手的问题就是,React 加上 React-Route ...
- 数据库引擎InnoDB和MyISAM区别
MyISAM是MySQL的默认数据库引擎(5.5版之前),由早期的ISAM(Indexed Sequential Access Method:有索引的顺序访问方法)所改良.虽然性能极佳,但却有一个缺点 ...
- 中国省市 Json 二级联动
Json数据: var cities = {'北京': ['北京'], '广东': ['广州', '深圳', '珠海', '汕头', '韶关', '佛山', '江门', '湛江', '茂名', '肇庆 ...
- angular 数据内容有重复时不显示问题
<body ng-app="app"> <div ng-controller="myctl"> <ul> <li ng ...
- 【Python】内置函数
一.内置函数表格 详细信息 二.内置函数详情 2.1 abs(x) 返回绝对值 1 2 >>> abs(-5) 5 2.2 all(iterable) 如果这个可迭代的元素都为真,就 ...