The War(Zoj 3508)

D - The War

Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

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

For each case, output one integer represents the maximal number of armed soldier you can get.

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
 
   
 
   
解题思路
典型的贪新算法。先把所给范围的最大值按照从小到大排列,再把所给的重量从小到大排序,然后尽可能多的枚举。
 
   
 
   
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
struct node
{
    int min;
    int max;
} p[2510];
int cmp(struct node a,struct node b)
{
    return a.max<b.max;
}
int main()
{
    int n,m,i,j,count;
    int w[40010];
    while(~scanf("%d %d",&n,&m))
    {
        count=0;
        for(i=0;i<n;i++)
            scanf("%d %d",&p[i].min,&p[i].max);
        for(i=0;i<m;i++)
            scanf("%d",&w[i]);
        sort(p,p+n,cmp);
        sort(w,w+m);
        for(i=0;i<m;i++)
        {
            for(j=0;j<n;j++)
            {
                if(w[i]>=p[j].min&& w[i]<=p[j].max&&w[i]!=-1&&p[j].max!=-1)
                {
                    count++;
                    w[i]=-1;
                    p[j].max=-1;
                }
            }
        }
        printf("%d\n",count);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rocky0429

一块也是爱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值