BZOJ 1691: [Usaco2007 Dec]挑剔的美食家( 平衡树 )
按鮮嫩程度排個序, 從大到小處理, 用平衡樹維護價值?
----------------------------------------------------------------------
#include<bits/stdc++.h>using namespace std;typedef long long ll;const int maxn = 100009;const int INF = 2000000000;struct cow {int w, v;void Read() {scanf("%d%d", &w, &v);}bool operator < (const cow &o) const {return v > o.v;}} A[maxn];struct O {int w, v;void Read() {scanf("%d%d", &w, &v);}bool operator < (const O &o) const {return v > o.v;}} B[maxn];int N, M;struct Node {Node* ch[2];int v, s, r;} pool[maxn], *pt = pool, *null, *root;void init() {null = pt++;null->ch[0] = null->ch[1] = null;null->v = null->s = 0;}Node* newNode(int v) {pt->v = v; pt->s = 1; pt->r = rand(); pt->ch[0] = pt->ch[1] = null; return pt++;}void Rotate(Node* &t, int d) {Node* o = t->ch[d ^ 1];t->ch[d ^ 1] = o->ch[d];o->ch[d] = t;t = o;}void Insert(Node* &t, int v) {if(t == null)t = newNode(v);else {int d = (t->v < v);Insert(t->ch[d], v);if(t->ch[d]->r > t->r) Rotate(t, d ^ 1);}}void Delete(Node* &t, int v) {int d = (t->v != v ? (t->v < v) : -1);if(d == -1) {if(t->ch[0] != null && t->ch[1] != null) {int d = (t->ch[0]->r > t->ch[1]->r);Rotate(t, d); Delete(t->ch[d], v);} elset = (t->ch[0] != null ? t->ch[0] : t->ch[1]);} else?Delete(t->ch[d], v);}int succ(int v) {int ret = INF;for(Node* t = root; t != null;)if(t->v >= v)?ret = min(ret, t->v), t = t->ch[0];elset = t->ch[1];return ret;}int main() {init(); root = null;scanf("%d%d", &N, &M);for(int i = 0; i < N; i++) A[i].Read();for(int i = 0; i < M; i++) B[i].Read();sort(A, A + N); sort(B, B + M);int p = 0; ll ans = 0;for(int i = 0; i < N; i++) {while(p < M && B[p].v >= A[i].v)Insert(root, B[p++].w);int t = succ(A[i].w);Delete(root, t);ans += t;}printf("%lld\n", ans);return 0;}----------------------------------------------------------------------
1691: [Usaco2007 Dec]挑剔的美食家
Time Limit:?5 Sec??Memory Limit:?64 MBSubmit:?585??Solved:?261
[Submit][Status][Discuss]
Description
與很多奶牛一樣,Farmer John那群養尊處優的奶牛們對食物越來越挑剔,隨便拿堆草就能打發她們午飯的日子自然是一去不返了。現在,Farmer John不得不去牧草專供商那里購買大量美味多汁的牧草,來滿足他那N(1 <= N <= 100,000)頭挑剔的奶牛。 所有奶牛都對FJ提出了她對牧草的要求:第i頭奶牛要求她的食物每份的價錢不低于A_i(1 <= A_i <= 1,000,000,000),并且鮮嫩程度不能低于B_i(1 <= B_i <= 1,000,000,000)。商店里供應M(1 <= M <= 100,000)種不同的牧草,第i 種牧草的定價為C_i(1 <= C_i <= 1,000,000,000),鮮嫩程度為D_i (1 <= D_i <= 1,000,000,000)。 為了顯示她們的與眾不同,每頭奶牛都要求她的食物是獨一無二的,也就是說,沒有哪兩頭奶牛會選擇同一種食物。 Farmer John想知道,為了讓所有奶牛滿意,他最少得在購買食物上花多少錢。
Input
* 第1行: 2個用空格隔開的整數:N 和 M
* 第2..N+1行: 第i+1行包含2個用空格隔開的整數:A_i、B_i * 第N+2..N+M+1行: 第j+N+1行包含2個用空格隔開的整數:C_i、D_i
Output
* 第1行: 輸出1個整數,表示使所有奶牛滿意的最小花費。如果無論如何都無法 滿足所有奶牛的需求,輸出-1
Sample Input
4 71 1
2 3
1 4
4 2
3 2
2 1
4 3
5 2
5 4
2 6
4 4
Sample Output
12輸出說明:
給奶牛1吃價錢為2的2號牧草,奶牛2吃價錢為4的3號牧草,奶牛3分到價錢
為2的6號牧草,奶牛4選擇價錢為4的7號牧草,這種分配方案的總花費是12,為
所有方案中花費最少的。
HINT
Source
Gold
?
轉載于:https://www.cnblogs.com/JSZX11556/p/4883809.html
總結
以上是生活随笔為你收集整理的BZOJ 1691: [Usaco2007 Dec]挑剔的美食家( 平衡树 )的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: chown命令
- 下一篇: Ubuntu 使用记录