贪心

考场上因无优化与卡常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 (贪心,二分,数据结构优化)的更多相关文章

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

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

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

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

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

    题目描述 Like so many others, the cows have developed very haughty tastes and will no longer graze on ju ...

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

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

  5. poj 2782 Bin Packing (贪心+二分)

    F - 贪心+ 二分 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   Description ...

  6. Card Game Cheater(贪心+二分匹配)

    Card Game Cheater Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  7. 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 = { ...

  8. 贪心/二分查找 BestCoder Round #43 1002 pog loves szh II

    题目传送门 /* 贪心/二分查找:首先对ai%=p,然后sort,这样的话就有序能使用二分查找.贪心的思想是每次找到一个aj使得和为p-1(如果有的话) 当然有可能两个数和超过p,那么an的值最优,每 ...

  9. 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 ...

随机推荐

  1. 数位 dp 总结

    数位 dp 总结 特征 问你一个区间 \([L,R]\) 中符合要求的数的个数 一个简单的 trick :把答案拆成前缀和 \(Ans(R)-Ans(L-1)\) 如何求 \(Ans()\) ,就要用 ...

  2. 架构师必备:系统容量现状checklist

    正如飞机在起飞前,机长.副机长要过一遍checklist检查,确认没问题了才能起飞.楼主也整理了一个系统容量现状checklist,方便对照检查.本文搭配架构师必备:如何做容量预估和调优,食用更佳. ...

  3. html实现3d视觉特效

    <html> <head> <title>HTML5实现3D球效果</title> <style type="text/css" ...

  4. Elasticsearch学习系列一(部署和配置IK分词器)

    Elasticsearch简介 Elasticsearch是什么? Elaticsearch简称为ES,是一个开源的可扩展的分布式的全文检索引擎,它可以近乎实时的存储.检索数据.本身扩展性很好,可扩展 ...

  5. 使用nodejs的wxmnode模块,开发一个微信自动监控提醒功能,做个天气预报。

    这个模块是一个公众号的模块,名字叫"帮你看着". 原本这个公众号是做股票监控提醒的,我也没炒股.因为接口支持写入任何内容,所以可以有其他的用处.比如做成天气预报定时提醒. 我们去n ...

  6. SpringMVC 概述

    1. SpringMVC 概述 1) Spring 为展现层提供的基于 MVC 设计理念的优秀的 Web 框架,是目前最主流的MVC 框架之一 .MVC,M:model,模型层,指的是项目中的实体Ja ...

  7. labview从入门到出家4--用事件结构实现运算功能

    使用事件结构可以快速定位响应界面的操作事件,如按下,拖动,双击的事件.基本上我们所要实现的所有功能,都可以通过条件结构+事件结构去实现,比如后面进阶篇将会讲到的状态机就是通过条件结构和事件结构组成的. ...

  8. Java学习dayo4

    分支结构和循环语句 1.包的概念 包就是文件夹 包的命名规范:全小写,域名倒置,不能以点开头或结尾,可以包含点,每存在一个点表示一个子目录 举例:com.baidu.demo 定义包后,包中的java ...

  9. 比起网易有数BI,也许这款数据可视化软件更适合你!

    有数BI是网易推出的面向企业客户的可视化敏捷BI产品.拥有数据填报和自助式商业智能分析产品,提供网页端和手机端应用,帮助客户快速实现数据填报.多维分析.大数据探索.实时大数据展示和成员分享. 山海鲸可 ...

  10. Centos7借助docker部署mysql,提供远程链接服务

    Centos7 借助docker部署mysql,并提供远程连接服务 安装docker 运行docker 注意安装docker和运行docker的步骤很简单,可以参考我学习docker的笔记 docke ...