zoukankan      html  css  js  c++  java
  • 最小化价格

    题目描述

    现有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;
    }
  • 相关阅读:
    python 的 类属性 与 实例属性
    python 的 append 和 extend
    机器学习的最佳入门学习资源
    各种排序算法探讨与实现
    C++基础:C++中vector使用简介
    C++基础:C++中的explicit关键字
    差分约束(poj 1201
    codeforeces 547C
    2015 7月 做题记录
    set&map
  • 原文地址:https://www.cnblogs.com/czy-power/p/10589956.html
Copyright © 2011-2022 走看看