LG_2869_[USACO07DEC]美食的食草动物Gourmet Grazers
题目描述
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的更多相关文章
- P2869 [USACO07DEC]美食的食草动物Gourmet Grazers
P2869 [USACO07DEC]美食的食草动物Gourmet Grazers 题目:约翰的奶牛对食物越来越挑剔了.现在,商店有M 份牧草可供出售,奶牛食量很大,每份牧草仅能供一头奶牛食用.第i 份 ...
- [USACO07DEC]美食的食草动物Gourmet Grazers
---题面--- 题解: 首先观察题面,直觉上对于一头奶牛,肯定要给它配pi和qi符合条件的草中两者尽量低的草,以节省下好草给高要求的牛. 实际上这是对的,但观察到两者尽量低这个条件并不明确,无法用于 ...
- luogu2869 [USACO07DEC]美食的食草动物Gourmet Grazers
先满足挑剔的 #include <algorithm> #include <iostream> #include <cstdlib> #include <cs ...
- Luogu2869 [USACO07DEC]美食的食草动物Gourmet Grazers (贪心,二分,数据结构优化)
贪心 考场上因无优化与卡常T掉的\(n \log(n)\) //#include <iostream> #include <cstdio> #include <cstri ...
- POJ3622 Gourmet Grazers(FHQ Treap)
Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 2363 Accepted: 881 Description Like s ...
- POJ 3622 Gourmet Grazers(贪心)
[题目链接] http://poj.org/problem?id=3622 [题目大意] 给出一些物品拥有两个属性值,价格和精美程度 给出一些需求表示要求获得的物品两个属性值的两种属性下界, 一个物品 ...
- luogu 2869 挑剔的美食家
Gourmet Grazers 传送门 题目大意 约翰的奶牛对食物越来越挑剔了.现在,商店有\(M\) 份牧草可供出售,奶牛食量很大,每份牧草仅能供一头奶牛食用.第\(i\) 份牧草的价格为\(P_i ...
- POJ1058 The Gourmet Club
题目来源:http://poj.org/problem?id=1058 题目大意:ACM城的美食俱乐部有16位成员.他们连续了当地的法国餐厅Chatrau Java来安排连续5天的晚餐.晚餐时他们每4 ...
- Hawk 1.2 快速入门2 (大众点评18万美食数据)
本文将讲解通过本软件,获取大众点评的所有美食数据,可选择任一城市,也可以很方便地修改成获取其他生活门类信息的爬虫. 本文将省略原理,一步步地介绍如何在20分钟内完成爬虫的设计,基本不需要编程,还能自动 ...
随机推荐
- 瑞芯微RK3399六核-迅为3399开发板介绍
迅为3399开发板基于瑞芯微的RK3399处理器设计,Rockchip RK3399是瑞芯微推出的一款低功耗.高性能的应用处理器芯片,该芯片基于Big.Little架构,即具有独立的NEON协同处理器 ...
- Jupyer Notebook, Jupyter Lab 虚拟环境配置
虚拟环境 conda create -n python36 python=3.6 使用以下命令激活: activate python36 Notebook 安装插件 conda install nb_ ...
- Linux笔记(三)——Shell编程
预备知识 1.Shell是解释执行的脚本语言,可以直接调用Linux系统命令 2.文件以.sh结尾, #!bin/bash 标识, 说明这是一个shell脚本, 不能省略 3.执行 赋予权限,直接运行 ...
- Python实现自动处理表格,让你拥有更多的自由时间!
相信有不少朋友日常工作会用到 Excel 处理各式表格文件,更有甚者可能要花大把时间来做繁琐耗时的表格整理工作.最近有朋友问可否编程来减轻表格整理工作量,今儿我们就通过实例来实现 Python 对表格 ...
- python 3.6
安装了最新版anaconda3-4.3 发现jupyter-notebook 少了一些东西.需要手工安装 https://github.com/Anaconda-Platform/nbpresent
- 设置R更新源
命令行设置R更新源 创建文件 R.home()/etc/Rprofile.site 设置更新源 local({r <- getOption("repos") r[" ...
- postgreSQL命令大全(更新中)
1.PostgreSQL索引的建立https://blog.csdn.net/jubaoquan/article/details/78850899: 2.PostgreSQL9中索引的原理和效率查询h ...
- 对象数组和for循环遍历输出学生的信息
public class Student { private int no; private String name; private int age; public Student(int no, ...
- TOJ-2811 Bessie's Weight Problem(DP、背包问题)
链接:https://ac.nowcoder.com/acm/contest/1082/K 题目描述 Bessie, like so many of her sisters, has put on a ...
- NOIP2002 过河卒(DFS,DP)
https://www.luogu.org/problem/P1002 题目描述 如图,A 点有一个过河卒,需要走到目标 B 点.卒行走规则:可以向下.或者向右.同时在棋盘上的任一点有一个对方的马(如 ...