hdu 6003 Problem Buyer(贪心)

Problem Buyer

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 195    Accepted Submission(s): 60


Problem Description
TopSetter is an organization that creates problems. They’ve prepared N problems with estimated difficulty score in range [  Ai,Bi  ]. TopHoster would like to host a contest consisting of M problems.
The  ith  problem should be of difficulty score  Ci . The  ith  problem from TopSetter can be used in the contest if and only if its estimated difficulty score range  [Ai,Bi]  covers the difficulty score c of its target problem in the contest, i.e.  AicBi  . Hosting a contest with M problems needs tohave M distinct problems which satisfy the required difficulty scores for each problem.
Unfortunately, TopSetter doesn’t provide a service to buy specific problems. You can only request a problem set containing K problems and they will give you K distinct problems from all the N problems, but you don’t know which problems will be given.
As TopSetter is the only problem provider for TopHoster, TopHoster would like to know the least number K of problems they need to buy to make sure they can host a contest.
 

Input
The first line of the input gives the number of test cases, T. T test cases follow. Each test case starts with 2 integers, N and M. Then N lines follow, each line consists of 2 integers representing the difficulty score range of the  ith  problem,  AiandBi  . The last line of each test case consists of M integers representing the target difficulty scores of the M problems  Ci  .
 

Output
For each test case, output one line containing “Case #x: y”, where x is the test case number (starting from 1) and y is the least number of problems which the TopHoster needs to buy.
Output “IMPOSSIBLE!” if it’s impossible.

limits


1T100.
1N,M105.
1AiBi109.
1Ci109.
 

Sample Input
  
  
3 3 1 1 4 2 3 5 6 3 3 2 1 10 3 4 7 9 4 8 3 3 1 2 5 6 8 9 1 5 10
 

Sample Output
  
  
Case #1: 2 Case #2: 2 Case #3: IMPOSSIBLE!



读了好久连题目意思都读不懂。。。

真是一道超级难想的贪心题

题意:给出n个区间,m个数,让你任意选k个数,使一定能包含这m个数,一个区间只能包含一个数 求最小的k

解:难就难在 任意 二字 解题思路是 求出一个数的所有满足区间 那么这个区间取一个数即可包含这个数 那么ans=n-size+1


解题报告:http://www.cnblogs.com/xiaochaoqun/p/7243606.html

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
#include <bits/stdc++.h>
using namespace std;
const int N =1e5+10;
typedef long long LL;
const LL mod = 1000000007;
typedef pair<int,int>pi;
struct node
{
    int l, r;
    bool operator <(const node &A)const
    {
        if(l!=A.l) return l<A.l;
        else return r>A.r;
    }
}p[N];
int c[N];
priority_queue<int,vector<int>,greater<int> >q;

int main()
{
    int t, ncase=1;
    scanf("%d", &t);
    while(t--)
    {
        int n, m;
        scanf("%d %d", &n, &m);
        for(int i=0;i<n;i++) scanf("%d %d", &p[i].l,&p[i].r);
        for(int i=0;i<m;i++) scanf("%d", &c[i]);
        sort(p,p+n);
        sort(c,c+m);
        int ans=-1;
        while(!q.empty()) q.pop();
        int pos=0;
        for(int i=0;i<m;i++)
        {
            while(pos<n&&p[pos].l<=c[i])
            {
                if(p[pos].r>=c[i])
                {
                    q.push(p[pos].r);
                }
                pos++;
            }
            while(!q.empty()&&q.top()<c[i]) q.pop();
            if(q.empty())
            {
                ans=-1;
                break;
            }
            ans=max(ans,n-(int)q.size()+1);
            q.pop();
        }
        if(ans==-1) printf("Case #%d: IMPOSSIBLE!\n",ncase++);
        else printf("Case #%d: %d\n",ncase++,ans);
    }
    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值