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多加了一个判断,加上去结果过了,太弱了

代码:

   

///1085422276
#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=0,f=1;
    char ch=getchar();
    while(ch<‘0‘||ch>‘9‘)
    {
        if(ch==‘-‘)f=-1;
        ch=getchar();
    }
    while(ch>=‘0‘&&ch<=‘9‘)
    {
        x=x*10+ch-‘0‘;
        ch=getchar();
    }
    return x*f;
}
//***************************************************************

struct ss
{
    int x,y;
}a[100005],b[100005];
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=0; i<n; i++)
        {
            a[i].x=read();
            a[i].y=read();
        }
        int tot=0;
        sort(a,a+n,cmp);
        for(int i=0;i<n;i++)
        {
            int flag=0;
            for(int j=i+1;j<n;j++)
            {
                if(a[j].y>a[i].y)break;
                if(a[j].x<a[i].x)continue;
                flag=1;
                break;
            }
            if(!flag)b[tot++]=a[i];
        }
        n=tot;
        for(int i=1;i<=m;i++)
        {
            lll=read();
            rrr=read();
            int ans=0;
             l=0;r=n-1;
            int aa=-1;
            while(l<=r)
            {
                int mid=(l+r)>>1;
                if(b[mid].y<=lll)
                {
                    aa=mid;
                    l=mid+1;
                }
                else r=mid-1;
            }
           for(int i=aa+1;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 0;
}

时间: 2024-12-13 14:17:02

HDU 4343 贪心的相关文章

hdu 4105 贪心思想

淋漓尽致的贪心思想 波谷一定是一位数,波峰一位数不够大的时候添加到两位数就一定够大了的. 当在寻找波谷碰到零了就自然当成波谷. 当在寻找波峰时碰到零时,将前面的波谷加到前一个波峰上,让当前的零做波谷,使得波谷的值尽量小,这就是本题最关键的贪心思想,一直想不到. 代码中:a表示前一个值,b表示当前考虑的值,tag为偶数时表示正在寻找波谷,奇数时在寻找波峰. #include<iostream> #include<cstdio> #include<cstring> #inc

HDU 4923 (贪心+证明)

Room and Moor Problem Description PM Room defines a sequence A = {A1, A2,..., AN}, each of which is either 0 or 1. In order to beat him, programmer Moor has to construct another sequence B = {B1, B2,... , BN} of the same length, which satisfies that:

hdu 2037 贪心

今年暑假不AC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 27361    Accepted Submission(s): 14439 Problem Description "今年暑假不AC?" "是的." "那你干什么呢?" "看世界杯呀,笨蛋!" &quo

HDU 4932 贪心

Miaomiao's Geometry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 191    Accepted Submission(s): 38 Problem Description There are N point on X-axis . Miaomiao would like to cover them ALL by

hdu 4292 贪心

http://acm.hdu.edu.cn/showproblem.php?pid=4296 Problem Description Have you ever heard the story of Blue.Mary, the great civil engineer? Unlike Mr. Wolowitz, Dr. Blue.Mary has accomplished many great projects, one of which is the Guanghua Building. T

hdu 4442 贪心

http://acm.hdu.edu.cn/showproblem.php?pid=4442 Problem Description WANGPENG is a freshman. He is requested to have a physical examination when entering the university. Now WANGPENG arrives at the hospital. Er-.. There are so many students, and the nu

hdu 1050(贪心算法)

Moving Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 19278    Accepted Submission(s): 6582 Problem Description The famous ACM (Advanced Computer Maker) Company has rented a floor of a

HDU2111 Saving HDU 【贪心】

Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5245    Accepted Submission(s): 2397 Problem Description 话说上回讲到海东集团面临内外交困,公司的元老也只剩下XHD夫妇二人了.显然,作为多年拼搏的商人,XHD不会坐以待毙的. 一天,当他正在苦思冥想解困良策的时

HDU 4343 多查询求区间内的最大不相交区间个数-思维&amp;贪心-卡时间&amp;二分&amp;剪枝

题意:给你一些区间,现在有m个查询,求出每个查询的区间内的最大的不相交区间个数 分析: 几天前那道说谎问题时用dp的摞箱子模型求的最大的不相交区间个数,但是这题不能用这个方法,因为这题是动态的查询,不可能每个查询dp一次,超时. 这题用贪心策略.求区间[l~r]里的最大不相交区间,贪心策略就应该先选该区间内右端点最小的,这样给以后待选的区间留下更大的空间,所以我们的做法就是先按照区间的右端点排序,然后每次查询依次挑出查询区间里右端点最小的,并且把查询区间的左端点更新为刚才挑出的区间的右端点(这个