zoukankan      html  css  js  c++  java
  • 牛客算法周周练3

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

    时间限制:C/C++ 1秒,其他语言2秒
    空间限制:C/C++ 32768K,其他语言65536K
    64bit IO Format: %lld

    题目描述

    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

    示例1

    输入

    3 2
    3 10
    2 5
    1 5
    6 2
    4 1

    输出

    2

    题意:

    有c只牛,l种防晒液,每种牛有一个舒适范围,防晒液能够让牛位于某个值SPF,但有数量限制,问最多能让几头牛处在舒适范围内

    题解:

    这题我们贪心就好了,我们将奶牛的最小SPFi按照从小到大排序,同时将防晒乳液的SPFi从小到大排序

    那么我们遍历防晒液,对于每种防晒液,我们将之前的最小SPFi值比这种防晒乳液SPFi值小的牛加到优先队列里,然后每次我们优先让最大SPFi小的奶牛使用当前这种防晒乳液。

     1 #include <bits/stdc++.h>
     2 typedef long long LL;
     3 #define pb push_back
     4 const int INF = 0x3f3f3f3f;
     5 const double eps = 1e-8;
     6 const int mod = 1e9+7;
     7 const int maxn = 1e5+10;
     8 using namespace std;
     9 
    10 struct cow
    11 {
    12     int l,r;
    13 }A[3005];
    14 struct bo
    15 {
    16     int val;
    17     int num;
    18 }B[3005];
    19 bool cmp1(cow a, cow b)
    20 {
    21     if(a.l!=b.l) return a.l<b.l;
    22     else return a.r<b.r;
    23 }
    24 bool cmp2(bo a, bo b)
    25 {
    26     if(a.val!=b.val) return a.val<b.val;
    27     else return a.num<b.num;
    28 }
    29 priority_queue<int ,vector<int>,greater<int> > qe;
    30 
    31 int main()
    32 {
    33     #ifdef DEBUG
    34     freopen("sample.txt","r",stdin); //freopen("data.out", "w", stdout);
    35     #endif
    36     
    37     int n,m;
    38     scanf("%d %d",&n,&m);
    39     for(int i=1;i<=n;i++)
    40         scanf("%d %d",&A[i].l,&A[i].r);
    41     for(int i=1;i<=m;i++)
    42         scanf("%d %d",&B[i].val,&B[i].num);
    43     sort(A+1,A+1+n,cmp1);
    44     sort(B+1,B+1+m,cmp2);
    45     int ans = 0;
    46     int p = 1;
    47     for(int i=1;i<=m;i++)
    48     {
    49         while(p<=n && A[p].l<=B[i].val)//将l比这种防晒液val小的牛的r入队
    50         {
    51             qe.push(A[p].r);
    52             p++;
    53         }
    54         while(!qe.empty() && B[i].num>0)//优先让r小的奶牛使用
    55         {
    56             int t = qe.top(); qe.pop();
    57             if(t >= B[i].val)//之后的防晒液val值只会更大,所以r<val的出队也不影响后面
    58             {
    59                 ans++;
    60                 B[i].num--;
    61             }
    62         }
    63     }
    64     printf("%d
    ",ans);
    65     
    66     return 0;
    67 }

     另一种贪心写法:

     1 #include <bits/stdc++.h>
     2 typedef long long LL;
     3 const int INF = 0x3f3f3f3f;
     4 const double eps = 1e-8;
     5 const int mod = 1e9+7;
     6 const int maxn = 1e5+10;
     7 using namespace std;
     8 
     9 vector<pair<int,int> > vt;
    10 map<int,int> mp;
    11 
    12 int main()
    13 {
    14     #ifdef DEBUG
    15     freopen("sample.txt","r",stdin); //freopen("data.out", "w", stdout);
    16     #endif
    17     
    18     int n,m;
    19     scanf("%d %d",&n,&m);
    20     for(int i=1;i<=n;i++)
    21     {
    22         int l,r;
    23         scanf("%d %d",&l,&r);
    24         vt.push_back({l,r});
    25     }
    26     for(int i=1;i<=m;i++)
    27     {
    28         int val,num;
    29         scanf("%d %d",&val,&num);
    30         mp[val]+=num;
    31     }
    32     sort(vt.begin(),vt.end());
    33     mp[0] += 1;
    34     int ans = 0;
    35     for(int i=n-1;~i;i--)
    36     {
    37         map<int,int>::iterator it = mp.upper_bound(vt[i].second);
    38         it--;
    39         if(it->first >= vt[i].first)
    40         {
    41             ans++;
    42             it->second--;
    43             if(it->second == 0) mp.erase(it);
    44         }
    45     }
    46     printf("%d
    ",ans);
    47     
    48     return 0;
    49 }

    -

  • 相关阅读:
    LoadRunner利用ODBC编写MySql脚本(转)
    未在本地计算机上注册 microsoft.jet.oledb.4.0 提供程序
    趣文:舌尖上的程序猿
    Hadoop之我见
    C语言的经典排序算法源码
    Oracle自学笔记(一)
    log4j.properties 详解与配置步骤总结
    修改oracle用户密码永不过期
    Android 发送短信总结
    CEF禁止右键菜单
  • 原文地址:https://www.cnblogs.com/jiamian/p/12809949.html
Copyright © 2011-2022 走看看