POJ3622 Gourmet Grazers(FHQ Treap)
Description
Like so many others, the cows have developed very haughty tastes and will no longer graze on just any grass. Instead, Farmer John must purchase gourmet organic grass at the Green Grass Grocers store for each of his N (1 ≤ N ≤ 100,000) cows.
Each cow i demands grass of price at least Ai (1 ≤ Ai ≤ 1,000,000,000) and with a greenness score at least Bi (1 ≤ Bi ≤ 1,000,000,000). The GGG store has M (1 ≤ M ≤ 100,000) different types of grass available, each with a price Ci (1 ≤ Ci ≤ 1,000,000,000) and a greenness score of Di (1 ≤ Di ≤ 1,000,000,000). Of course, no cow would sacrifice her individuality, so no two cows can have the same kind of grass.
Help Farmer John satisfy the cows' expensive gourmet tastes while spending as little money as is necessary.
Input
* Line 1: Two space-separated integers: N and M. * Lines 2..N+1: Line i+1 contains two space-separated integers: Ai and Bi * Lines N+2..N+M+1: Line i+N+1 contains two space-separated integers: Ci and Di
Output
* Line 1: A single integer which is the minimum cost to satisfy all the cows. If that is not possible, output -1.
Sample Input
4 7
1 1
2 3
1 4
4 2
3 2
2 1
4 3
5 2
5 4
2 6
4 4
Sample Output
12
Source
USACO 2007 December Gold
一开始一眼二分图,但是时间复杂度太高了
我们考虑贪心
因为他让着求最小的钱数
所以我们按照钱数排序,
然后枚举牧草,对于每一个牧草,我们统计一下它可以满足哪些奶牛
然后在能满足的奶牛中取一个需要的新鲜度离他最近的
这个过程显然可以用平衡树维护。
平衡树我用的是FHQ Treap
1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<ctime>
5 #include<cstdlib>
6 #include<algorithm>
7 using namespace std;
8 #define ls T[now].ch[0]
9 #define rs T[now].ch[1]
10 const int MAXN=3*1e5+10;
11 inline char nc()
12 {
13 static char buf[MAXN],*p1=buf,*p2=buf;
14 return p1==p2&&(p2=(p1=buf)+fread(buf,1,MAXN,stdin),p1==p2)?EOF:*p1++;
15 }
16 inline int read()
17 {
18 char c=nc();int x=0,f=1;
19 while(c<'0'||c>'9'){if(c=='-')f=-1;c=nc();}
20 while(c>='0'&&c<='9'){x=x*10+c-'0',c=nc();}
21 return x*f;
22 }
23 struct node
24 {
25 int val,ch[2],pri,siz;
26 }T[MAXN];
27 int x,y,z,root=0,pos;
28 int tot=0;
29 void update(int now)
30 {
31 T[now].siz=T[ls].siz+T[rs].siz+1;
32 }
33 inline int newnode(int v)
34 {
35 T[++tot].val=v;
36 T[tot].pri=rand();
37 T[tot].siz=1;
38 return tot;
39 }
40 int merge(int x,int y)
41 {
42 if(!x||!y) return x+y;
43 if(T[x].pri<T[y].pri)
44 {
45 T[x].ch[1]=merge(T[x].ch[1],y);
46 update(x);
47 return x;
48 }
49 else
50 {
51 T[y].ch[0]=merge(x,T[y].ch[0]);
52 update(y);
53 return y;
54 }
55 }
56 void split(int now,int k,int &x,int &y)
57 {
58 if(!now) {x=y=0;return ;}
59 if(T[now].val<=k) x=now,split(rs,k,rs,y);
60 else y=now,split(ls,k,x,ls);
61 update(now);
62 }
63 struct N
64 {
65 int x,y;
66 }a[MAXN],b[MAXN];
67 int comp(const N &a,const N &b)
68 {
69 return a.x<b.x||(a.x==b.x&&a.y<b.y);
70 }
71 void insert(int val)
72 {
73 split(root,val,x,y);
74 root=merge( merge(x,newnode(val)) , y );
75 }
76 int kth(int now,int x)
77 {
78 while(now)
79 {
80 if(T[ls].siz+1==x) return now;
81 else if(T[ls].siz>=x) now=ls;
82 else x-=T[ls].siz+1,now=rs;
83 }
84 }
85 void Delet(int now)
86 {
87 split(root,now,x,z);
88 split(x,now-1,x,y);
89 y=merge(T[y].ch[0] , T[y].ch[1] );
90 root=merge( merge(x,y) ,z );
91 }
92 int main()
93 {
94 #ifdef WIN32
95 freopen("a.in","r",stdin);
96 #else
97 #endif
98 int n,m;
99 scanf("%d%d",&n,&m);
100 if (m<n){puts("-1");return 0;}
101 for(int i=1;i<=n;i++) scanf("%d%d",&a[i].x,&a[i].y);
102 for(int i=1;i<=m;i++) scanf("%d%d",&b[i].x,&b[i].y);
103 sort(a+1,a+n+1,comp);
104 sort(b+1,b+m+1,comp);
105 long long int cur=1,ans=0,num=0;//在第一个中找到了几个
106 for(int i=1;i<=m;i++)
107 {
108 while(a[cur].x<=b[i].x&&cur<=n)
109 insert(a[cur].y),cur++;
110 split(root,b[i].y,x,y);
111 pos=kth(x,T[x].siz);
112 if(pos==0) continue;
113 num++;
114 root=merge(x,y);
115 Delet(T[pos].val);
116 ans+=b[i].x;
117 }
118 if(num==n) printf("%lld",ans);
119 else printf("-1");
120 return 0;
121 }
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Activity onStop,onDestroy延迟10s执行
- 内存优化实战
- Nali:一个离线查询 IP 地理信息和 CDN 提供商的终端利器
- MySQL8.0的几个新特性
- read_only和super_read_only参数的区别
- AWS 命名提示需要指定 region
- AWS CodeArtifact 如何设置用户的 TOKEN
- GORM V2 自动迁移和迁移接口的方法
- Vue Nginx反向代理配置 解决生产环境跨域
- react的事件处理为什么要bind this 改变this的指向?
- 没有用到React,为什么我需要import引入React?
- JSON对象和JavaScript对象直接量的区别--不同之处
- (Demo分享)利用原生JavaScript-随机数-实现做一个烟花案例
- 关于DOM的理解
- 关于闭包函数和递归函数的详细理解