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. 拍照购物APP之可行性分析

    你一定有过这样的生活经历:走在路上发现一个陌生人的穿着非常符合自己的穿衣品味,想要购买一件同样款式的衣服却找不到购买地址,偷偷拍了张照片也只能留作纪念.此时,在手机上安装一款通过图片进行购物搜索的AP ...

  2. 【原创干货】大数据Hadoop/Spark开发环境搭建

    已经自学了好几个月的大数据了,第一个月里自己通过看书.看视频.网上查资料也把hadoop(1.x.2.x).spark单机.伪分布式.集群都部署了一遍,但经历短暂的兴奋后,还是觉得不得门而入. 只有深 ...

  3. jQuer实时监控input对table进行筛选

    记得以前写过一个预定表格~~~~~比这个更难,一大串前端js~~~忘了~~~好记性不如烂笔头~~记录下,既帮助别人,也帮助自己~~~ 实现思路~通过.on监听input标签的内容变化,通过this获取 ...

  4. HTML学习过程-(1)

    记录我HTML的学习 (1) 最开始学习html是在因为在听北京理工大学教授讲的网络公开课上.当时老师讲的是网络爬虫,因为要爬取特定网页的信息,需要借助[正则表达式](https://baike.ba ...

  5. 12. 前后端联调 + ( proxy代理 ) + ( axios拦截器 ) + ( css Modules模块化方案 ) + ( css-loader ) + ( 非路由组件如何使用history ) + ( bodyParser,cookieParser中间件 ) + ( utility MD5加密库 ) + ( nodemon自动重启node ) + +

    (1) proxy 前端的端口在:localhost:3000后端的端口在:localhost:1234所以要在webpack中配置proxy选项 (proxy是代理的意思) 在package.jso ...

  6. javascript实例教程使用canvas技术模仿echarts柱状图

    canvas 画布是HTML5中新增的标签,可以通过js操作 canvas 绘图 API在网页中绘制图像. 百度开发了一个开源的可视化图表库ECharts,功能非常强大,可以实现折线图.柱状图.散点图 ...

  7. Oracle数据库排序后分页查询数据错误问题解决

    一.问题描述:根据更新时间倒序排序然后分页查询数据,但是点击分页操作的时候,会出现数据重复看似没有操作的情况 二.问题错误原因分析 分页查询的SQL语句: select * FROM (select ...

  8. 反转链表-PHP的实现

    <? //节点 class Node { private $Data;//节点数据 private $Next;//下一节点 public function setData($value) { ...

  9. php时间:获取上一个月,本月天数,下一个月

    时间戳转日期 date() 日期转时间戳 strtotime() 当前时间戳time() 获取当前月的天数: $i=; $y=; echo date("t",strtotime(& ...

  10. [Windows] 如何用编程的方式格式化硬盘

    If memory serves, you're looking for SHFormatDrive(). Use the Format method of the Win32_Volume clas ...