【题目大意】

有n头奶牛m种牧草,每种牧草有它的价格和鲜嫩度。每头奶牛要求它的牧草的鲜嫩度要不低于一个值,价格也不低于一个值。每种牧草只会被一头牛选择。问最少要多少钱?

【思路】

显然的贪心,把奶牛和牧草都按照鲜嫩度由大到小排序,对于每奶牛把鲜嫩度大于它的都扔进treap,然后找出后继。

不过注意后继的概念是大于它且最小的,然而我们这里是可以等于的,所以应该是找cow[i].fresh-1的后继,注意一下……

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
const int MAXN=+;
const int INF=0x7fffffff;
struct node
{
int fresh,price;
bool operator < (const node &x) const
{
return (fresh>x.fresh);//注意是由大到小排序
}
}cow[MAXN],grass[MAXN];
struct treap
{
int key,cnt,priority;
treap* lson;
treap* rson;
treap()
{
cnt=;
priority=rand();
lson=rson=NULL;
}
};
treap* root=NULL;
int n,m; void RightRotate(treap* &rt)
{
treap* tmp=rt->lson;
rt->lson=tmp->rson;
tmp->rson=rt;
rt=tmp;
} void LeftRotate(treap* &rt)
{
treap* tmp=rt->rson;
rt->rson=tmp->lson;
tmp->lson=rt;
rt=tmp;
} void insert(treap* &rt,int x)
{
if (rt==NULL)
{
rt=new treap;//不要忘记了新建
rt->key=x;
}
else if (rt->key==x) rt->cnt++;
else
{
if (rt->key>x)
{
insert(rt->lson,x);
if (rt->lson->priority>rt->priority) RightRotate(rt);
}
if (rt->key<x)
{
insert(rt->rson,x);
if (rt->rson->priority>rt->priority) LeftRotate(rt);
}
}
} void del(treap* &rt,int x)
{
if (rt->key==x)
{
if (rt->cnt>) rt->cnt--;
else
{
treap* tmp=rt;
if (rt->lson==NULL)
{
rt=rt->rson;
delete tmp;
}
else if (rt->rson==NULL)
{
rt=rt->lson;
delete tmp;
}
else
{
if (rt->lson->priority>rt->rson->priority) RightRotate(rt),del(rt->rson,x);
else LeftRotate(rt),del(rt->lson,x);
}
}
}
else
{
if (x<rt->key) del(rt->lson,x);
else del(rt->rson,x);
}
} int suc(treap* &rt,int x)
{
int ans=INF;
treap* tmp=rt;
while (tmp)
{
if (tmp->key>x)
{
ans=min(ans,tmp->key);
tmp=tmp->lson;
}
else tmp=tmp->rson;
}
return ans;
} void init()
{
scanf("%d%d",&n,&m);
for (int i=;i<=n;i++) scanf("%d%d",&cow[i].price,&cow[i].fresh);
for (int i=;i<=m;i++) scanf("%d%d",&grass[i].price,&grass[i].fresh);
sort(cow+,cow+n+);
sort(grass+,grass+m+);
} ll solve()
{
int j=;
ll ans=;
for (int i=;i<=n;i++)
{
while (grass[j].fresh>=cow[i].fresh && j<=m)
{
insert(root,grass[j].price);//这里一开始把j打成i了
j++;
}
int find=suc(root,cow[i].price-);//注意由于这里是不低于,也就是≥,并不是取cow[i].price的后继,而是cow[i].price-1的
if (find==INF) return(-);
else
{
ans+=(ll)find;
del(root,find);
}
}
return ans;
} int main()
{
init();
printf("%lld",solve());
return ;
}

【贪心+Treap】BZOJ1691-[Usaco2007 Dec]挑剔的美食家的更多相关文章

  1. [BZOJ1691][Usaco2007 Dec]挑剔的美食家

    [BZOJ1691][Usaco2007 Dec]挑剔的美食家 试题描述 与很多奶牛一样,Farmer John那群养尊处优的奶牛们对食物越来越挑剔,随便拿堆草就能打发她们午饭的日子自然是一去不返了. ...

  2. bzoj1691[Usaco2007 Dec]挑剔的美食家 平衡树treap

    Description 与很多奶牛一样,Farmer John那群养尊处优的奶牛们对食物越来越挑剔,随便拿堆草就能打发她们午饭的日子自然是一去不返了.现在,Farmer John不得不去牧草专供商那里 ...

  3. 【贪心】【二维偏序】【权值分块】bzoj1691 [Usaco2007 Dec]挑剔的美食家

    既然题目中的要求满足二维偏序,那么我们很自然地想到将所有东西(草和牛)都读进来之后,对一维(美味度)排序,然后在另一维(价值)中取当前最小的. 于是,Splay.mutiset.权值分块什么的都支持查 ...

  4. BZOJ 1691: [Usaco2007 Dec]挑剔的美食家 [treap 贪心]

    1691: [Usaco2007 Dec]挑剔的美食家 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 786  Solved: 391[Submit][S ...

  5. BZOJ_1691_[Usaco2007 Dec]挑剔的美食家_贪心

    BZOJ_1691_[Usaco2007 Dec]挑剔的美食家_贪心 题意: 与很多奶牛一样,Farmer John那群养尊处优的奶牛们对食物越来越挑剔,随便拿堆草就能打发她们午饭的日子自然是一去不返 ...

  6. BZOJ 1691: [Usaco2007 Dec]挑剔的美食家( 平衡树 )

    按鲜嫩程度排个序, 从大到小处理, 用平衡树维护价值 ---------------------------------------------------------------------- #i ...

  7. BZOJ1691: [Usaco2007 Dec]挑剔的美食家

    传送门: 一句话题解:贪心+treap 好几天前刚学的treap,然后真到了考treap又写不出来,这么辣鸡还搞什么OI 先按$A_i$递减排序,然后把$C_i$也递减排序,然后用一个指针指向$M$序 ...

  8. 「BZOJ1691」[Usaco2007 Dec] 挑剔的美食家 (Treap)

    Description 与很多奶牛一样,Farmer John那群养尊处优的奶牛们对食物越来越挑剔,随便拿堆草就能打发她们午饭的日子自然是一去不返了.现在,Farmer John不得不去牧草专供商那里 ...

  9. 【BZOJ】1691: [Usaco2007 Dec]挑剔的美食家(set+贪心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1691 懒得打平衡树了.... 而且multiset是很快的... 排到了rank1 T_T 贪心就是 ...

随机推荐

  1. Yii2 的 redis 应用

    在应用的时候需要先对yii2进行扩展安装 如果装有composer直接运行 php composer.phar require --prefer-dist yiisoft/yii2-redis 当然也 ...

  2. Python3 动态导入模块的两种方式

    动态导入模块就是只知道str类型的模块名字符串,通过这个字符串导入模块 需要导入的模块: #!/usr/bin/env python # _*_ coding:utf-8 _*_ # Author:C ...

  3. Mysql存储之原生语句操作(pymysql)

    Mysql存储之原生语句操作(pymysql) 关系型数据库是基于关系模型的数据库,而关系模型是通过二维表时实现的,于是构成了行列的表结构. 表可以看作是某个实体的集合,而实体之间存在联系,这个就需要 ...

  4. c#操作pdf文件系列之创建文件

    1.我使用的工具是vs2013,引用的第三方程序集itextpdf 具体安装方法,可以通过nuget搜索iTextSharp然后进行安装. 2具体代码如下 创建两个不同pdf文件,每个地方什么意思代码 ...

  5. c++各种排序的简单实现

    /* 直插排序 */ void InsertSort(vector<int> &arr){ for(int i = 1;i < arr.size();++i){ for(in ...

  6. 匿名函数、lambda表达式

    匿名函数 func = lambda x: y #x是形参,y是返回值 键字lambda表示匿名函数,冒号前面的x表示函数参数,冒号后面的y表示匿名函数的返回值. 例1:返回列表中长度大于等于3的元素 ...

  7. [转载]Windows服务编写原理及探讨(2)

    (二)对服务的深入讨论之上 上一章其实只是概括性的介绍,下面开始才是真正的细节所在.在进入点函数里面要完成ServiceMain的初始化,准确点说是初始化一个 SERVICE_TABLE_ENTRY结 ...

  8. Retrofit:类型安全的REST客户端for 安卓&Java

    Retrofit:类型安全的REST客户端for 安卓&Java 2014年5月5日 星期一 21:11 官网:  http://square.github.io/retrofit/ GitH ...

  9. Delphi根据字符串实例化对象

    我们可以通过ClassRegistry单元的TClassRegistry类很轻松的根据字符串创建出对象. 下面是该类几个主要函数的说明: // 获取TClassRegistry自身的单例引用class ...

  10. s3cmd : Add a config parameter to enable path-style bucket access 当ceph rgw使用域名时,需要支持 path-style bucket特性

    s3cmd 要是1.6.1 之后的版本 增加配置项:  vi .s3cfg use_path_mode = True 源码参考: cat  /usr/local/lib/python2.7/dist- ...