POJ3622 Gourmet Grazers(FHQ Treap)

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 2363   Accepted: 881

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

 
 
 
一开始一眼二分图,但是时间复杂度太高了
我们考虑贪心
因为他让着求最小的钱数
所以我们按照钱数排序,
然后枚举牧草,对于每一个牧草,我们统计一下它可以满足哪些奶牛
然后在能满足的奶牛中取一个需要的新鲜度离他最近的
这个过程显然可以用平衡树维护。
平衡树我用的是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 }
 
原文地址:https://www.cnblogs.com/zwfymqz/p/8029313.html