Luogu2869 [USACO07DEC]美食的食草动物Gourmet Grazers (贪心,二分,数据结构优化)
贪心
考场上因无优化与卡常T掉的\(n \log(n)\)
//#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long
#define ON_DEBUG
#ifdef ON_DEBUG
#define D_e_Line printf("\n\n----------\n\n")
#define D_e(x) cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin);
#else
#define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ;
#endif
struct ios{
template<typename ATP>ios& operator >> (ATP &x){
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x*= f;
return *this;
}
}io;
using namespace std;
const int N = 100007;
struct Day{
long long cost, taste;
bool operator < (const Day &com) const{
if(taste != com.taste) return taste > com.taste;
return cost > com.cost;
}
}day[N];
struct Food{
long long cost, taste;
bool operator < (const Food &com) const{
if(cost != com.cost) return cost < com.cost;
return taste < com.taste;
}
}food[N];
int n, m;
inline int FindFood(int price){
int l = 1, r = m;
while(l <= r){
int mid = (l + r) >> 1;
if(food[mid].cost >= price)
r = mid - 1;
else
l = mid + 1;
}
return l;
}
int vis[N];
int main(){
freopen("snack.in", "r", stdin);
freopen("snack.out", "w", stdout);
io >> n >> m;
R(i,1,n){
io >> day[i].cost >> day[i].taste;
}
R(i,1,m){
io >> food[i].cost >> food[i].taste;
}
if(m < n){
printf("-1");
return 0;
}
sort(day + 1, day + n + 1);
sort(food + 1, food + m + 1);
long long ans = 0;
R(i,1,n){
int pos = FindFood(day[i].cost);
while(vis[pos] == true) ++pos;
if(pos > m){
printf("-1"); return 0;
}
int flag = 0;
R(j,pos,m){
if(vis[j]) continue;
if(food[j].taste >= day[i].taste){
vis[j] = true;
ans += food[j].cost;
flag = 1;
break;
}
}
if(!flag){
printf("-1"); return 0;
}
}
printf("%lld", ans);
return 0;
}
/*
one money raise
one taste down
4 7
1 1
2 3
1 4
4 2
3 2
2 1
4 3
5 2
5 4
2 6
4 4
*/
正解用数据结构,或STL水
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long
#define ON_DEBUG
#ifdef ON_DEBUG
#define D_e_Line printf("\n\n----------\n\n")
#define D_e(x) cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin);
#else
#define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ;
#endif
struct ios{
template<typename ATP>ios& operator >> (ATP &x){
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x*= f;
return *this;
}
}io;
using namespace std;
#include <set>
const int N = 100007;
multiset<int> st;
pair<int,int> day[N], food[N];
int main(){
//FileOpen();
int n,m;
io >> n >> m;
R(i,1,n){
io >> day[i].second >> day[i].first;
}
R(i,1,m){
io >> food[i].second >> food[i].first;
}
sort(day + 1, day + n + 1);
sort(food + 1, food + m + 1);
int j = m;
long long ans = 0;
nR(i,n,1){
while(j && food[j].first >= day[i].first){
st.insert(food[j].second);
--j;
}
multiset<int>::iterator it = st.lower_bound(day[i].second);
if(it == st.end()){
printf("-1"); return 0;
}
ans += *it;
st.erase(it);
}
printf("%lld", ans);
return 0;
}

Luogu2869 [USACO07DEC]美食的食草动物Gourmet Grazers (贪心,二分,数据结构优化)的更多相关文章
- luogu2869 [USACO07DEC]美食的食草动物Gourmet Grazers
先满足挑剔的 #include <algorithm> #include <iostream> #include <cstdlib> #include <cs ...
- P2869 [USACO07DEC]美食的食草动物Gourmet Grazers
P2869 [USACO07DEC]美食的食草动物Gourmet Grazers 题目:约翰的奶牛对食物越来越挑剔了.现在,商店有M 份牧草可供出售,奶牛食量很大,每份牧草仅能供一头奶牛食用.第i 份 ...
- LG_2869_[USACO07DEC]美食的食草动物Gourmet Grazers
题目描述 Like so many others, the cows have developed very haughty tastes and will no longer graze on ju ...
- [USACO07DEC]美食的食草动物Gourmet Grazers
---题面--- 题解: 首先观察题面,直觉上对于一头奶牛,肯定要给它配pi和qi符合条件的草中两者尽量低的草,以节省下好草给高要求的牛. 实际上这是对的,但观察到两者尽量低这个条件并不明确,无法用于 ...
- poj 2782 Bin Packing (贪心+二分)
F - 贪心+ 二分 Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description ...
- Card Game Cheater(贪心+二分匹配)
Card Game Cheater Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple - F 贪心+二分
Heap Partition Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge A sequence S = { ...
- 贪心/二分查找 BestCoder Round #43 1002 pog loves szh II
题目传送门 /* 贪心/二分查找:首先对ai%=p,然后sort,这样的话就有序能使用二分查找.贪心的思想是每次找到一个aj使得和为p-1(如果有的话) 当然有可能两个数和超过p,那么an的值最优,每 ...
- Codeforces Round #768 (Div. 2) D. Range and Partition // 思维 + 贪心 + 二分查找
The link to problem:Problem - D - Codeforces D. Range and Partition time limit per test: 2 second ...
随机推荐
- 【Java面试】什么是幂等?如何解决幂等性问题?
一个在传统行业工作了7年的粉丝私信我. 他最近去很多互联网公司面试,遇到的很多技术和概念都没听过. 其中就有一道题是:"什么是幂等.如何解决幂等性问题"? 他说,这个概念听都没听过 ...
- Python <算法思想集结>之抽丝剥茧聊动态规划
1. 概述 动态规划算法应用非常之广泛. 对于算法学习者而言,不跨过动态规划这道门,不算真正了解算法. 初接触动态规划者,理解其思想精髓会存在一定的难度,本文将通过一个案例,抽丝剥茧般和大家聊聊动态规 ...
- Python 多道技术以及进程、线程和协程
多道技术 并发:看起来像同时运行 并行:真正意义上的同时运行,并行肯定是并发 空间的复用与时间复用 空间复用 多个程序用一套计算机硬件 时间复用 程序切换节省时间 ''' 切换(cup)分为两种情况 ...
- Python使用EasyOCR库对行程码图片进行OCR文字识别介绍与实践
关注「WeiyiGeek」点我,点我 设为「特别关注」,每天带你在B站玩转网络安全运维.应用开发.物联网IOT学习! 希望各位看友[关注.点赞.评论.收藏.投币],助力每一个梦想. 文章目录 0x00 ...
- [学习笔记]使用Docker+Jenkin自动化流水线发布.Net应用
使用Docker容器方案可以快速安全地将项目部署到客户的服务器上,作为公司项目,需要解决两个问题: 1. 需要搭建一个私有的Docker仓库,以便安全的存储镜像 2. 需要一套自动化发布方案,实现代 ...
- Oracle常见问题解决方法
1.设置数据库用户的密码有效期为 无限制 --查询proile文件名 SELECT username,PROFILE FROM dba_users; --查询文件 的密码保护策略 SELECT * F ...
- Spring基础只是—AOP的概念介绍
Spring容器包含两个重要的特性:面向切面编程(AOP)和控制反转(IOC).面向切面编程是面向对象(OOP)的一种补充,在面向对象编程的过程中编程针对的目标是一个个对象,而面向切面编程中编程针对的 ...
- 开发工具-PowerShell下载地址
更新日志 2022年6月10日 初始化链接. https://github.com/PowerShell/PowerShell/releases/
- dubbo容错机制
dubbo的容错机制 Failover Cluster(默认) 失败自动切换,当出现失败,重试其它服务器.通常用于读操作,但重试会带来更长延迟. Failfast Cluster 快速失败,只发起一次 ...
- MAUI与Blazor共享一套UI,媲美Flutter,实现Windows、macOS、Android、iOS、Web通用UI
1. 前言 距离上次发<MAUI初体验:爽>一文已经过去2个月了,本计划是下半年或者明年再研究MAUI的,现在计划提前啦,因为我觉得MAUI Blazor挺有意思的:在Android.iO ...