HDU 4343 D - Interval query 二分贪心
D - Interval query
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88748#problem/D
Description
This is a very simple question. There are N intervals in number axis, and M queries just like “QUERY(a,b)” indicate asking the maximum number of the disjoint intervals between (a,b) .
Input
There are several test cases. For each test case, the first line contains two integers N, M (0<N, M<=100000) as described above. In each following N lines, there are two integers indicate two endpoints of the i-th interval. Then come M lines and each line contains two integers indicating the endpoints of the i-th query.
You can assume the left-endpoint is strictly less than the right-endpoint in each given interval and query and all these endpoints are between 0 and 1,000,000,000.
Output
For each query, you need to output the maximum number of the disjoint intervals in the asked interval in one line.
Sample Input
3 2
1 2
2 3
1 3
1 2
1 3
Sample Output
1
2
HINT
题意
给你n个线段
m次询问,问你在ab区间内,最多放下几个不能相互重叠的线段
题解:
直接n*m暴力
首先,我们按照右端点排序,然后能放进去就放进去,这种策略肯定是最优的
然后就暴力吧……15s
有两个想一想好像并咩有什么用的剪枝:
预处理出来第一个能放进区间的线段
预处理出哪些线段永远都不可能选的
代码:
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#define maxn 110000
using namespace std; struct node
{
int x,y;
};
bool cmp(node a,node b)
{
if(a.y==b.y)
return a.x<b.x;
return a.y<b.y;
}
node a[maxn];
node b[maxn];
node qq[maxn];
int cc[maxn];
//node b[maxn];
int tot=;
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
tot=;
memset(a,,sizeof(a));
memset(b,,sizeof(b));
memset(qq,,sizeof(qq));
memset(cc,,sizeof(cc));
for(int i=;i<n;i++)
scanf("%d %d",&a[i].x,&a[i].y);
sort(a,a+n,cmp);
for(int i=;i<n;i++)
{
int flag=;
for(int j=i+;j<n;j++)
{
if(a[j].y>a[i].y)
break;
if(a[j].x<a[i].x)
continue;
flag=;
break;
}
if(!flag)
b[tot++]=a[i];
}
int x,y,ans;
for(int i=;i<m;i++)
{
scanf("%d%d",&qq[i].x,&qq[i].y);
int l=,r=tot-;
x = qq[i].x;
y = qq[i].y;
while(l<=r)
{
int mid=(l+r)>>;
if(b[mid].y<=x)
l=mid+;
else
r=mid-;
}
cc[i]=l;
}
for(int i=;i<m;i++)
{
x = qq[i].x,y = qq[i].y;
ans=;
int tmp=x;
int l = cc[i];
for(int j=l;j<tot;j++)
{
if(b[j].y<=y)
{
if(b[j].x<tmp)
continue;
tmp = b[j].y;
ans++;
}
else
break;
}
printf("%d\n",ans);
}
}
}
HDU 4343 D - Interval query 二分贪心的更多相关文章
- 【HDU 4343】Interval query(倍增)
BUPT2017 wintertraining(15) #8D 题意 给你x轴上的N个线段,M次查询,每次问你[l,r]区间里最多有多少个不相交的线段.(0<N, M<=100000) 限 ...
- HDU 4343 Interval query(贪心 + 倍增)
题目链接 2012多校5 Problem D 题意 给定$n$个区间,数字范围在$[0, 10^{9}]$之间,保证左端点严格大于右端点. 然后有$m$个询问,每个询问也为一个区间,数字范围在$[ ...
- HDU 4343 贪心
D - Interval queryTime Limit: 1.5 Sec Memory Limit: 256 MB Description This is a very simple questio ...
- Codeforces Gym 100231B Intervals 线段树+二分+贪心
Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...
- hdu 3433 A Task Process 二分+dp
A Task Process Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- 2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 二分+贪心
/** 题目:2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 链接:http://codeforces.com/gym/101194 题意:给n个木块,堆 ...
- 【bzoj2097】[Usaco2010 Dec]Exercise 奶牛健美操 二分+贪心
题目描述 Farmer John为了保持奶牛们的健康,让可怜的奶牛们不停在牧场之间 的小路上奔跑.这些奶牛的路径集合可以被表示成一个点集和一些连接 两个顶点的双向路,使得每对点之间恰好有一条简单路径. ...
- HDU 3622 Bomb Game(二分+2-SAT)
Bomb Game Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- Codeforces_732D_(二分贪心)
D. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...
随机推荐
- 【转】cocos2d-x 3x Sprite3D
Sprite3D Sprite3D works in many ways like a normal Sprite. Sprite3D is a three-dimensional model tha ...
- 她让我懂得了怎样学习Flash
原文:http://www.asv5.cn/blog/article.asp?id=169 最近忙着寻找两样丢失了很久的东西,都是她帮我找回来的,第一样叫做自信,第二样叫做梦想.也正因为有了她,我才从 ...
- 35、Android 性能优化、内存优化
http://blog.csdn.net/a_asinceo/article/details/8222104 http://blog.csdn.net/a_asinceo/article/detail ...
- ASP.NET导出excel表方法汇总
asp.net里导出excel表方法汇总 1.由dataset生成 public void CreateExcel(DataSet ds,string typeid,string FileName) ...
- 使用jQuery Mobile实现通讯录
jQuery Mobile 通讯录 拨打电话作者:方倍工作室 地址: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional/ ...
- php里面为什么header之前有输出报错 源码分析
众所周知,php 里面 header之前有输出的话,会报错,例如下面这样 就这个错误,我们开始查阅php源代码,到底是怎样做的,至于php源代码分析,安装,和调试时怎样配置的,我会专门写一篇文章去 ...
- 链表回文串判断&&链式A+B
有段时间没有练习了,链表回文串判断用到了栈.链式A+B将没有的项用0补充.链表有没有头节点,及结点和链表的区别,即pNode和pHead. //#include<iostream> //u ...
- JAVA中的数据结构——集合类(线性表:Vector、Stack、LinkedList、set接口;键值对:Hashtable、Map接口<HashMap类、TreeMap类>)
Java的集合可以分为两种,第一种是以数组为代表的线性表,基类是Collection:第二种是以Hashtable为代表的键值对. ... 线性表,基类是Collection: 数组类: person ...
- mark标签:
mark元素表示页面中需要突出或高亮显示的内容,在搜索结果中也常常出现,比如检索结果中的关键词高亮显示. 案例:[html]<!DOCTYPE HTML><html> & ...
- Java邮件服务学习之五:邮箱服务服务端 Apache
Apache James(Java Apache Mail Enterprise Server)是Apache组织的子项目之一,完全采用纯Java技术开发,实现了SMTP.POP3与NNTP等多种邮件 ...