现有n组人,m个地点,给出每组人的人数,每个地点可容纳的最大人数和选择的价格
要求一种方式,使得每组人都到一个各不相同的地点,最小化选择的价格
每个队伍的人都要在同一个地方每个地方只能有一个队伍
以上是题,刚开始想到结构体排序,二分,找规律等办法,但是都走不通,题解上看到别人用 vector+优先队列 实现了每段每段的排序是真的很厉害了,都差点忘了vector的这个用法,之前一直当数组用的,所以可以说这个题非常有价值了。
样例是
3 4
2 3 4
1 2
2 3
3 4
4 5
使用vector把地点x,价格y数据保存成这样:
x: 1 2 3 4
v[x].y : 2 3 4 5
然后就可以把每个x地点对应y放入优先队列q排序,就很好操作了
实现了昨天比赛的时候想的 :要是能分开给他排序就好了。以下是代码;
#include"cstdio"
#include"vector"
#include"queue"
#include"algorithm"
#include"iostream"
using namespace std;
const int maxn = 100000+10;
vector<int> v[maxn];
priority_queue<int, vector<int>,greater<int> > q;//优先队列
int main()
{
int n,m;
int a[maxn]={0};
scanf("%d %d",&n,&m);
for(int i = 0;i < n ;i ++)
{
int x;
scanf("%d",&x);
a[x]++;
}
for(int i = 0;i < m;i ++)
{
int x,y;
scanf("%d %d",&x,&y);
v[x].push_back(y);
}
int sum=0,tot=0;
for(int i = maxn;i >= 1;i --)
{
for(int j = 0;j < v[i].size();j ++)
{
q.push(v[i][j]);
}
while(!q.empty() && a[i]!=0)
{
int x=q.top();
q.pop();
sum += x;
a[i]--;
tot++;
// cout<<"t==="<<tot<<"x="<<x<<endl;
}
}
if(tot!=n)
{
puts("-1");
}
else
{
printf("%d\n",sum);
}
return 0;
}