Sunscreen
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12907 Accepted: 4534 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. Cow i has a minimum and maximum SPF rating (1 ≤ minSPFi ≤ 1,000; minSPFi ≤ maxSPFi ≤ 1,000) that will work. If the SPF 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 bottle i with an SPF rating SPFi (1 ≤ SPFi ≤ 1,000). Lotion bottle i 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 bottle i with space-separated integers: SPFi and coveriOutput
A single line with an integer that is the maximum number of cows that can be protected while tanning
Sample Input
3 2 3 10 2 5 1 5 6 2 4 1Sample Output
2Source
题解:现在有一群奶牛在沙滩上晒太阳,需要涂防晒霜。求所能被防晒的奶牛最多的只数。
输入:1st 两个用空格分开的整数 C L,分别代表 奶牛的头数(cows) 和 防晒霜的 瓶数( lotion )。
2nd 接下来 C行,输入 单只奶牛所需最小的防晒霜的防嗮系数 minSPF ,最大的 maxSPF,每行数据用空格隔开。
3rd 再接下来 L行 ,输入 防晒霜的防晒系数t1 和瓶数 t2 每行数据用空格隔开。
注意:不同行的防晒霜,他们的防晒系数可能一样,所以可以用类似于桶排序中的桶的使用方法 用一个数组来存储防晒霜瓶数,数组下标就是防晒系数。
思路:读入数据->以奶牛的最小防晒系数为关键字 由高到低 排列 奶牛数据所在的结构体数组->循环遍历奶牛:一只奶牛 再遍历最高防晒系数j=maxspf 到最低防晒系数j>=minspf j-- 查询 (类似于桶排序)防晒霜数组 lo[j],一旦有防晒霜在
这个区间,那么可被防晒的奶牛数量加一,对应的 lo[j] 即防晒系数spf=j 的防晒霜数量减一。条件是这个防晒霜数量不能少于1.
(所以就是贪心)
1 #include <iostream> 2 #include <stdio.h> 3 #include <cstring> 4 5 #include <algorithm> 6 using namespace std; 7 //bool vis[2509]; 8 struct cow{ 9 int mina; 10 int maxa; 11 }co[2509]; 12 13 bool cmpcow(cow x, cow y) 14 { 15 return x.mina>y.mina; 16 } 17 int lo[1009]; 18 //bool lotions(lotion x,lotion y) 19 //{ 20 // return x.spf<y.spf; 21 //} 22 int main() 23 { int c,l; 24 while(scanf("%d%d",&c,&l)!=EOF) 25 { 26 27 int sum=0; 28 // memset(vis,false,sizeof(vis)); 29 30 for(int i=1;i<=c;i++){ 31 scanf("%d%d",&co[i].mina,&co[i].maxa); 32 } 33 for(int i=1;i<=l;i++){ 34 int t1,t2; 35 scanf("%d%d",&t1,&t2);//type 36 // scanf("");//quantity 37 // lo[t1].spf=t1; 38 lo[t1]+=t2; 39 } 40 41 sort(co+1,co+c+1,cmpcow); 42 // sort(lo,lo+l,lotions); 43 44 for(int i=1;i<=c;i++){ 45 for(int j=co[i].maxa;j>=co[i].mina;j--){ 46 if(lo[j]) 47 { 48 sum++,lo[j]--; 49 break; 50 } 51 } 52 } 53 54 printf("%d",sum); 55 } 56 return 0; 57 }
。。。