2012 ACM/ICPC Asia Regional Changchun Online(Alice and bob)
Problem Description
Alice and Bob's game never ends. Today, they introduce a new game. In this game, both of them have N different rectangular cards respectively. Alice wants to use his cards to cover Bob's. The card A can cover the card B if the height of A is not smaller than B and the width of A is not smaller than B. As the best programmer, you are asked to compute the maximal number of Bob's cards that Alice can cover. Please pay attention that each card can be used only once and the cards cannot be rotated.
Input
The first line of the input is a number T (T <= 40) which means the number of test cases. For each case, the first line is a number N which means the number of cards that Alice and Bob have respectively. Each of the following N (N <= 100,000) lines contains two integers h (h <= 1,000,000,000) and w (w <= 1,000,000,000) which means the height and width of Alice's card, then the following N lines means that of Bob's.
Output
For each test case, output an answer using one line which contains just one number.
Sample Input
2 2 1 2 3 4 2 3 4 5 3 2 3 5 7 6 8 4 1 2 5 3 4
Sample Output
1 2
题意:给定待包围的矩形若干,用若干个矩形去包围它。最多可以包围多少个。
思路:贪心的思想。从小到大覆盖是满足长覆盖的情况下应该使得覆盖的宽尽量大。将矩形的长宽排序。采用标记的方法可以区分覆盖的和被覆盖的矩形。
set容器和upper_bounder函数能很好的解决
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<set>
using namespace std;
struct lmx{
int x;
int y;
int num;
};
bool is(lmx s,lmx t)
{
//if(s.num!=t.num) return s.num>t.num;
if(s.x!=t.x) return s.x<t.x;
if(s.y!=t.y) s.y<t.y;
return s.num<t.num;
}
lmx lm[200005];
int main()
{
int test,i,n,cnt;
set<int> s;
scanf("%d",&test);
while(test--)
{
s.clear();
cin>>n;
cnt=0;
for(i=0;i<n;i++)
{
scanf("%d%d",&lm[i].x,&lm[i].y);
lm[i].num=1;
}
for(i=n;i<2*n;i++)
{
scanf("%d%d",&lm[i].x,&lm[i].y);
lm[i].num=0;
}
sort(lm,lm+2*n,is);
for(i=0;i<2*n;i++)
{
if(lm[i].num==0) s.insert(lm[i].y);
else
{
if(!s.empty()&&*s.begin()<=lm[i].y)
{
set<int>::iterator it=s.upper_bound(lm[i].y);
it--;
cnt++;
s.erase(it);
}
}
}
cout<<cnt<<endl;
}
return 0;
}
#include<iostream>
#include<algorithm>
#include<set>
using namespace std;
struct lmx{
int x;
int y;
int num;
};
bool is(lmx s,lmx t)
{
//if(s.num!=t.num) return s.num>t.num;
if(s.x!=t.x) return s.x<t.x;
if(s.y!=t.y) s.y<t.y;
return s.num<t.num;
}
lmx lm[200005];
int main()
{
int test,i,n,cnt;
set<int> s;
scanf("%d",&test);
while(test--)
{
s.clear();
cin>>n;
cnt=0;
for(i=0;i<n;i++)
{
scanf("%d%d",&lm[i].x,&lm[i].y);
lm[i].num=1;
}
for(i=n;i<2*n;i++)
{
scanf("%d%d",&lm[i].x,&lm[i].y);
lm[i].num=0;
}
sort(lm,lm+2*n,is);
for(i=0;i<2*n;i++)
{
if(lm[i].num==0) s.insert(lm[i].y);
else
{
if(!s.empty()&&*s.begin()<=lm[i].y)
{
set<int>::iterator it=s.upper_bound(lm[i].y);
it--;
cnt++;
s.erase(it);
}
}
}
cout<<cnt<<endl;
}
return 0;
}
为了明天所以选择坚定的执着今天。