博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 4325
阅读量:4704 次
发布时间:2019-06-10

本文共 1473 字,大约阅读时间需要 4 分钟。

//用二分查找,先找到x小于等于m的有几个数这些数有可能y大于u即为符合。
//然后找这些数中y小于u的即为排除
//相减即可
#include<stdio.h>
#include<stdlib.h>
#define N 100100
struct node {
int x,y;
}a[N],b[N];
int n;
int cmp1(const void *a,const void *b) {//对x小到大排序
if((*(struct node *)a).x==(*(struct node *)b).x)
return (*(struct node *)a).y-(*(struct node *)b).y;
return (*(struct node *)a).x-(*(struct node *)b).x;
}
int cmp2(const void *a,const void *b) {//对y从小到大排序
if((*(struct node *)a).y==(*(struct node *)b).y)
return (*(struct node *)a).x-(*(struct node *)b).x;
return (*(struct node *)a).y-(*(struct node *)b).y;
}
int seach1(int start,int end,int u) {
int mid;
if(a[start].x>u)//最小的都大于u那么肯定没有一个了
return 0;
if(a[end].x<=u)//最大的都小于u那么肯定有end+1中可能符合
return end+1;
while(end>=start) {
          mid=(start+end)/2;
 if(a[mid].x>u)
 end=mid-1;
 else 
 start=mid+1;
 }
return start;//找到个数
}
int seach2(int start,int end,int u) {
int mid;
if(b[start].y>=u)
return 0;
if(b[end].y<u)
return end+1;
while(end>=start) {
          mid=(start+end)/2;
 if(b[mid].y>=u)
 end=mid-1;
 else 
 start=mid+1;//找到个数
 }
return start;
}
int main() {
int i,j,k,m,t,count=0;
scanf("%d",&t);
while(t--) {
scanf("%d%d",&n,&m);
for(i=0;i<n;i++) {
scanf("%d%d",&a[i].x,&a[i].y);
b[i].x=a[i].x;b[i].y=a[i].y;
}
qsort(a,n,sizeof(a[0]),cmp1);
qsort(b,n,sizeof(b[0]),cmp2);
printf("Case #%d:\n",++count);
while(m--) {
scanf("%d",&k);
i=seach1(0,n-1,k);
printf("%d",i);
j=seach2(0,n-1,k);
printf("%d\n",i-j);//相减
}
}
return 0;
}

转载于:https://www.cnblogs.com/thefirstfeeling/p/4410924.html

你可能感兴趣的文章
python生成.exe文件
查看>>
PHP面向对象(OOP)----分页类
查看>>
监听SD卡状态
查看>>
vs2017 EFCore 迁移数据库命令
查看>>
serialVersionUID的作用
查看>>
liunx trac 插件使用之GanttCalendarPlugin
查看>>
(14)嵌入式软件开发工程师技能要求总结
查看>>
[hackerrank]Closest Number
查看>>
volatile关键字
查看>>
[Android] TabLayout设置下划线(Indicator)宽度
查看>>
<潭州教育>-Python学习笔记@条件与循环
查看>>
web自动化之验证码识别解决方案
查看>>
netty接收大文件的方法
查看>>
软件工程设计之四则运算
查看>>
SpringMVC @ResponseBody 406
查看>>
Partial Tree UVALive - 7190(完全背包)
查看>>
Ubuntu安装搜狗拼音教程
查看>>
Happy Number
查看>>
Sqlserver 系统视图简单说明
查看>>
php缓存机制
查看>>