Description
A war had broken out because a sheep from your kingdom ate some grasses which belong to your neighboring kingdom. The counselor of your kingdom had to get prepared for this war. There are N (1 <= N <= 2500) unarmed soldier in your kingdom and there are M (1 <= M <= 40000) weapons in your arsenal. Each weapon has a weight W (1 <= W <= 1000), and for soldier i, he can only arm the weapon whose weight is between minWi and maxWi ( 1 <= minWi <= maxWi <= 1000). More armed soldier means higher success rate of this war, so the counselor wants to know the maximal armed soldier he can get, can you help him to win this war?
Input
There multiple test cases. The first line of each case are two integers N, M. Then the following N lines, each line contain two integers minWi, maxWi for each soldier. Next M lines, each line contain one integer W represents the weight of each weapon.
Output
Sample Input
3 3 1 5 3 7 5 10 4 8 9 2 2 5 10 10 20 4 21
Sample Output
2 0
思路:按每个人所要求的武器的最大重量从小到大排序,再按给定的武器的重量从小到大排序,然后枚举所有人尽可能多的找满足要求的武器,
AC代码:
#include<iostream> #include<cstdio> #include<algorithm> #include<string.h> #include<string> #include<set> using namespace std; typedef struct { int x; int y; }Node; Node s[2505]; bool cmp(Node a,Node b) {return a.y<b.y;} int main() { int n,m; while(~scanf("%d%d",&n,&m)) { for(int i=0;i<n;++i) scanf("%d%d",&s[i].x,&s[i].y); sort(s,s+n,cmp); multiset<int>Q; for(int i=1;i<=m;++i) { int a; scanf("%d",&a); Q.insert(a); } int ans=0; multiset<int>::iterator it; for(int i=0;i!=n;++i) { it=Q.lower_bound(s[i].x); if(it!=Q.end()&&*it<=s[i].y) ans++,Q.erase(it); } printf("%d\n",ans); }return 0; }
本文介绍了一种用于解决战争中武器分配问题的算法。通过将士兵和武器按特定条件排序,并采用迭代方式为每个士兵匹配合适的武器,从而实现最大化武装士兵数量的目标。
1289

被折叠的 条评论
为什么被折叠?



