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


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

    Time Limit: 5 Sec  Memory Limit: 64 MB
    Submit: 926  Solved: 477
    [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 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

    输出说明:

    给奶牛1吃价钱为2的2号牧草,奶牛2吃价钱为4的3号牧草,奶牛3分到价钱
    为2的6号牧草,奶牛4选择价钱为4的7号牧草,这种分配方案的总花费是12,为
    所有方案中花费最少的。
     
     

    code

    贪心,之后查找前驱,

    treap,splay,set等都支持

      1 #include<cstdio>
      2 #include<algorithm>
      3 #include<cstring>
      4 #include<cmath>
      5 #include<iostream>
      6 
      7 using namespace std;
      8 
      9 #define lson t[k].l
     10 #define rson t[k].r
     11 typedef long long LL;
     12 const int N = 200100;
     13 
     14 struct Data{
     15     int l,r,key,val,cnt;
     16 }t[N];
     17 struct Node{
     18     int x,y;
     19     bool operator < (const Node &c) const {
     20         if (x==c.x) return y < c.y;
     21         return x < c.x;
     22     }
     23 }a[N],b[N];
     24 int tn,Root,ans;
     25 LL Ans;
     26 
     27 inline char nc() {
     28     static char buf[100000],*p1 = buf,*p2 = buf;
     29     return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
     30 }
     31 inline int read() {
     32     int x = 0,f = 1;char ch = nc();
     33     for (; ch<'0'||ch>'9'; ch = nc()) if (ch=='-') f = -1;
     34     for (; ch>='0'&&ch<='9'; ch = nc()) x = x * 10 + ch - '0';
     35     return x * f;
     36 }
     37 inline void lturn(int &k) {
     38     int a = rson;
     39     rson = t[a].l;
     40     t[a].l = k;
     41     k = a;
     42 }
     43 inline void rturn(int &k) {
     44     int a = lson;
     45     lson = t[a].r;
     46     t[a].r = k;
     47     k = a;
     48 }
     49 void Insert(int &k,int x) {
     50     if (k==0) {
     51         ++tn;k = tn;
     52         t[k].cnt = 1;
     53         t[k].val = x;t[k].key = rand();
     54         return ;
     55     }
     56     if (x == t[k].val) t[k].cnt++;
     57     else if (x > t[k].val) {
     58         Insert(rson,x);
     59         if (t[rson].key < t[k].key) lturn(k);
     60     }
     61     else {
     62         Insert(lson,x);
     63         if (t[lson].key < t[k].key) rturn(k);
     64     }
     65 }
     66 void Delete(int &k,int x) {
     67     if (k==0) return ;
     68     if (t[k].val == x) {
     69         if (t[k].cnt > 1) {t[k].cnt --;return ;}
     70         if (t[lson].val * t[rson].val == 0) k = lson + rson;
     71         else if (t[lson].key < t[rson].key) rturn(k),Delete(k,x);
     72         else lturn(k),Delete(k,x);
     73     }
     74     else if (x > t[k].val) Delete(rson,x);
     75     else Delete(lson,x);
     76 }
     77 void getpre(int &k,int x) {
     78     if (k==0) return ;
     79     if (x >= t[k].val) ans = k,getpre(rson,x);
     80     else getpre(lson,x);
     81 }
     82 int main() {
     83     int n = read(),m = read();
     84     for (int i=1; i<=n; ++i) 
     85         a[i].x = read(),a[i].y = read();
     86     for (int i=1; i<=m; ++i) 
     87         b[i].x = read(),b[i].y = read();
     88 
     89     sort(a+1,a+n+1);
     90     sort(b+1,b+m+1);
     91     
     92     int i = 1,j = 1,c = 0;
     93     for (; i<=m; ++i) {
     94         while (a[j].x <= b[i].x && j <= n) Insert(Root,a[j].y),j++;
     95         ans = -1;getpre(Root,b[i].y);
     96         if (ans==-1) {c++;continue;}
     97         Delete(Root,t[ans].val);
     98         Ans += b[i].x;
     99     }
    100     if (j <= n || (m-c)<n) cout<<"-1";
    101     else cout<<Ans;
    102     return 0;
    103 }
  • 相关阅读:
    Docker For Windows | Setting Up Docker On Windows
    10款游戏的设计分析
    游戏中的沉浸(Flow in Games)
    游戏中运用了人工智能、机器学习等智能算法的思考
    虚幻4的智能指针
    linux下查看已安装的软件与卸载
    Centos7.2下安装mysql5.7,使用rpm包安装
    VMware 安装 CentOS7
    并发之痛 Thread,Goroutine,Actor
    Creating and using a blendspace in c++
  • 原文地址:https://www.cnblogs.com/mjtcn/p/8016511.html
Copyright © 2020-2023  润新知