poj 3614

http://poj.org/problem?id=3614

题意:有n头奶牛想要晒太阳,但他们每个人对太阳都有不同的耐受程度,也就是说,太阳不能太大也不能太小,现在有一种防晒霜,涂抹这个防晒霜可以把太阳的强度固定到一个值

求一共有多少头奶牛可以晒太阳

 1 #include <stdio.h>
 2 #include <queue>
 3 #include <stdlib.h>
 4 using namespace std;
 5 
 6 int m,n;
 7 struct co{
 8     int mi,ma;
 9 }cow[ 2555 ];
10 
11 struct suns{
12     int num,val;
13 }bot[ 2555 ];
14 int cmp(const void *a,const void *b)
15 {
16     return (*(co *)a).mi-(*(co *)b).mi;
17 }
18 int cmp1(const void *a,const void *b)
19 {
20     return (*(suns *)a).val-(*(suns *)b).val;
21 }
22 
23 int main()
24 {
25     int c,l,ans = 0,tmp = 1;
26     priority_queue<int, vector<int>, greater<int> >s;       //构建一个大值优先的优先队列
27     scanf("%d%d",&c,&l);
28     for(int i = 1 ; i <= c ; i++)
29         scanf("%d%d",&cow[i].mi,&cow[i].ma);
30     for(int i = 1 ; i <= l ; i++)
31         scanf("%d%d",&bot[i].val,&bot[i].num);
32     qsort(cow,c + 1,sizeof(cow[0]),cmp);                     //对奶牛和防晒霜进行排序。
33     qsort(bot,l + 1,sizeof(bot[0]),cmp1);
34     for(int i = 1 ; i <= l ; i++)
35     {
36         while(tmp <= c && cow[tmp].mi <= bot[i].val)      //把可以用这种防晒霜奶牛进队列
37         {
38             s.push(cow[tmp++].ma);
39         }
40         while(!s.empty()&&bot[i].num)                       //然后计算这一种防晒霜可以在几头牛上用
41         {
42             int x = s.top();
43             s.pop();
44             if(x<bot[i].val) continue;
45             ans++;
46             bot[i].num--;
47         }
48     }
49     printf("%d
",ans);
50     return 0;
51 }
原文地址:https://www.cnblogs.com/Tree-dream/p/5977969.html