贪心

考场上因无优化与卡常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. datax在解析完配置后,会将core.json,job.json,plugin.json合并内容

    { "common": { "column": { "dateFormat": "yyyy-MM-dd", " ...

  2. C语言- 基础数据结构和算法 - 09 栈的应用_中缀表达式转后缀表达式20220611

    09 栈的应用_中缀表达式转后缀表达式20220611 听黑马程序员教程<基础数据结构和算法 (C版本)>, 照着老师所讲抄的, 视频地址https://www.bilibili.com/ ...

  3. C# List转String的办法

    2022年5月28日 初始记录 代码: String.Join(",", List.ToArray());

  4. 在生产中部署ML前需要了解的事

    在生产中部署ML前需要了解的事 译自:What You Should Know before Deploying ML in Production MLOps的必要性 MLOps之所以重要,有几个原因 ...

  5. python中 OS模块中 os.path.join() 函数用法简介

    基础用法 os.path.join() 用于拼接文件的路径,可以传入多个待拼接的路径 若各个路径之间不存在 " / ", 则其会自动为各个路径之间增加连接符 " / &q ...

  6. VSCode配置远程免密登陆

    生成秘钥 在本地pc的cmd窗口输入:ssh-keygen -t rsa 生成秘钥 C:\Users\NZY/.ssh/id_rsa.pub 该目录就是生成的秘钥要保存的地方(以我自己的电脑为例) 将 ...

  7. SAP 实例 5 CFW Events

    REPORT demo_custom_control . * Declarations ***************************************************** CL ...

  8. 分享|智慧环保-生态文明信息化解决方案(附PDF)

    内容摘要: 生态文明建设被提到前所未有的战略高度,我们既要绿水青山,也要金山银山.宁要绿水青山,不要金山银山,而且绿水青山就是金山银山.要正确处理好经济发展同生态环境保护的关系,牢固树立保护生态环境就 ...

  9. 手写网站服务器~用Python手动实现一个简单的服务器,不借助任何框架在浏览器中输出任意内容

    写在前面的一些P话: 在公司网站开发中,我们往往借助于Flask.Django等网站开发框架去提高网站开发效率.那么在面试后端开发工程师的时候,面试官可能就会问到网站开发的底层原理是什么? 我们不止仅 ...

  10. typescript+webpack构建一个js库

    依赖说明 入口文件 tsconfig配置 webpack配置文件 webpack入口文件配置 webpack为typescript和less文件配置各自的loader webpack的output配置 ...