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;
    }
    

      

  • 相关阅读:
    2、容器初探
    3、二叉树:先序,中序,后序循环遍历详解
    Hebbian Learning Rule
    论文笔记 Weakly-Supervised Spatial Context Networks
    在Caffe添加Python layer详细步骤
    论文笔记 Learning to Compare Image Patches via Convolutional Neural Networks
    Deconvolution 反卷积理解
    论文笔记 Feature Pyramid Networks for Object Detection
    Caffe2 初识
    论文笔记 Densely Connected Convolutional Networks
  • 原文地址:https://www.cnblogs.com/weiyukang/p/9350672.html
Copyright © 2011-2022 走看看