1822: [JSOI2010]Frozen Nova 冷冻波
2298: [HAOI2011]problem a

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

高橋直樹 posted @ 2013年4月01日 11:42 in BZOJ with tags usaco bzoj , 804 阅读

http://www.lydsy.com/JudgeOnline/problem.php?id=1691

RunID User Problem Result Memory Time Language Code_Length Submit_Time
380740 Liu_Ts 1691 Accepted 3924 kb 428 ms C++ 945 B 2013-03-31 17:18:47

 

题意

两个序列n和m,有权值a(钱),b(新鲜度)

求满足所有N序列与M序列匹配, 

M序列的元素可以匹配一个N中的元素,当且仅当M序列的值满足 m.a>=n.a && m.b>=n.b

求minΣm.a。

题解

把M和N按b排降序,

枚举N,用一个BST维护满足当前n.b的所有M中的元素。

每次去除BST中满足N.a的最小的M.a;

 

#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<set>
#define PB push_back
#define MP make_pair
using namespace std;
int n,m;
vector<pair<int,int> > cow,food;
multiset<int> s;
long long ans;
int main()
{
    scanf("%d%d",&n,&m);
    int a,b;
    for(int i=0;i<n;i++)
    {
        scanf("%d%d",&a,&b);
        cow.PB(MP(b,a));
    }
    for(int i=0;i<m;i++)
    {
        scanf("%d%d",&a,&b);
        food.PB(MP(b,a));
    }
    sort(cow.begin(),cow.end());
    sort(food.begin(),food.end());
    bool flag=true;int j=m-1;
    for(int i=n-1;i>=0;i--)
    {
        for(;j>=0;)
            if(food[j].first>=cow[i].first)
                s.insert(food[j--].second);
            else    break;
        if(s.size()==0)
        {
            puts("-1");
            return 0;
        }
        multiset<int>::iterator p=s.lower_bound(cow[i].second);
        ans+=*p;s.erase(p);
    }
    printf("%lld\n",ans);
}

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter