题目描述

Like so many others, the cows have developed very haughty tastes and will no longer graze on just any grass. Instead, Farmer John must purchase gourmet organic grass at the Green Grass Grocers store for each of his N (1 ≤ N ≤ 100,000) cows.

Each cow i demands grass of price at least Ai (1 ≤ Ai ≤ 1,000,000,000) and with a greenness score at least Bi (1 ≤ Bi ≤ 1,000,000,000). The GGG store has M (1 ≤ M ≤ 100,000) different types of grass available, each with a price Ci (1 ≤ Ci ≤ 1,000,000,000) and a greenness score of Di (1 ≤ Di ≤ 1,000,000,000). Of course, no cow would sacrifice her individuality, so no two cows can have the same kind of grass.

Help Farmer John satisfy the cows' expensive gourmet tastes while spending as little money as is necessary.

约翰的奶牛对食物越来越挑剔了。现在,商店有M 份牧草可供出售,奶

牛食量很大,每份牧草仅能供一头奶牛食用。第i 份牧草的价格为Pi,口感为

Qi。约翰一共有N 头奶牛,他要为每头奶牛订购一份牧草,第i 头奶牛要求

它的牧草价格不低于Ai,口感不低于Bi。请问,约翰应该如何为每头奶牛选

择牧草,才能让他花的钱最少?

输入输出格式

输入格式

  • Line 1: Two space-separated integers: N and M.

  • Lines 2..N+1: Line i+1 contains two space-separated integers: Ai and Bi

  • Lines N+2..N+M+1: Line i+N+1 contains two space-separated integers: Ci and Di

输出格式

  • Line 1: A single integer which is the minimum cost to satisfy all the cows. If that is not possible, output -1.

样例

INPUT

4 7

1 1

2 3

1 4

4 2

3 2

2 1

4 3

5 2

5 4

2 6

4 4

OUTPUT

12

SOLUTION

贪心+multiset

本题每一头奶牛有两个标准来找到它合适的牧草。为了使花费最小,当然是选择的两个参数值越小越好。

而且这两个参数是绑定在一起的,所以考虑用pair存数据(pair在库里别忘了qwq)。

首先找到最小符合的牧草,再把这个牧草相关数据从set里删除。

我们可以以其中一个参数为主关键字,按要求从高到低来处理奶牛的要求,每当现在指针所指的牧草能够满足要求,我们就把它的另一参数加入我们的multiset中,加入完成之后multiset会自动维护好当前的最优值。倒着处理可以避免预先加入了不合当前奶牛要求的牧草,保证了正确性,从而降低了处理的代码复杂度。

这里维护有一个也不能说上是技巧的小技巧:

因为我们的pair是默认以first为主关键字升序排序的,而我们要求输出的是其中一个参数:价格的和。因为我们在操作中是把两个参数的一个参数加入multiset维护,所以显而易见的,要得到所有选取的牧草的价格,我们只要把牧草变为pair中的second就可以了。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <set>
#include <algorithm>
#include <utility>
typedef long long LL;
inline int read(){
int x=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9') {x=(x<<3)+(x<<1)+ch-48;ch=getchar();}
return x*f;}
const int N=101000;
int n,m;
std::pair<int,int> cow[N],grass[N];
using namespace std;
int main(){
int i,j;
n=read();m=read();
for (i=1;i<=n;++i){cow[i].second=read();cow[i].first=read();}
sort(cow+1,cow+n+1);
for (i=1;i<=m;++i){grass[i].second=read();grass[i].first=read();}
sort(grass+1,grass+m+1);
multiset<int> G;j=m;LL ans=0;
for (i=n;i>=1;--i){
for (;j&&(grass[j].first>=cow[i].first);--j) G.insert(grass[j].second);
multiset<int>::iterator iter;
iter=G.lower_bound(cow[i].second);//因为默认以first为主关键字
if (iter==G.end()) {puts("-1");return 0;}
ans+=(*iter);G.erase(iter);
}
printf("%lld\n",ans);
return 0;
}

LG_2869_[USACO07DEC]美食的食草动物Gourmet Grazers的更多相关文章

  1. P2869 [USACO07DEC]美食的食草动物Gourmet Grazers

    P2869 [USACO07DEC]美食的食草动物Gourmet Grazers 题目:约翰的奶牛对食物越来越挑剔了.现在,商店有M 份牧草可供出售,奶牛食量很大,每份牧草仅能供一头奶牛食用.第i 份 ...

  2. [USACO07DEC]美食的食草动物Gourmet Grazers

    ---题面--- 题解: 首先观察题面,直觉上对于一头奶牛,肯定要给它配pi和qi符合条件的草中两者尽量低的草,以节省下好草给高要求的牛. 实际上这是对的,但观察到两者尽量低这个条件并不明确,无法用于 ...

  3. luogu2869 [USACO07DEC]美食的食草动物Gourmet Grazers

    先满足挑剔的 #include <algorithm> #include <iostream> #include <cstdlib> #include <cs ...

  4. Luogu2869 [USACO07DEC]美食的食草动物Gourmet Grazers (贪心,二分,数据结构优化)

    贪心 考场上因无优化与卡常T掉的\(n \log(n)\) //#include <iostream> #include <cstdio> #include <cstri ...

  5. POJ3622 Gourmet Grazers(FHQ Treap)

    Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 2363   Accepted: 881 Description Like s ...

  6. POJ 3622 Gourmet Grazers(贪心)

    [题目链接] http://poj.org/problem?id=3622 [题目大意] 给出一些物品拥有两个属性值,价格和精美程度 给出一些需求表示要求获得的物品两个属性值的两种属性下界, 一个物品 ...

  7. luogu 2869 挑剔的美食家

    Gourmet Grazers 传送门 题目大意 约翰的奶牛对食物越来越挑剔了.现在,商店有\(M\) 份牧草可供出售,奶牛食量很大,每份牧草仅能供一头奶牛食用.第\(i\) 份牧草的价格为\(P_i ...

  8. POJ1058 The Gourmet Club

    题目来源:http://poj.org/problem?id=1058 题目大意:ACM城的美食俱乐部有16位成员.他们连续了当地的法国餐厅Chatrau Java来安排连续5天的晚餐.晚餐时他们每4 ...

  9. Hawk 1.2 快速入门2 (大众点评18万美食数据)

    本文将讲解通过本软件,获取大众点评的所有美食数据,可选择任一城市,也可以很方便地修改成获取其他生活门类信息的爬虫. 本文将省略原理,一步步地介绍如何在20分钟内完成爬虫的设计,基本不需要编程,还能自动 ...

随机推荐

  1. Spring使用Rabbitmq (简单使用)

    1.pom.xml jar包引用 <dependencies> <dependency> <groupId>org.springframework</grou ...

  2. 吴裕雄--天生自然 JAVA开发学习:条件语句

    public class Test { public static void main(String args[]){ int x = 10; if( x < 20 ){ System.out. ...

  3. springboot学习笔记:8. springboot+druid+mysql+mybatis+通用mapper+pagehelper+mybatis-generator+freemarker+layui

    前言: 开发环境:IDEA+jdk1.8+windows10 目标:使用springboot整合druid数据源+mysql+mybatis+通用mapper插件+pagehelper插件+mybat ...

  4. \_\_init\_\_和\_\_new\_\_

    __init__和__new__ 一.__new__和__init__ 曾经我幼稚的以为认识了python的__init__()方法就相当于认识了类构造器,结果,__new__()方法突然出现在我眼前 ...

  5. 深度学习数据集MNIST ImageNet COCO PASCAL VOC介绍

    参考文档 深度学习数据集汇总介绍 1.  MNIST 深度学习领域的“Hello World!”,入门必备!MNIST是一个手写数字数据库,它有60000个训练样本集和10000个测试样本集,每个样本 ...

  6. 爬虫加入数据post请求

    formdata = {'...': '...', '......': '......', '......': '......'}HEADERS = { 'User-Agent': 'Mozilla/ ...

  7. LeetCode——919.完全二叉树插入器

    完全二叉树是每一层(除最后一层外)都是完全填充(即,结点数达到最大)的,并且所有的结点都尽可能地集中在左侧. 设计一个用完全二叉树初始化的数据结构 CBTInserter,它支持以下几种操作: CBT ...

  8. 联想的amd电脑,Debian8.8开机后亮度值始终最大,尝试过各种方法,始终无法解决,最后debian8.8在安装开源驱动后,成功调节

    安装ATI显卡驱动(开源)(方法步骤来自Debian WiKi) A.先升级可用的包 # aptitude upgrade B.安装下面3个包 # apt-get install firmware-l ...

  9. 【Java杂货铺】用Security做权限极简入门

    原来大多数单体项目都是用的shiro,随着分布式的逐渐普及以及与Spring的天生自然的结合.Spring Security安全框架越受大家的青睐.本文会教你用SpringSecurity设计单项目的 ...

  10. UIView 的Transform属性以及 CGAffineTransform的使用

    什么是Transform? Transform是一个3×3的矩阵,如下图所示: 通过这个矩阵我们可以对一个坐标系统进行缩放,平移,旋转以及这两者的任意组着操作.而且矩阵的操作不具备交换律,即矩阵的操作 ...