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. 详解 Paths类 与 Files类

    在本篇博文中,本人主要讲解NIO 的两个核心点 -- 缓冲区(Buffer) 和 通道 (Channel)之一的 缓冲区(Buffer), 有关NIO流的其他知识点请观看本人博文<详解 NIO流 ...

  2. jmeter并发时生成唯一变量

    vars.put("partnerOrderId","ZS"+Thread.currentThread().getId()+System.currentTime ...

  3. [数据库]Mysql蠕虫复制增加数据

    将查询出来的数据插入到指定表中,例: 将查询user表数据添加到user表中,数据会成倍增加 insert into user(uname,pwd) select uname,pwd from use ...

  4. 使用dynamic和MEF实现轻量级的AOP组件 (2)

    转摘 https://www.cnblogs.com/niceWk/archive/2010/07/21/1782092.html 偷梁换柱 上一篇我们初试了DynamicAspect这把小刀,如果你 ...

  5. Mac安装Nginx、Mysql、PHP、Redis

    安装xcode命令行工具的命令 xcode-select --install   安装homebrew: ruby -e "$(curl -fsSL https://raw.githubus ...

  6. ubuntu(物理机)连接ARM开发板

    非虚拟机 ubuntu下连接开发板 首先安装超级终端minicom sudo apt-get install minicom 安装完minicom以后,需要将开发板和电脑进行物理连接.需要使用一条网线 ...

  7. Python之路【第二十八篇】:生成器与迭代器

    #!/usr/bin/env python # -*- coding:utf-8 -*- #只要函数的代码里面出现了yield关键字,这个函数就不再是一个普通的函数了,叫做生成器函数 #执行生成器函数 ...

  8. 怎么查看当前的git分支是基于哪个分支创建的?

    2019独角兽企业重金招聘Python工程师标准>>> Question: 比如从 branch A 切出一个 branch B 然后对branch B做了一系列的操作 然后忘记了b ...

  9. bootstrap-分页-默认分页

    说明 默认分页 示例 <!DOCTYPE html> <html lang="zh-CN">    <head>      <meta c ...

  10. Nakamori Akina

    听过中森明菜的歌以后,一直想写点什么.恰好前段时间看过她的一个访谈https://b23.tv/av13810011,节目里已经39岁左右的她看着已经有些衰老,但是那份属于她的天真却保持的很好. 节目 ...