您的位置:首页 > 其它

hdu 6003 Problem Buyer(贪心)

2017-09-29 20:15 344 查看


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. Ai≤c≤Bi .
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

∙1≤T≤100.
∙1≤N,M≤105.
∙1≤Ai≤Bi≤109.
∙1≤Ci≤109.

 

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
;
int c
;
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: