protagonist

最小化价格

题目描述

现有n组人,m个地点,给出每组人的人数,每个地点可容纳的最大人数和选择的价格
要求一种方式,使得每组人都到一个各不相同的地点,最小化选择的价格
每个队伍的人都要在同一个地方每个地方只能有一个队伍

输入描述:

第一行n,m
第二行n个数,表示每组的人数
接下来m行,每行两个数,表示可容纳的最大人数和选择的价格

输出描述:

输出最小化选择的价格,无解输出-1
示例1

输入

复制
3 4
2 3 4
1 2
2 3
3 4
4 5

输出

复制
12

备注:

所有数据小于1e5
#include <bits/stdc++.h>

using namespace std;
const int maxn=1e5+666;
typedef long long ll;
ll n,m,ans;
struct node{
      int person,val;
      bool operator<(const node& tmp)const{
           return person>tmp.person;
      }
}p[maxn];
int s[maxn];
bool cmp(int a,int b){return a>b;}
int main()
{
    cin>>n>>m;
    priority_queue<int,vector<int>,greater<int> >q;
    for(int i=1;i<=n;i++)scanf("%d",&s[i]);
    for(int i=1;i<=m;i++)cin>>p[i].person>>p[i].val;
    sort(s+1,s+1+n,cmp);
    sort(p+1,p+1+m);
    for(int i=1,l=1;i<=n;i++){
         while(p[l].person>=s[i])q.push(p[l++].val);
         if(q.empty()){
              puts("-1");
              return 0;
         }
         ans+=q.top();
         q.pop();
    }
    cout<<ans<<endl;
    return 0;
}

 

posted @ 2019-03-24 19:51  czy-power  阅读(231)  评论(0编辑  收藏  举报