HDU 4343 贪心
D - Interval query
Time Limit: 1.5 Sec
Memory Limit: 256 MB
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
题意
给你一些区间,现在有m个查询,求出每个查询的区间内的最大的不相交区间个数
题解:
二分,比赛的时候这样写,我艹了一直超时,赛后看了qscqesze多加了一个判断,加上去结果过了,太弱了
代码:
///
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
#include <typeinfo>
#include <map>
typedef long long ll;
using namespace std;
#define inf 10000000
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
//*************************************************************** struct ss
{
int x,y;
}a[],b[];
int lll;
int rrr;
bool cmp(ss a,ss b)
{
return a.y<b.y||(a.y==b.y&&a.x>b.x);
}
int n,l,r;
int main()
{
int m;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=; i<n; i++)
{
a[i].x=read();
a[i].y=read();
}
int tot=;
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];
}
n=tot;
for(int i=;i<=m;i++)
{
lll=read();
rrr=read();
int ans=;
l=;r=n-;
int aa=-;
while(l<=r)
{
int mid=(l+r)>>;
if(b[mid].y<=lll)
{
aa=mid;
l=mid+;
}
else r=mid-;
}
for(int i=aa+;i<n;i++)
{
if(b[i].y<=rrr)
{
if(b[i].x>=lll)ans++,lll=b[i].y;
}else break;
}
printf("%d\n",ans);
} }
return ;
}
HDU 4343 贪心的更多相关文章
- HDU 4343 D - Interval query 二分贪心
D - Interval queryTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest ...
- HDU 4343 Interval query(贪心 + 倍增)
题目链接 2012多校5 Problem D 题意 给定$n$个区间,数字范围在$[0, 10^{9}]$之间,保证左端点严格大于右端点. 然后有$m$个询问,每个询问也为一个区间,数字范围在$[ ...
- Hdu 5289-Assignment 贪心,ST表
题目: http://acm.hdu.edu.cn/showproblem.php?pid=5289 Assignment Time Limit: 4000/2000 MS (Java/Others) ...
- hdu 4803 贪心/思维题
http://acm.hdu.edu.cn/showproblem.php?pid=4803 话说C++还卡精度么? G++ AC C++ WA 我自己的贪心策略错了 -- 就是尽量下键,然后上 ...
- hdu 1735(贪心) 统计字数
戳我穿越:http://acm.hdu.edu.cn/showproblem.php?pid=1735 对于贪心,二分,枚举等基础一定要掌握的很牢,要一步一个脚印走踏实 这是道贪心的题目,要有贪心的意 ...
- hdu 4974 贪心
http://acm.hdu.edu.cn/showproblem.php?pid=4974 n个人进行选秀,有一个人做裁判,每次有两人进行对决,裁判可以选择为两人打分,可以同时加上1分,或者单独为一 ...
- hdu 4982 贪心构造序列
http://acm.hdu.edu.cn/showproblem.php?pid=4982 给定n和k,求一个包含k个不相同正整数的集合,要求元素之和为n,并且其中k-1的元素的和为完全平方数 枚举 ...
- HDU 2307 贪心之活动安排问题
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2037 今年暑假不AC Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 1052 贪心+dp
http://acm.hdu.edu.cn/showproblem.php?pid=1052 Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS ...
随机推荐
- Jetty和tomcat的比较
转自: http://blog.csdn.net/classicbear/article/details/6968930 相同点: 1. Tomcat和Jetty都是一种Servlet引擎, ...
- l来自wentao:项目加入缓存(redis),实时调试时用 -----可视化缓存,flushdb
下文来自segmentfault,网站:一个交流网站:https://segmentfault.com/ 在做一个项目时如果加入缓存(例如redis),我如何进行实时调试呢? 缓存 高并发 架构 gz ...
- this prototype 闭包 总结
this对象 整理下思路: 一般用到this中的情景: 1.构造方法中 function A(){ this.name="yinshen"; } var a=new A(); co ...
- CSS居中完全解决方案
上次面试面试官问到了,问了个定宽局中和不定宽局中,下来我把所有有关CSS居中都总结了一下 原文摘自我的前端博客,欢迎大家来访问 http://www.hacke2.cn 水平居中 行内元素 把行内元素 ...
- 为dedecms文章列表页标题增加序号,第二页开始才显示第x页
想必大伙建站都会写文章,随着时间的推移,你的智慧结晶会越来越多,一般的建站程序早帮你想好了,把这些文章做成一个列表,比如dedecms栏目列表,便于观众浏览,但有个问题就是dedecms文章列表页标题 ...
- java笔记--使用SwingWoker类完成耗时操作
使用SwingWoker类完成耗时操作: 对于Swing中的耗时操作,通常要在一个新的线程中运行,以免程序"假死". 在java6.0中,可以用SwingWoker类来完成 Swi ...
- 淘宝(阿里百川)手机客户端开发日记第七篇 Service,Handler和Thread
现在我们已经已经知道android有Service,Handler和Thread这些内容了,但是我想应该还有很多人对此并不是很清楚他们之间的区别! (1)Service 是运行在后端的程序,不与UI直 ...
- 在CentOS 7 上搭建LAMP
导读 要求:httpd的动态和静态资源分为两台主机提供,mysql也用单独一台主机.httpd服务提供虚拟主机,一个虚拟主机用于提供phpMyAdmin:另一个虚拟主机用于提供wordpress.安装 ...
- 谷歌浏览器 DEV Tools
谷歌浏览器如今是Web开发者们所使用的最流行的网页浏览器.伴随每六个星期一次的发布周期和不断扩大的强大的开发功能,Chrome变成了一个必须的工具.大多数可能熟悉关于chorme的许多特点,例如使用c ...
- ZOJ3741 状压DP Eternal Reality
E - Eternal Reality Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu S ...