[题目链接]

https://www.lydsy.com/JudgeOnline/problem.php?id=1691

[算法]

不难想到如下算法 :

将所有牛和牧草按鲜嫩程度降序排序,按顺序扫描,如果扫描到的是牧草,则将牧草的美味程度加入一个集合,否则,将答案加上比这头牛的期望价格大的牧草中价格最小的

这个贪心策略的正确性是显然的,具体实现时,我们可以维护一棵平衡树,这棵平衡树支持 : 插入/删除一个数,查询一个数的后继,我们可以方便地使用STL-set完成这个任务,为了练习平衡树,笔者使用的是伸展树

时间复杂度 : O(Nlog(N))

[代码]

#include<bits/stdc++.h>
using namespace std;
#define MAXN 100010 struct info
{
long long x,y;
int opt;
} a[MAXN << ]; int i,n,m;
long long ans,tmp; template <typename T> inline void read(T &x)
{
long long f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar())
{
if (c == '-') f = -f;
}
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
struct Splay
{
int root,total;
struct Node
{
int fa;
long long val;
int cnt;
int son[];
} Tree[MAXN << ];
inline bool get(int x)
{
return Tree[Tree[x].fa].son[] == x;
}
inline void rotate(int x)
{
int f = Tree[x].fa,g = Tree[f].fa;
int tmpx = get(x),tmpf = get(f);
if (!f) return;
Tree[f].son[tmpx] = Tree[x].son[tmpx ^ ];
if (Tree[x].son[tmpx ^ ]) Tree[Tree[x].son[tmpx ^ ]].fa = f;
Tree[x].son[tmpx ^ ] = f;
Tree[f].fa = x;
Tree[x].fa = g;
if (g) Tree[g].son[tmpf] = x;
}
inline void splay(int x)
{
int fa;
for (fa = Tree[x].fa; (fa = Tree[x].fa); rotate(x))
rotate((get(fa) == get(x)) ? fa : x);
root = x;
}
inline int insert(long long x)
{
bool tmp;
if (!root)
{
root = ++total;
Tree[root].fa = ;
Tree[root].son[] = Tree[root].son[] = ;
Tree[root].cnt = ;
Tree[root].val = x;
return total;
}
int now = root;
while (now > )
{
if (Tree[now].val == x)
{
Tree[now].cnt++;
splay(now);
return now;
}
tmp = x > Tree[now].val;
if (!Tree[now].son[tmp])
{
Tree[now].son[tmp] = ++total;
Tree[total].fa = now;
Tree[total].val = x;
Tree[total].cnt = ;
Tree[total].son[] = Tree[total].son[] = ;
splay(total);
return total;
} else now = Tree[now].son[tmp];
}
}
inline int get_pos(int x)
{
int now = x;
while (Tree[now].son[]) now = Tree[now].son[];
return now;
}
inline void join(int x,int y)
{
int p = get_pos(x);
splay(p);
Tree[p].son[] = y;
Tree[y].fa = p;
}
inline void erase(int x)
{
Tree[x].cnt--;
if (Tree[x].cnt > ) return;
if (!Tree[x].son[] && !Tree[x].son[])
{
root = ;
return;
}
if (!Tree[x].son[])
{
root = Tree[x].son[];
Tree[root].fa = ;
return;
}
if (!Tree[x].son[])
{
root = Tree[x].son[];
Tree[root].fa = ;
return;
}
join(Tree[x].son[],Tree[x].son[]);
}
inline long long query(long long x)
{
int p = insert(x);
int now = p;
if (Tree[p].cnt > )
{
erase(p);
erase(p);
return x;
}
now = Tree[p].son[];
while (Tree[now].son[]) now = Tree[now].son[];
erase(p);
long long ret = Tree[now].val;
splay(now);
erase(now);
return ret;
}
} T;
inline bool cmp(info a,info b)
{
if (a.y != b.y) return a.y > b.y;
else return a.opt > b.opt;
} int main()
{ read(n); read(m);
for (i = ; i <= n; i++)
{
read(a[i].x);
read(a[i].y);
a[i].opt = ;
}
for (i = ; i <= m; i++)
{
read(a[n + i].x);
read(a[n + i].y);
a[n + i].opt = ;
}
sort(a + ,a + (n + m) + ,cmp);
for (i = ; i <= n + m; i++)
{
if (a[i].opt == )
{
tmp = T.query(a[i].x);
if (tmp != -)
{
ans += tmp;
continue;
}
printf("-1\n");
return ;
} else T.insert(a[i].x);
}
printf("%lld\n",ans); return ; }

[BZOJ 1691] 挑剔的美食家的更多相关文章

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

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

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

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

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

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

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

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

  5. 51nod 挑剔的美食家

    挑剔的美食家    基准时间限制:1 秒 空间限制:131072 KB 分值: 5 与很多奶牛一样,Farmer John那群养尊处优的奶牛们对食物越来越挑剔,随便拿堆草就能打发她们午饭的日子自然是一 ...

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

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

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

    Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 621  Solved: 280[Submit][Status][Discuss] Description ...

  8. BZOJ 1691 [Usaco2007 Dec]挑剔的美食家 multiset_排序_贪心

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

  9. 【BZOJ】1691: [Usaco2007 Dec]挑剔的美食家

    [算法]扫描线+平衡树(set) [题解]很明显的二维偏序数点,排序后扫描线,现加点后查询答案. 则问题转化为一维偏序,显然贪心找第一个比当前大的最优,所以用平衡树维护. 记得开multiset!!! ...

随机推荐

  1. Apache添加到windows服务和移除Apache的windows服务

    Apache添加到windows服务和移除Apache的windows服务 Apache免安装版将其添加到Windows服务中: 打开cmd控制台,在上面输入"你的Apache安装目录\bi ...

  2. 【原】thinkphp修改Redis操作类,支持选择数据库功能及添加其他方法

    版本3.2.2(ThinkPHP\Library\Think\Cache\Driver\Redis.class.php), 一:官方默认不支持选择数据库功能及,现就可选择数据库功能进行说明. 1 co ...

  3. MyBatis 的基本介绍及使用

    一.简介 ​ MyBatis 是支持定制化 SQL.存储过程以及高级映射的持久层框架(ORM).MyBatis 可以使用简单的 XML 或 注解用于配置和映射数据表,是将 POJO(Plain Old ...

  4. canvas练手项目(三)——Canvas中的Text文本

    Canvas中的Text文本也是一个知识点~,我们需要掌握一下几个基本的Text操作方法 首先是重要参数textAlign和textBaseline: textAlign left center ri ...

  5. FTP配置

    常用的匿名FTP配置项   anonymous_enable=YES           是否允许匿名用户访问   anon_umask=022                匿名用户所上传文件的权限 ...

  6. Jmeter使用基础笔记-写一个http请求

    前言 本篇文章主要讲述2个部分: 搭建一个简单的测试环境 用Jmeter发送一个简单的http请求 搭建测试环境 编写flask代码(我参考了开源项目HttpRunner的测试服务器),将如下的代码保 ...

  7. eshing wind/tidal turbine using Turbogrid

    Table of Contents 1. meshing wind turbine using Turbogrid 1.1. ref 1 meshing wind turbine using Turb ...

  8. Educational Codeforces Round 41 D. Pair Of Lines(961D)

    [题意概述] 给出平面上的10W个点,要求判断这些点能否被两条直线穿过,即一个点至少在一条直线上. [题解] 思路很快可以想到.取3个不共线的点,它们形成一个三角形:如果有解,其中的一条直线一定与三角 ...

  9. 九度oj 题目1473:二进制数

    题目描述: 大家都知道,数据在计算机里中存储是以二进制的形式存储的. 有一天,小明学了C语言之后,他想知道一个类型为unsigned int 类型的数字,存储在计算机中的二进制串是什么样子的. 你能帮 ...

  10. [K/3Cloud] K/3 Cloud1.0怎样和2.0共存在一台服务器上

    第一步:安装Cloud1.0,创建管理中心,创建业务数据中心,备份管理数据库和业务数据库,并且备份安装目录: 第二步:卸载Cloud1.0,清理安装目录,安装Cloud2.0,创建管理中心,创建业务数 ...