Xenia and Colorful Gems

题意

给出三个数组,在每个数组中选择一个数字x,y,z,,使得\((x-y)^2+(y-z)^2+(x-z)^2\)最小。

思路

我们假设x<=y<=z

枚举所有的数作为y时,可以取得的最小值。

具体实现:使用vectorvec[4]存三个数组里的数字。

定义一个数组arr[]={1,2,3},对它进行全排列,每次枚举vec[arr[2]]里的元素作为y,vec[arr[1]]里的元素作为x,vec[arr[3]]里的元素作为z。

x取第一个<=y的,z取第一个>=y的。

代码

#include<bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long ll;
const int N=1e6+10;
const ll inf=0x3f3f3f3f3f3f3f3f; vector<int>vec[10];
int num[5],arr[4];
int Min(int x,int pos)
{
int l=0,r=vec[pos].size()-1,ans=-1;
while(l<=r)
{
int mid=(l+r)/2;
if(vec[pos][mid]<=x)
{
ans=vec[pos][mid];
l=mid+1;
}
else
r=mid-1;
}
return ans;
}
ll cal(int a,int b,int c)
{
return 1LL*(a-b)*(a-b)+1LL*(a-c)*(a-c)+1LL*(b-c)*(b-c);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
for(int i=1; i<=3; i++)
scanf("%d",&num[i]);
for(int i=1; i<=3; i++)
{
for(int j=1; j<=num[i]; j++)
{
int x;
scanf("%d",&x);
vec[i].pb(x);
}
sort(vec[i].begin(),vec[i].end());
}
for(int i=1; i<4; i++)
arr[i]=i;
ll ans=inf;
do
{
int l=arr[1],m=arr[2],r=arr[3];
for(int x:vec[m])
{
int index=lower_bound(vec[l].begin(),vec[l].end(),x)-vec[l].begin();
int lmax=-1;
if(index<vec[l].size())
lmax=vec[l][index];
int rmin=Min(x,r);
if(lmax!=-1&&rmin!=-1)
ans=min(ans,cal(x,rmin,lmax));
}
}
while(next_permutation(arr+1,arr+4));
printf("%lld\n",ans);
for(int i=1;i<=3;i++)
vec[i].clear();
}
return 0;
}

博客

CF #635D Xenia and Colorful Gems 枚举+二分的更多相关文章

  1. CF R 635 div2 1337D Xenia and Colorful Gems 贪心 二分 双指针

    LINK:Xenia and Colorful Gems 考试的时候没想到一个很好的做法. 赛后也有一个想法. 可以考虑答案的样子 x,y,z 可以发现 一共有 x<=y<=z,z< ...

  2. Xenia and Colorful Gems(二分--思维)

    给定三个数组a,b,c. 要求从每个数字取一个数,使得两两之差和最小. 求出这个数. \(我又懵逼了.我是会O(n^3)的暴力啊,怎么办.\) \(\color{Red}{从结果看,选出来的三个数必定 ...

  3. Codeforces 1337D Xenia and Colorful Gems

    题意 给你3个数组\(a, b\)和\(c\),最小化\((x-y)^2+(y-z)^2+(z-x)^2\),其中\(x \in a, y \in b, z \in c\). 解题思路 这题其实第一眼 ...

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

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

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

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

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

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

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

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

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

随机推荐

  1. E2. Send Boxes to Alice (Hard Version)

    秒的有点难以理解:https://blog.csdn.net/weixin_42868863/article/details/103200132 #include<bits/stdc++.h&g ...

  2. 运行一个nodejs服务,先发布为deployment,然后创建service,让集群外可以访问

    问题来源 海口-老男人 17:42:43 就是我要运行一个nodejs服务,先发布为deployment,然后创建service,让集群外可以访问 旧报纸 17:43:35 也就是 你的需求为 一个a ...

  3. 详解 字符串—— String、StringBuffer 与 StringBuilder

    本来这篇博文的内容,本人打算在之后的代码中一点一点通过实例讲解的,但是,本人发现,其实这里的知识点还是蛮重要的. 并且,字符串类型,在任何的程序语言中都是被认真对待的,所以,今天专门写一篇博文来介绍一 ...

  4. 详解 Map集合

    (请关注 本人"集合总集篇"博文--<详解 集合框架>) 首先,本人来讲解下 Map集合 的特点: Map集合 的特点: 特点: 通过 键 映射到 值的对象 一个 映射 ...

  5. linux上Docker安装gogs私服

    一.背景介绍 Gogs 是一款类似GitHub的开源文件/代码管理系统(基于Git),Gogs 的目标是打造一个最简单.最快速和最轻松的方式搭建自助 Git 服务.使用 Go 语言开发使得 Gogs ...

  6. tensorflow1.0 矩阵相乘

    import tensorflow as tf matrix1 = tf.constant([[3,3]]) matrix2 = tf.constant([[2],[2]]) product = tf ...

  7. python中文语料分词处理,按字或者词cut_sentence

    cut_sentence.py import string import jieba import jieba.posseg as psg import logging #关闭jieba日制 jieb ...

  8. Jmeter与LoadRunner的比较

    一.与Loadrunner的比较相似点 1.Jmeter的架构跟loadrunner原理一样, 都是通过中间代理,监控&收集并发客户端发现的指令,把他们生成脚本,再发送到应用服务器,再监控服务 ...

  9. 百度云虚拟空间(BCH)

    百度云虚拟空间(BCH)上的一些默认配置 :first-child { margin-top: 0;}blockquote > :last-child { margin-bottom: 0;}i ...

  10. php://input和parse_str()使用

    php://input可以读取没有处理过的POST数据,总结起来就是, 在用$_POST获取不到由APP或者一些接口的回调数据时,就用php://input试试 实例 index.php <fo ...