poj3614 贪心

来源:互联网 发布:网络安全法出台的意义 编辑:程序博客网 时间:2024/05/28 11:48

 

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

Sunscreen
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 4426 Accepted: 1544

Description

To avoid unsightly burns while tanning, each of the C (1 ≤ C ≤ 2500) cows must cover her hide with sunscreen when they're at the beach. Cowi has a minimum and maximum SPF rating (1 ≤ minSPFi ≤ 1,000;minSPFimaxSPFi ≤ 1,000) that will work. If theSPF rating is too low, the cow suffers sunburn; if the SPF rating is too high, the cow doesn't tan at all........

The cows have a picnic basket with L (1 ≤ L ≤ 2500) bottles of sunscreen lotion, each bottlei with an SPF rating SPFi (1 ≤SPFi ≤ 1,000). Lotion bottlei can cover coveri cows with lotion. A cow may lotion from only one bottle.

What is the maximum number of cows that can protect themselves while tanning given the available lotions?

Input

* Line 1: Two space-separated integers: C and L
* Lines 2..C+1: Line i describes cow i's lotion requires with two integers:minSPFi and maxSPFi
* Lines C+2..C+L+1: Line i+C+1 describes a sunscreen lotion bottlei with space-separated integers: SPFi andcoveri

Output

A single line with an integer that is the maximum number of cows that can be protected while tanning

Sample Input

3 23 102 51 56 24 1

Sample Output

2

Source

USACO 2007 November Gold

 

题目大意:有C头牛,L种防晒霜,每头牛给出可以忍受的光强区间,每种防晒霜给出可以使牛的光强固定的值和这种防晒霜的数量,默认一开始光强无限大,问使用防晒霜后最多使几头牛能好好晒太阳。

 

思路:容易想到贪心策略,先对牛的最低限度从小到大排序,对防晒霜每一种的固定光强从小到大排序,遍历防晒霜的同时,维护一个牛的最大忍受光强小根堆,保证最优策略。

#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;

struct node
{
 int min,max;
};

node cow[2505];

struct node1
{
 int spf,cover;
};
node1 lotion[2505];

int cmp(node &a,node &b)
{
 return a.min<b.min;
}
int cmp1(node1 &a,node1 &b)
{
 return a.spf<b.spf;
}

int main()
{
 int C,L;
 cin>>C>>L;
 int i;
 for(i=1;i<=C;i++)
  scanf("%d%d",&cow[i].min,&cow[i].max);
 for(i=1;i<=L;i++)
  scanf("%d%d",&lotion[i].spf,&lotion[i].cover);
 sort(cow+1,cow+1+C,cmp);
 sort(lotion+1,lotion+1+L,cmp1);
 priority_queue<int,vector<int>,greater<int>>que;
 int res=0;
 int j=1;
 for(i=1;i<=L;i++)
 {
  while(j<=C&&cow[j].min<=lotion[i].spf)
  {
   que.push(cow[j].max);
   j++;
  }
  while(!que.empty()&&lotion[i].cover)
  {
   int t=que.top();
   que.pop();
   if(t>=lotion[i].spf)
   {
    res++;
   lotion[i].cover--; 
   }
  }
 }
 printf("%d\n",res);
}

 

0 0
原创粉丝点击