K-th Closest Distance
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6621
K-th Closest Distance
Time Limit: 20000/15000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 2426 Accepted Submission(s): 871
For each query, you are given an interval [L, R] and two numbers p and K. Your goal is to find the Kth closest distance between p and aL, aL+1, ..., aR.
The distance between p and ai is equal to |p - ai|.
For example:
A = {31, 2, 5, 45, 4 } and L = 2, R = 5, p = 3, K = 2.
|p - a2| = 1, |p - a3| = 2, |p - a4| = 42, |p - a5| = 1.
Sorted distance is {1, 1, 2, 42}. Thus, the 2nd closest distance is 1.
For each test case:
冘The first line contains two integers n and m (1 <= n, m <= 10^5) denoting the size of array and number of queries.
The second line contains n space-separated integers a1, a2, ..., an (1 <= ai <= 10^6). Each value of array is unique.
Each of the next m lines contains four integers L', R', p' and K'.
From these 4 numbers, you must get a real query L, R, p, K like this:
L = L' xor X, R = R' xor X, p = p' xor X, K = K' xor X, where X is just previous answer and at the beginning, X = 0.
(1 <= L < R <= n, 1 <= p <= 10^6, 1 <= K <= 169, R - L + 1 >= K).
#include<iostream> #include<vector> #include<cstdio> #include<algorithm> #include<math.h> using namespace std; typedef long long LL; const int maxn=1e5+5; int N,M; int sz,cnt=0,sum=0; int root[maxn],a[maxn],v[maxn]; int L,R,P,K,X=0; struct Node {int l,r,sum;Node(){l=r=sum=0;} }T[maxn<<6]; void Init() {cnt=0;root[0]=0;T[cnt].l=T[cnt].r=T[cnt].sum=0;X=0; } void Read(int &n)//只能读入整数 {n=0;int f=1;//用于记录正负char ch=getchar();while(ch<'0'||ch>'9')// {if(ch=='-') f=-1;ch=getchar();}while(ch>='0'&&ch<='9')//下面两种用法都可以 {//n=(n<<1)+(n<<3)+(ch-'0');// n=(n<<1)+(n<<3)+(ch^48);//相当于 x*10+ch-'0';ch=getchar();}n=n*f;//切记乘以f (记录的是+ -)return ; } void Update(int l,int r,int &x,int y,int pos) {T[++cnt]=T[y];T[cnt].sum++;x=cnt;if(l==r) return ;int mid=(l+r)>>1;if(pos<=mid) Update(l,mid,T[x].l,T[y].l,pos);else Update(mid+1,r,T[x].r,T[y].r,pos); } int getid(int x) {return lower_bound(v+1,v+1+sz,x)-(v); } void Query(int l,int r,int x,int y,int h) { // cout<<"l:"<<l<<" r:"<<r<<" sum:"<<sum<<endl;if(l==r){if(h>=v[l]) sum+=T[x].sum-T[y].sum;return ;}int mid=(l+r)>>1;if(h<=v[mid]) Query(l,mid,T[x].l,T[y].l,h);else{sum+=T[T[x].l].sum-T[T[y].l].sum;Query(mid+1,r,T[x].r,T[y].r,h);} } bool judge(int x) {sum=0;Query(1,sz,root[R],root[L-1],P+x);int sum1=sum;sum=0;Query(1,sz,root[R],root[L-1],P-x-1);int sum2=sum;return sum1-sum2>=K; } int main() {int T; // scanf("%d",&T); Read(T);int ca=1;while(T--){Init(); // scanf("%d%d",&N,&M); Read(N);Read(M);for(int i=1;i<=N;i++){scanf("%d",&a[i]);v[i]=a[i];}sort(v+1,v+1+N);sz=unique(v+1,v+1+N)-(v+1); // for(int i=1;i<=sz;i++) cout<<v[i]<<" ";cout<<endl; // for(int i=1;i<=N;i++) cout<<getid(a[i])<<" ";cout<<endl;for(int i=1;i<=N;i++) Update(1,sz,root[i],root[i-1],getid(a[i]));while(M--){sum=0; // scanf("%d%d%d%d",&L,&R,&P,&K); Read(L);Read(R);Read(P);Read(K);L^=X;R^=X;P^=X;K^=X;int l=0,r=1e6,ans;while(l<=r)//二分答案 {int mid=(l+r)>>1;if(judge(mid)){ans=mid;r=mid-1;}else l=mid+1;}printf("%d\n",ans);X=ans;}}return 0; }
转载于:https://www.cnblogs.com/caijiaming/p/11289099.html
K-th Closest Distance相关推荐
- K-th Closest Distance HDU - 6621(第k小绝对值+主席树+二分)
You have an array: a1, a2, , an and you must answer for some queries. For each query, you are given ...
- 杭电多校第四场-H- K-th Closest Distance
题目描述 You have an array: a1, a2, , an and you must answer for some queries. For each query, you are ...
- HDU - 6621 K-th Closest Distance——主席树+二分
[题目描述] HDU - 6621 K-th Closest Distance [题目分析] 因为看到第kkk大的要求,刚开始的时候一直都在想怎么运用第kkk大来解决问题,但是后来看其他人的博客才发现 ...
- 2019 Multi-University Training Contest 4 - K-th Closest Distance
主席树 + 二分答案 对于这种区间内的值域问题一般用主席树进行求解. 因为数据范围只有1e6,所以不用离散化,直接建树即可. 题目要求找到区间内离p第k近的数,可以想到,这个问题具有单调性(某个区间长 ...
- 斯坦福CS231n项目实战(一):k最近邻(kNN)分类算法
我的网站:红色石头的机器学习之路 我的CSDN:红色石头的专栏 我的知乎:红色石头 我的微博:RedstoneWill的微博 我的GitHub:RedstoneWill的GitHub 我的微信公众号: ...
- knn 邻居数量k的选取_选择K个最近的邻居
knn 邻居数量k的选取 Classification is more-or-less just a matter of figuring out to what available group so ...
- scikit-learn K近邻法类库使用小结
1. scikit-learn 中KNN相关的类库概述 在scikit-learn 中,与近邻法这一大类相关的类库都在sklearn.neighbors包之中.KNN分类树的类是KNeighborsC ...
- [机器学习-Sklearn]K-means(K均值)学习与总结
K-means总结 前言 一,k-means算法 二,k的选择(仅供参考) 1.肘部法则 2. 根据实际应用的目的选择K 三,代码讲解 四, K值的确定的代码 五, 相同数据下用K-means分成3个 ...
- 机器学习(三)k均值聚类
k均值聚类 原文地址:http://blog.csdn.net/hjimce/article/details/45200985 作者:hjimce 高斯混合模型和k均值聚类是聚类算法中的两种比较常用而 ...
最新文章
- 【C++】lambda 表达式
- 软件测试——进程调度(短作业优先调度算法+先来先服务算法)测试
- 四中方式实现单例模式
- verilog设置24进制计数器_阅读笔记:《Verilog HDL入门》第3章 Verilog语言要素
- 图解Android 内存分析工具之Mat使用教程
- Python实现红黑树的删除操作
- mysql中清空数据库,并重置主键为1
- 运营商管道的精细化运营之路
- 数据结构与算法分析——Hash表
- 各种软件系统架构图解析
- 计算机应用能力考试湖南成绩查询,湖南计算机等级考试成绩查询入口
- win10系统连接不上服务器,win10系统电驴连接不上服务器的解决方法
- 使用DreamweaverMX2004的搜索替换功能提高工作效率。
- 中创软件哪个部分是外包_什么是外包| 第2部分
- Codeforces 1324D(红黑树+求指定区间中大于指定值的个数)
- Java通过javacsv实现读取csv文件数据
- cocos creator 牌面翻转
- 黑马程序员————高新技术————类加载器
- ElasticSearch6.5.4快速入门
- Base64与MD5(数据加密)与ValidateCode(验证码)