zoj 3508 The War
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
/*这算是我自己写的第一个贪心题吧,做题的时候感觉是贪心,但在之前没看过这算法,经常听他们说起。这道题是我进入acm院队后第二次积分赛时做的题,比赛时比较粗心,没过。后来再看一下就过了。
/*下面把我的代码发出来
#include <stdio.h> #include <stdlib.h> struct weight { int min; int max; }w[2500]; int com(const void *x,const void *y) { weight *p1 = (weight *)x; weight *p2 = (weight *)y; if(p1->max != p2->max) return p1->max -p2->max; else return p1->min - p2->min; } int cmp(const void *x,const void *y) { int *p1 = (int *)x; int *p2 = (int *)y; return *p1 - *p2; } int main() { int i,n,m,*pw,j; while(scanf("%d %d",&n,&m) != EOF) { int sum = 0; pw = new int [m]; for(i = 0;i < n;i ++) scanf("%d %d",&w[i].min,&w[i].max); for(i = 0;i < m;i ++) scanf("%d",&pw[i]); qsort(w,n,sizeof(weight),com); qsort(pw,m,sizeof(int),cmp); for(i = 0;i <= m-1;i ++) { for(j = 0;j < n;j ++) { if(pw[i] >= w[j].min && pw[i] <= w[j].max && w[j].max!= 0 && pw[i]!=0) { sum ++; w[j].max = 0; pw[i] = 0; } } } printf("%d\n",sum); } return 0; }