题目链接:http://codeforces.com/gym/100712/attachments
将各年龄的人数、各袋糖果个数相同的袋数统计一下,然后从糖果个数小的袋开始匹配年龄小的,如果能够匹配所有年龄,则成功,否则失败。
附上AC代码:
#include <bits/stdc++.h>
//#pragma comment(linker, "/STACK:102400000, 102400000")
using namespace std;
map<int, int> age, candy;
map<int, int>::iterator ita, itc;
int n, m;
int main(){
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int T;
scanf("%d", &T);
while (T--){
scanf("%d%d", &n, &m);
int t;
age.clear();
candy.clear();
while (n--){
scanf("%d", &t);
++age[t];
}
while (m--){
scanf("%d", &t);
++candy[t];
}
ita = age.begin();
itc = candy.begin();
while (ita!=age.end() && itc!=candy.end()){
while (itc!=candy.end() && ita->second>itc->second)
candy.erase(itc++);
if (itc == candy.end())
break;
age.erase(ita++);
candy.erase(itc++);
}
printf(age.empty() ? "YES\n" : "NO\n");
}
return 0;
}