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 areM (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 minWiand 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?
InputThere 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.
<b< dd="">
Output For each case, output one integer represents the maximal number of armed soldier you can get.<b< dd="">
Sample Input3 3 1 5 3 7 5 10 4 8 9 2 2 5 10 10 20 4 21
<b< dd="">
Sample Output2 0
#include<bits/stdc++.h>
using namespace std;
int br[100010];
struct node
{
int p;
int q;
}ar[10010];
bool cmp1(node a,node b)
{
if(a.p!=b.p) return a.p>b.p;
return a.q<b.q;
}
bool cmp2(int a,int b)
{
return (a>b);
}
int main()
{
int m,n,i,j,k,sum;
while(scanf("%d%d",&m,&n)!=EOF)
{
sum=0;
for(i=1;i<=m;i++)
{
scanf("%d%d",&ar[i].p,&ar[i].q);
}
for(i=1;i<=n;i++)
{
scanf("%d",&br[i]);
}
sort(ar+1,ar+1+m,cmp1);
sort(br+1,br+1+n,cmp2);
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
if(ar[i].p<=br[j]&&ar[i].q>=br[j])
{
sum++;
br[j]=0;
break;
}
}
}
printf("%d\n",sum);
}
return 0;
}
分清楚怎样才能使利益最大化,按照最小承重量降序然后最大承重量升序用sort函数进行二次排序,然后再将武器的重量按照降序排序,为了和士兵的承重量进行匹配,这样使得利益最大化,贪心的思想。