zoukankan      html  css  js  c++  java
  • 51nod 挑剔的美食家

      因为要找最小的价格呀

      所以对于每个牛 我们都去找最小的满足条件的草

      将牛和草 的鲜嫩程度从大到小排序

      然后 依次枚举牛

      用 multiset来保存所有满足条件的草的价格

      然后去二分找 第一个大于等于 第i个牛的要求的价格的值  找到之后就erase一下 否则就输出-1

      因为草的鲜嫩程度是从大到小的

      所以满足第一个牛的所有草 它的鲜嫩程度一定满足第二个牛

     

    #include <cstdio>
    #include <utility>
    #include <iostream>
    #include <set>
    #include <algorithm>
    using namespace std;
    const int maxn = 100100;
    struct node{
        int value;  // 价格
        int neng; // 鲜嫩程度
    };
    bool cmp(node left, node right){
        return left.neng > right.neng;
    }
    multiset<int > s; // 因为不同草的定价相同
    multiset<int > :: iterator it;
    node cow[maxn];  // 牛
    node grass[maxn]; // 草
    int main(){
        int n, m;
        long long int ans = 0;
        scanf("%d%d", &n, &m);
        for(int i = 1; i <= n; i++){
            scanf("%d%d", &cow[i].value, &cow[i].neng);
        }
        for(int i = 1; i <= m; i++){
            scanf("%d%d", &grass[i].value, &grass[i].neng);
        }
        // 按照草的新鲜程度从大到小排序
        sort(cow + 1, cow + 1 + n, cmp);
        sort(grass + 1, grass + 1 + m, cmp);
        int num = 1;
        for(int i = 1; i <= n; i++){
            while(num <= m && grass[num].neng >= cow[i].neng){
                s.insert(grass[num].value);
                num++;
            }
            //for(it = s.begin(); it != s.end(); ++it){
            //    cout << *it << " ";
            //}
            cout << endl;
            it = s.lower_bound(cow[i].value);  // 找到第一个大于等于第i个牛的要求的最低价
            //cout << *it << endl;
            if(it == s.end()){
                puts("-1");
                return 0;
            }
            ans += *it;
            s.erase(it);
        }
        printf("%lld
    ", ans);
        return 0;
    }
    

      

  • 相关阅读:
    【CF-148E】Porcelain DP
    【CF-687C】The Values You Can Make 01背包变形
    【CF-269】B. Greenhouse Effect 最公共子序列
    【CF-1345】C.Hilbert's Hotel 数论
    【CF-607B】Zuma 经典区间DP
    【【模板】三分法】
    【CF-1355 E.Restorer Distance】三分,贪心
    【CF-1355 BCount Triangles】枚举
    【CF-1355 Sequence with Digits】
    触发器的类型有哪三种?
  • 原文地址:https://www.cnblogs.com/weiyukang/p/9350672.html
Copyright © 2011-2022 走看看