给你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 枚举二分的更多相关文章

  1. HDU4430 Yukari's Birthday(枚举+二分)

    Yukari's Birthday  HDU4430 就是枚举+二分: 注意处理怎样判断溢出...(因为题目只要10^12) 先前还以为要用到快速幂和等比数列的快速求和(但肯定会超__int64) 而 ...

  2. CSU OJ PID=1514: Packs 超大背包问题,折半枚举+二分查找。

    1514: Packs Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 61  Solved: 4[Submit][Status][Web Board] ...

  3. 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 ...

  4. 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 ...

  5. 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) ...

  6. Subset---poj3977(折半枚举+二分查找)

    题目链接:http://poj.org/problem?id=3977 给你n个数,找到一个子集,使得这个子集的和的绝对值是最小的,如果有多种情况,输出子集个数最少的: n<=35,|a[i]| ...

  7. 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 ...

  8. codeforces 613B B. Skills(枚举+二分+贪心)

    题目链接: B. Skills time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  9. Codeforces gym101612 L.Little Difference(枚举+二分)

    传送:http://codeforces.com/gym/101612 题意:给定一个数n(<=1e18),将n分解为若干个数的成绩.要求这些数两两之间的差值不能大于1. 分析: 若n==2^k ...

随机推荐

  1. vue视频插件VLC

    VLC 仅支持windows下特定版本火狐浏览器--Firefox_ESR_55.3 <template> <object type='application/x-vlc-plugi ...

  2. C#高级编程 (第六版) 学习 第六章:运算符和类型强制转换

    第六章 运算符和类型强制转换 1,运算符 类别 运算符 算术运算符 + - * / % 逻辑运算符 & | ^ ~ && || ! 字符串连接运算符 + 增量和减量运算符 ++ ...

  3. PHP 多文件打包下载 zip

    <?php $zipname = './photo.zip'; //服务器根目录下有文件夹public,其中包含三个文件img1.jpg, img2.jpg, img3.jpg,将这三个文件打包 ...

  4. 使用salt-cloud创建虚拟机

    salt-cloud也是基于openstack来做的,它可以支持多种云的使用.比如:Aliyun.Azure.DigitalOcean.EC2.Google Compute Engine.HP Clo ...

  5. (转自大富翁)LockType 属性

    指示编辑过程中对记录使用的锁定类型. 设置和返回值 设置或返回以下某个 LockTypeEnum 的值. 常量 说明 adLockReadOnly 默认值,只读.无法更改数据. adLockPessi ...

  6. MVC 中创建自己的异常处理

    1.新建类一个类继承 HandleErrorAttribute 类把异常书写到队列中 public class MyExceptionAttribute: HandleErrorAttribute { ...

  7. iOS-开发者账号与证书

    0.开发者账号的申请 1.iOS-证书相关 2.iOS-证书申请 3.iOS-APNS证书申请与使用 4.iOS-App发布证书的申请与使用

  8. bzoj2669-局部极小值

    题意 有一个 \(n\times m\) 的矩阵,其中每个数都是 \([1,n\times m]\) 中的一个,不会重复.有一些地方的值比周围的8个位置都小(如果有的话).给出这些位置,求这样的矩阵有 ...

  9. 编写高效SQL语句(转)

    转至http://blog.csdn.net/u012150457/article/details/41846299 一.编写高效SQL语句 1) 选择最有效的表名顺序(仅适用于RBO模式) ORAC ...

  10. OSPF虚连接简单配置

    实验实例:<华为路由器学习指南>P715 本实例的拓扑结构如图:Area 2没有直接与骨干区域直接相连,Area 1被用作传输区域来连接Area 0与Area2.为了使Area2与骨干区域 ...