现在的位置: 首页 > 算法 > 正文

zoj 贪心

2018年04月15日 算法 ⁄ 共 1532字 ⁄ 字号 评论关闭
D - The War

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 <algorithm>
using namespace std;

int maxx[5000];
int minn[5000];
int w[50000];
int main()
{
    int n,m;
    int i,j;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(i=0; i<n; i++)
        {
            scanf("%d%d",&minn[i],&maxx[i]);
        }

        for(i=0; i<m; i++)
        {
            scanf("%d",&w[i]);
        }
        int t;
        for(i=0; i<n; i++)
        {
            for(j=0; j<n-1-i; j++)
            {
                if(maxx[j]>=maxx[j+1])
                {
                    t  = maxx[j];
                    maxx[j] = maxx[j+1];
                    maxx[j+1] = t;
                    t = minn[j];
                    minn[j] = minn[j+1];
                    minn[j+1] = t;
                }
            }
        }
        sort(w,w+m);
        int ans=0;
        for(i=0; i<m; i++)
        {
            for(j=0; j<n; j++)
            {
                if(w[i]<=maxx[j]&&w[i]>=minn[j])
                {
                    ans++;
                    maxx[j]=0;
                    minn[j]=0;
                    w[i]=0;
                    break;
                }

            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

抱歉!评论已关闭.