HDU 4268-Alice and Bob( STL: set)
題意: 要求找出 Alice 的卡片覆蓋 Box的卡片的最大張數, 長和寬都大於等於另一張卡片則能夠覆蓋..
思路: 貪心思想+STL
一開始想到的是賽馬問題, 從最大的開始比, WA 了, 問了下靜姐才明白, 不能從最大比, 而應該從最小比, 因為 最小才是最難匹配到的...
比如這個例子: A: (2,2) ,(2,4) ,(3,5)
B: (1,5) , (2,4), (4,6) 若從大到小比較則答案為1, 實際上最大的答案為 2...
接著 又WA 了N 遍 , 沒有 容器清空!!!!
# lower_bound 的使用:
se[]={1,3,4,5,6};
set<int> se;
set<int>::iterator it=se.lower_bound(num) ; // 返回到第一個大於等於 num 的數的指標~ lower_bound(2)=3, lower_bound(3)=3,目標物件存在即為目標物件的位置,不存在則為後一個位置.
# upper_bound的使用:
// 返回>物件的第一個位置, upper_bound(2)=3,upper_bound(3)=4
#binary_search:判斷是否存在某個物件
#equal_bound: 返回由lower_bound和upper_bound返回值構成的pair,也就是所有等價元素區間。
equal_bound有兩個需要注意的地方:
1. 如果返回的兩個迭代器相同,說明查詢區間為空,沒有這樣的值
2. 返回迭代器間的距離與迭代器中物件數目是相等的,對於排序區間,他完成了count和find的雙重任務
以上的函式處理的都是有序序列~
以下的函式不需要有序序列~~~~
#count:計算物件區間中的數目。
#find:返回第一個物件的位置。
查詢成功的話,find會立即返回,count不會立即返回(直到查詢完整個區間),此時find效率較高。
因此除非是要計算物件的數目,否則不考慮count。
CODE:
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<set>
using namespace std;
struct node
{
int h,w;
}a[100005],b[100005];
bool cmp(node a,node b)
{
if(a.h==b.h) return a.w<b.w;
return a.h<b.h;
}
multiset<int> mul;
int main()
{
freopen("in.in","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
mul.clear();
int n,hh,ww;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d%d",&a[i].h,&a[i].w);
}
for(int i=0;i<n;i++)
{
scanf("%d%d",&b[i].h,&b[i].w);
}
sort(a,a+n,cmp); //printf("%d %d\n",a[1].h,a[1].w);
sort(b,b+n,cmp);
int ans=0;
int k=0;
for(int i=0;i<n;i++)
{
while(k<n && b[k].h<=a[i].h)
{
mul.insert(b[k].w);
k++;
}
if(mul.size() >= 1)
{
multiset<int>::iterator it=mul.lower_bound(a[i].w);
{
if(it==mul.end()) it--;
if(it!=mul.begin() && *it>a[i].w) it--;
if(*it <= a[i].w)
{
mul.erase(it);
ans++;
}
}
}
}
printf("%d\n",ans);
}
return 0;
}