CodeForces - 1337D Xenia and Colorful Gems(二分)
题目链接:点击查看
题目大意:给出三个序列分别记为 a,b,c,现在要求分别从三个序列中找出 x , y , z ,使得 ( x - y )^2 + ( y - z )^2 + ( y - z )^2 最小,求出这个最小值
题目分析:读完题第一反应就是二分嘛,稍微理了一下思路,假设 x <= y <= z ,可以枚举其中一个数组的数作为 y ,然后在另外两个数组中,一个找到小于等于 y 的最大值记为 x ,另一个找大于等于 y 的最小值记为 z ,维护最小值就是答案了
思路非常简单,就是实现起来可能写的比较恶心,这边建议可以将函数封装一下,然后反复调用就好了
一开始想到了 splay 里恰好就有这两个操作,于是就想白嫖,把以前的代码拿下来copy过去,果不其然的TLE了,难顶,然后自己写二分过的,过了四个题之后愉快下班洗漱睡觉,在洗漱的过程中想到了 inf 可能不能作为无穷大,但自己也没法hack掉自己,就懒得在改了,第二天早晨果不其然被fst了,不过还好是小号,不然心态就崩了。。
最后补充一个小技巧,为了防止二分时对边界的判断,在每个数组中加一个 -inf 和 inf 就好了,需要注意的是,因为我的 inf 是0x3f3f3f3f ,在这个题目中不够大,所以在计算 ( x - y )^2 + ( y - z )^2 + ( y - z )^2 的函数中特判一下就行了
代码:
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<unordered_map>
using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=2e5+100;LL a[N],b[N],c[N];inline LL get_upper(LL a[],int n,LL x)//在数组a中找到大于等于x的最小值
{int l=0,r=n+1;LL ans;while(l<=r){int mid=l+r>>1;if(a[mid]>=x){ans=a[mid];r=mid-1;}elsel=mid+1;}return ans;
}inline LL get_lower(LL a[],int n,LL x)//在数组a中找到小于等于x的最大值
{int l=0,r=n+1;LL ans;while(l<=r){int mid=l+r>>1;if(a[mid]<=x){ans=a[mid];l=mid+1;}elser=mid-1;}return ans;
}inline LL fun(LL x,LL y,LL z)
{if(x==inf||y==inf||z==inf)//特判infreturn LLONG_MAX;return (x-y)*(x-y)+(x-z)*(x-z)+(y-z)*(y-z);
}LL cal(LL a[],int na,LL b[],int nb,LL c[],int nc)//遍历数组a作为y
{LL ans=LLONG_MAX;for(int i=1;i<=na;i++){ans=min(ans,fun(a[i],get_lower(b,nb,a[i]),get_upper(c,nc,a[i])));ans=min(ans,fun(a[i],get_upper(b,nb,a[i]),get_lower(c,nc,a[i])));}return ans;
}int main()
{
#ifndef ONLINE_JUDGE
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
#endif
// ios::sync_with_stdio(false);int w;cin>>w;while(w--){int na,nb,nc;scanf("%d%d%d",&na,&nb,&nc);for(int i=1;i<=na;i++)scanf("%lld",a+i);for(int i=1;i<=nb;i++)scanf("%lld",b+i);for(int i=1;i<=nc;i++)scanf("%lld",c+i);a[0]=b[0]=c[0]=-inf;a[na+1]=b[nb+1]=c[nc+1]=inf;sort(a+1,a+1+na);sort(b+1,b+1+nb);sort(c+1,c+1+nc);printf("%lld\n",min(cal(c,nc,a,na,b,nb),min(cal(a,na,b,nb,c,nc),cal(b,nb,a,na,c,nc))));}return 0;
}
CodeForces - 1337D Xenia and Colorful Gems(二分)相关推荐
- [Codeforces Round #635 (div2)]1337D - Xenia and Colorful Gems[二分][枚举]
1337D - Xenia and Colorful Gems[二分][枚举] time limit per test memory limit per test input output 3 sec ...
- Xenia and Colorful Gems(二分--思维)
给定三个数组a,b,c. 要求从每个数字取一个数,使得两两之差和最小. 求出这个数. 我 又 懵 逼 了 . 我 是 会 O ( n 3 ) 的 暴 力 啊 , 怎 么 办 . 我又懵逼了.我是会O( ...
- Codeforces Round #635 (Div. 2) D. Xenia and Colorful Gems 暴力 + 二分
传送门 文章目录 题意: 思路: 题意: 给你三个数组a,b,ca,b,ca,b,c,让你从每个数组中选择一个数x,y,zx,y,zx,y,z,使得(x−y)2+(x−z)2+(y−z)2(x-y)^ ...
- Codeforces Round #635 (Div. 2) D.Xenia and Colorful Gems
Codeforces Round #635 (Div. 2) D.Xenia and Colorful Gems 题目链接 Xenia is a girl being born a noble. Du ...
- 线段树——思维(Codeforces 339D Xenia and Bit Operations/Billboard HDU - 2795)
Codeforces 339D Xenia and Bit Operations vj地址 题意:给出2的n次方个数,每次将现在这个序列中相邻的两个数运算后合并为一个数,得到一个新的序列,这个新序列的 ...
- codeforces 808 E. Selling Souvenirs (dp+二分+思维)
题目链接:http://codeforces.com/contest/808/problem/E 题意:最多有100000个物品最大能放下300000的背包,每个物品都有权值和重量,为能够带的最大权值 ...
- CodeForces - 1480C Searching Local Minimum(交互+二分)
题目链接:点击查看 题目大意:给出一个长度为 nnn 的排列,需要找出一个"局部最小值",所谓"局部最小值"就是对于某个 iii 来说,满足 ai<ai− ...
- 【CodeForces - 608C】Chain Reaction (二分 或 dp ,思维)
题干: 题目大意: 题意是在一条直线上坐落着不同位置的灯塔,每一个灯塔有自己的power level,当作是射程范围.现在从最右边的灯塔开始激发,如果左边的灯塔在这个灯塔的范围之内,那么将会被毁灭.否 ...
- Codeforces 895 B XK Segments 思维 二分
题目链接: http://codeforces.com/contest/895/problem/B 题目描述: 给你长度为n的数列a,然后让你找出所有的对(i,j)满足ai≤aj并且[ai,aj]中能 ...
最新文章
- lstm timestep一般是多少_请问rnn和lstm中batchsize和timestep的区别是什么?
- 著名统计学家David Cox去世:他提出的「COX回归模型」曾影响一代人
- Jquery 获取日期date()对象,jquerydate
- jsonview浏览器插件 查看格式化json数据
- python解析css文件_Python格式化css文件的方法
- 自定义注解+AOP,优雅的打印方法接受和返回的参数内容
- eclipse 快捷调整字体_eclipse字体大小设置快捷键
- 门限回归模型的思想_门限回归汇总与空间门槛回归模型简介
- 如何沟通?如何商务谈判!《沃顿商学院最实用的谈判课》epub免费下载
- python函数找钱_找钱问题–动态规划一例
- SELECT 1 FROM DUAL中的DUAL的作用
- 全球与中国泄漏吸收枕头市场深度研究分析报告
- BUUCTF~Misc~Test5
- 产品经理告诉你什么是PMF?什么是MVP?
- 键盘按键错乱,鼠标也失灵怎么办。。。。。
- nginxconsul
- php fpm 测试,PHP脚本FPM环境最大执行时间研究
- NXP-MPC5748G车载MCU使用(食用)方法(踩坑)实用指南(骗人教程)(三):MATLAB/SIMULINK生成代码给MPC5848G嵌入式处理器运行
- 如何理解:ListString list=new ArrayListString();为甚麼要声明为List 而不是ArrayListString?
- Winhex数据粘贴方法