Wannafly挑战赛15 A最小化价格

链接: https://www.nowcoder.com/acm/contest/112/A
来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld

题目描述

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

输入描述:

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

输出描述:

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

#include <stdio.h>
#include<string.h>
#include<algorithm>
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f 
using namespace std;
const int maxn=100050;
struct node
{
	int num;
	int prie;
	bool vis;
}e[maxn];
int a[maxn];
bool cmp(int a,int b)
{
	return a<b;
}
bool cmp1(node a,node b)
{
	return a.num<b.num;
}
struct pqcmp
{
	bool operator () (node &a, node &b)
	{
		return a.prie>b.prie;  //实际上按小到大排序的,必须这么写 
	}
};
int main() 
{
	//freopen("C:/input.txt", "r", stdin);
	int n,m;
	int cnt=0;
	int sum=0;
	scanf("%d%d",&n,&m);
	for(int i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
	for(int i=0;i<m;i++)
	{
		scanf("%d%d",&e[i].num,&e[i].prie);
	}
	sort(a,a+n,cmp);
	sort(e,e+m,cmp1);
	priority_queue<node,vector<node> ,pqcmp>q;
	int i=n-1;
	int j=m-1;
	while(i>=0)   ///从小到大排序,反过来从最大的开始处理 
	{
		while(j>=0&&e[j].num>=a[i])
		{
			q.push(e[j--]);
		}
		if(q.empty())
		{
			printf("-1\n"); 
			return 0;
		}
		else
		{
			node a=q.top();
			sum+=a.prie;
			q.pop();
			i--;
		}
	}
	printf("%d\n",sum);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/evildoer_llc/article/details/80304784
今日推荐