zoukankan      html  css  js  c++  java
  • bzoj1691[Usaco2007 Dec]挑剔的美食家*

    bzoj1691[Usaco2007 Dec]挑剔的美食家

    题意:

    m种牧草,每种都有一个价钱和鲜度,n头奶牛,每头都有一个牧草价钱下限和牧草鲜度上限,要求从每头奶牛从m种牧草中选取一种符合要求的牧草,使得总价钱最小,两头奶牛选的种类不能相同。n,m≤100000。

    题解:

    贪心。先将所有牧草按鲜度排序,奶牛按鲜度下限排序,对于每头奶牛,选的应该是鲜度大于等于它要求的牧草中最便宜的那个,这个可以用set维护。

    代码:

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <set>
     5 #define inc(i,j,k) for(int i=j;i<=k;i++)
     6 #define maxn 100010
     7 #define ll long long
     8 using namespace std;
     9 
    10 inline int read(){
    11     char ch=getchar(); int f=1,x=0;
    12     while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
    13     while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    14     return f*x;
    15 }
    16 struct nd{int a,b; bool operator <(const nd &c)const{return b<c.b;}}cows[maxn],grass[maxn];
    17 multiset<nd>st; int n,m,j; ll ans;
    18 int main(){
    19     n=read(); m=read(); inc(i,1,n){int x=read(),y=read(); cows[i]=(nd){x,y};}
    20     inc(i,1,m){int x=read(),y=read(); grass[i]=(nd){x,y};} sort(cows+1,cows+1+n); sort(grass+1,grass+1+m);
    21     for(j=m;j>=1&&grass[j].b>=cows[n].b;j--)st.insert((nd){grass[j].b,grass[j].a});
    22     for(int i=n;i>=1;i--){
    23         multiset<nd>::iterator sti=st.lower_bound((nd){cows[i].b,cows[i].a});
    24         if(sti==st.end()){printf("-1"); return 0;} ans+=sti->b; st.erase(sti);
    25         for(j;j>=1&&grass[j].b>=cows[i-1].b;j--)st.insert((nd){grass[j].b,grass[j].a});
    26     }
    27     printf("%lld",ans); return 0;
    28 }

    20160922

  • 相关阅读:
    java中将表单转换为PDF
    base64图片
    ORACLE中用户等系统信息操作
    jquery中live is not a function的问题
    完全卸载Oracle11G
    jquery 获取鼠标和元素的坐标点
    JS的多线程
    Oracle和SQLServer解锁杀进程
    JAVA 通过LDAP获取AD域用户及组织信息
    oracle基础语法大全
  • 原文地址:https://www.cnblogs.com/YuanZiming/p/5906112.html
Copyright © 2011-2022 走看看