Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6410 | Accepted: 2239 |
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 coveri
Output
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 1
Sample Output
2
题目大意:具体单词我也不太懂,不过大体意思就是每一只奶牛都有着需要的spf最大值和最小值,然后现在有一些类似防晒霜
可以提供spf,如果一只奶牛被保护,意思就是涂的防晒液提供的spf刚好在要求的最大值和最小值之间,问你最多能保护度多
少只奶牛。
思路分析:目前我只会用贪心做,很显然,需要进行排序,但是是按照最大值还是最小值排序呢,用最小值排序很容易举出反例,
按最大值排序的时候很多情况都是符合的,防晒液按照能够提供的spf从小到大排序是毫无疑问的,然后遍历走一遍就能求出最多
的奶牛数了。
讲道理真让我证明我也不会,但是感觉是这样,也许这就是贪心的精髓吧,在没有其他思路的情况下贪心往往能有奇妙的作用。
另外,要细心!!!内层循环j写成了iwa了四发,555555555555.
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
const int maxn=2500+10;
struct nod
{
int a;
int b;
};
nod cow[maxn],s[maxn];
bool cmp1(nod x,nod y)
{
return x.b==y.b?x.a<y.a:x.b<y.b;
}
bool cmp2(nod x,nod y)
{
return x.a<y.a;
}
int main()
{
int c,l;
int m,n;
while(scanf("%d%d",&c,&l)!=EOF)
{
for(int i=0;i<c;i++)
{
scanf("%d%d",&cow[i].a,&cow[i].b);
}
for(int i=0;i<l;i++)
{
scanf("%d%d",&s[i].a,&s[i].b);
}
sort(cow,cow+c,cmp1);
sort(s,s+l,cmp2);
int cut=0;
for(int i=0;i<c;i++)
{
for(int j=0;j<l;j++)
{
if(s[j].a>=cow[i].a&&s[j].a<=cow[i].b&&s[j].b>0)
{
cut++;
s[j].b--;
break;
}
}
}
cout<<cut<<endl;
}
return 0;
}