题目大意:给出士兵能够拿武器的最大值和最小值,再给出一些武器的重量,求最多能拿武器的数量。
解题思路:看了题很快就想到了二分图匹配,只要将武器的重量与范围值的关系进行转化即可,拍了code,结果TLE了,还是做题的时候不够淡定呀!想了下,数据量比较大,边的数量会很多,肯定会T的。然后,队友想了是贪心,推了下是正确的,反例也对。直接这样就A了,看来是想多了!具体做法是:武器和士兵能拿的值进行排序,武器直接就可以排序,主要是对士兵的能量值排序,详见code。
题目来源:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3508
code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 2500+10;
const int MAXM = 40000+100;
int n,m,ans;
int wm[MAXM],vis[MAXM];
struct point{
int minw,maxw;
}wn[MAXN];
bool cmp(const point a,const point b){
if(a.minw==b.minw) return a.maxw<b.maxw;
else if(a.minw>b.minw && a.maxw<=b.maxw) return true;
else if(b.minw>=a.minw && b.maxw<=a.maxw) return false;
return a.minw<b.minw;
}
int main(){
//freopen("input.txt","r",stdin);
while(~scanf("%d%d",&n,&m)){
ans=0;
memset(vis,0,sizeof(vis));
for(int i=0;i<n;i++)
scanf("%d%d",&wn[i].minw,&wn[i].maxw);
for(int i=0;i<m;i++)
scanf("%d",&wm[i]);
sort(wn,wn+n,cmp);
sort(wm,wm+m);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++){
if(!vis[j] && wm[j]>=wn[i].minw && wm[j]<=wn[i].maxw){
ans++;
vis[j]=1;
break;
}
}
printf("%d\n",ans);
}
return 0;
}