高端贪心,好久没写splay调了好久……

以下v为价格,w为鲜嫩度

把牛和草都按v排升序,扫草,首先把v小于等于当前草的牛都丢进splay,这样一来splay里全是可选的牛了,按w排序,然后贪心的为当前的草取牛:w小于等于当前草的w的牛,取出来删除,ans加上当前草的价格(没有则跳过)

取牛的时候统计一下来判无解

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=100005,inf=1e9+7;
int n,m,tot,con,rot;
struct phs
{
int f,c[2],v,s;
}t[N];
struct qwe
{
int v,w;
}a[N],b[N];
bool cmp(const qwe &a,const qwe &b)
{
return a.v<b.v||(a.v==b.v&&a.w<b.w);;
}
int read()
{
int r=0,f=1;
char p=getchar();
while(p>'9'||p<'0')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
inline void ud(int x)
{
t[x].s=t[t[x].c[0]].s+t[t[x].c[1]].s+1;
}
void zhuan(int x,int &k)
{
int y=t[x].f,z=t[y].f,l=t[y].c[0]!=x,r=l^1;
if(y!=k)
t[z].c[t[z].c[0]!=y]=x;
else
k=x;
t[x].f=z;
t[y].c[l]=t[x].c[r];
t[t[y].c[l]].f=y;
t[x].c[r]=y;
t[y].f=x;
ud(y);
ud(x);
}
void splay(int x,int &k)
{
while(x!=k)
{//cerr<<x<<" "<<k<<endl;
int y=t[x].f,z=t[y].f;
if(y!=k)
{
if((t[y].c[0]==x)^(t[z].c[0]==y))
zhuan(x,k);
else
zhuan(y,k);
}
zhuan(x,k);
}
}
int rnk(int x,int v)
{//cerr<<"rnk"<<endl;
int re=0;
while(x)
{
if(t[x].v<=v)
re+=t[t[x].c[0]].s+1,x=t[x].c[1];
else
x=t[x].c[0];
}
return re;
}
int shu(int x,int k)
{//cerr<<"shu"<<endl;
if(!x)
return 0;
if(t[t[x].c[0]].s+1==k)
return x;
else if(t[t[x].c[0]].s+1<k)
return shu(t[x].c[1],k-t[t[x].c[0]].s-1);
else
return shu(t[x].c[0],k);
}
void charu(int w)
{//cerr<<"charu"<<" "<<w<<endl;
int x=shu(rot,rnk(rot,w));
splay(x,rot);
int y=shu(t[x].c[1],1);
splay(y,t[x].c[1]);
t[y].c[0]=++tot;
t[tot].f=y;
t[tot].v=w;
t[tot].s=1;
ud(y);
ud(x);
}
void shanchu(int rk)
{//cerr<<"shanchu"<<endl;
int x=shu(rot,rk-1);
splay(x,rot);
int y=shu(t[x].c[1],2);
splay(y,t[x].c[1]);
t[t[y].c[0]].f=0;
t[y].c[0]=0;
ud(y);
ud(x);
}
int main()
{
n=read(),m=read();
for(int i=1;i<=n;i++)
a[i].v=read(),a[i].w=read();
for(int i=1;i<=m;i++)
b[i].v=read(),b[i].w=read();
sort(a+1,a+1+n,cmp);
sort(b+1,b+1+m,cmp);
long long ans=0;
t[1].f=2,t[1].s=1,t[1].v=-inf;
t[2].s=4,t[2].c[0]=1,t[2].c[1]=3,t[2].v=-inf;
t[3].f=2,t[3].c[0]=4,t[3].s=2,t[3].v=inf;
t[4].f=3,t[4].s=1,t[4].v=inf;
tot=4,rot=2;
for(int i=1,j=1;i<=m&&con<=n;i++)
{//cerr<<b[i].v<<" "<<b[i].w<<endl;
while(j<=n&&a[j].v<=b[i].v)
charu(a[j++].w);//,cerr<<a[j-1].v<<" "<<a[j-1].w<<endl;
int now=rnk(rot,b[i].w);//cerr<<b[i].w<<" "<<now<<endl;
if(now>2)
ans+=b[i].v,con++,shanchu(now);//,cerr<<b[i].v<<" "<<b[i].w<<endl;cerr<<endl;
}//cerr<<ans<<endl;
printf("%lld\n",con<n?-1ll:ans);
return 0;
}

bzoj 1691: [Usaco2007 Dec]挑剔的美食家【贪心+splay】的更多相关文章

  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. BZOJ 1691 [Usaco2007 Dec]挑剔的美食家 multiset_排序_贪心

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. HDU3032 nim博弈

    题目大意: 可以从某一堆中取任意个数,也可把一堆分成两个不为0的堆,直到某一方无法操作为输 因为是nim博弈,所以只要考虑一堆时候的sg值,把所有堆的sg值异或即可 很显然这里 0 是一个终止态 sg ...

  2. hdu 1075

    #include<stdio.h> #include<string.h> #include<string> #include<iostream> #in ...

  3. SQL SERVER 2012 第三章 T-SQL 基本语句 group by 聚合函数

    select Name,salesPersonID From Sales.store where name between 'g' and 'j' and salespersonID > 283 ...

  4. Spring MVC中的拦截器/过滤器HandlerInterceptorAdapter的使用

    一般情况下,对来自浏览器的请求的拦截,是利用Filter实现的 而在Spring中,基于Filter这种方式可以实现Bean预处理.后处理. 比如注入FilterRegistrationBean,然后 ...

  5. Ubuntu 16.04安装qt5-default报错:qt5-default : 依赖: qtbase5-dev E: 无法修正错误,因为您要求某些软件包保持现状,就是它们破坏了软件包间的依赖关系。(此类问题终极解决方法)

    切记:没事不要进行sudo apt-get upgrade 错误: qt5-default : 依赖: qtbase5-dev E: 无法修正错误,因为您要求某些软件包保持现状,就是它们破坏了软件包间 ...

  6. MongoDB小结13 - find【2个参数】

    第一个参数 MongoDB使用find来进行查询,查询呢,就是返回一个集合中文档的子集,子集合的范围从0个文档到整个集合. find的第一个参数决定了要返回那些文档,其形式也就一个文档,说明了要执行的 ...

  7. springboot application.properties

    verify if you have this items: @Bean public CommonsMultipartResolver multipartResolver() { CommonsMu ...

  8. C#使用全局钩子(hook),SetWindowsHookEx返回0、不回调的解决

    http://www.csharpwin.com/csharpspace/3766r5747.shtml 在.net 2005平台下 在使用全局hook时,总是遇见SetWindowsHookEx的返 ...

  9. 【CV论文阅读】Unsupervised deep embedding for clustering analysis

    Unsupervised deep embedding for clustering analysis 偶然发现这篇发在ICML2016的论文,它主要的关注点在于unsupervised deep e ...

  10. Logstash学习系列之基础介绍

    Logstash功能特性 能集中处理各种类型的数据 能标准化不同模式和格式的数据 能快速的扩展自定义日志的格式 它具有收集,分析和转发数据流的功能 Logstash运行参数 -f 指定配置文件 -e ...