0x07 贪心

链接:https://ac.nowcoder.com/acm/contest/1003/A
来源:牛客网

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?
输入描述:

  • 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

输出描述:

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

示例
输入:
3 2
3 10
2 5
1 5
6 2
4 1
输出:
2

思路:

奶牛已被按照minSPF递减排序,所以每一个不低于当前奶牛minSPF值的防晒霜,都不会低于后面其他奶牛的minSPF,当前选SPF大的使用。

debug:

6的防晒霜可能有多瓶
非全局数组要初始化

#include<iostream>
#include<algorithm>
using namespace std;
struct node{
    int l,r;
};
bool cmp(struct node x,struct node y){
    return x.l>y.l;
} 
bool cmp2(int x,int y){
    return x>y;
}
int b[2600],res[2600];//res没有初始化,可以6 2有多个 
struct node a[2600];
int main(){
    int C,L;
    cin>>C>>L;
    for(int i=0;i<C;i++){
        cin>>a[i].l>>a[i].r;
    }
    sort(a,a+C,cmp);
    int t=0,mm=0;
    for(int i=0;i<L;i++){
        cin>>b[i]>>t;
        res[b[i]]+=t; 
    }
    sort(b,b+L,cmp2);
    int num=0;
    for(int i=0;i<C;i++){
        for(int j=0;j<L;j++){
            if(res[b[j]]>0&&a[i].l<=b[j]&&a[i].r>=b[j]){
                res[b[j]]--;
                num++;
                break;
            }
        }
    }
    cout<<num<<endl;
    return 0;
} 
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容