CF #635D Xenia and Colorful Gems 枚举+二分
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 枚举+二分的更多相关文章
- CF R 635 div2 1337D Xenia and Colorful Gems 贪心 二分 双指针
LINK:Xenia and Colorful Gems 考试的时候没想到一个很好的做法. 赛后也有一个想法. 可以考虑答案的样子 x,y,z 可以发现 一共有 x<=y<=z,z< ...
- Xenia and Colorful Gems(二分--思维)
给定三个数组a,b,c. 要求从每个数字取一个数,使得两两之差和最小. 求出这个数. \(我又懵逼了.我是会O(n^3)的暴力啊,怎么办.\) \(\color{Red}{从结果看,选出来的三个数必定 ...
- 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\). 解题思路 这题其实第一眼 ...
- 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 613B B. Skills(枚举+二分+贪心)
题目链接: B. Skills time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- 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) ...
随机推荐
- SPFA()判环
1 SPFA()判负环 SPFA()判负环的原理就是在求最短路的过程中,如果存在负环,比如说要求从A到a的最短距离,设为s,但是经过a->c->b->a可以更短,所以如果一直经过a- ...
- E2. Send Boxes to Alice (Hard Version)
秒的有点难以理解:https://blog.csdn.net/weixin_42868863/article/details/103200132 #include<bits/stdc++.h&g ...
- PHP函数:debug_backtrace
debug_backtrace() - 产生一条 PHP 的回溯跟踪(backtrace). 说明: debug_backtrace ([ int $options = DEBUG_BACKTRAC ...
- [源码分析] 带你梳理 Flink SQL / Table API内部执行流程
[源码分析] 带你梳理 Flink SQL / Table API内部执行流程 目录 [源码分析] 带你梳理 Flink SQL / Table API内部执行流程 0x00 摘要 0x01 Apac ...
- CTO的窘境
做CTO太难了,常年的coding思维让你早已与世隔绝,CTO有很好的技术方案,但CEO.CFO等O听不懂是很麻烦的事.总得来说,做CTO一定要技术背景出身,有很强的沟通能力和情商,敏锐的洞察力和决断 ...
- sk-learn实现L2岭回归,对线性回归正则化
岭回归算法: from sklearn.datasets import load_boston from sklearn.externals import joblib from sklearn.li ...
- 进阶 Linux基本命令-2
mkdir -p /a/b/c/d -p 循环创建目录yum install tree -y ...
- C1FlexGrid双grid滚动条联动
利用AfterScroll事件,来实现双grid联动. private void Grid_AfterScroll(object sender, RangeEventArgs e) { try { i ...
- 点击Qtableview表头,触发事件
connect(horizontalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(onHeaderClicked(int))); refer to ...
- Component Object Model (COM) 是什么?
本文主要介绍 COM 的基础知识,倾向于理论性的理解,面向初学者,浅尝辄止. 1. COM 是什么: COM 的英文全称是,Component Object Model,中文译为,组件对象模型.它官方 ...