Description
有 批人,第 批有 人,有 个公寓,第 个公寓可以容纳 人,租金是 ,要求从这 个公寓里选出 个给这 批人且总租金最小,输出第 批人应该租的公寓编号
Input
第一行两个整数 ,之后输入 个整数 表示每批人的人数,输入 个整数 表示每个公寓可以容纳的人数,最后输入 个整数 表示每个公寓的租金
Output
如果不存在合法方案则输出 ,否则输出 个整数表示每批人租的公寓编号
Sample Input
2 5
40 200
1000 199 201 10 50
600 300 400 200 800
Sample Output
2 3
Solution
把所有公寓按可容纳人数降序排,把 批人也按人数降序排,从人数多的开始考虑,每次把所有公寓中人数不低于当前考虑人数的公寓的租金加入优先队列,每次从优先队列里选出租金最小的公寓给当前考虑的这批人,如果考虑到某批人的时候队列为空说明无解,时间复杂度
Code
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f,maxn=5005;
int n,m,r[maxn],ans[maxn];
P a[maxn],b[maxn];
priority_queue<P,vector<P>,greater<P> >que;
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)scanf("%d",&a[i].first),a[i].second=i;
for(int i=1;i<=m;i++)scanf("%d",&b[i].first),b[i].second=i;
for(int i=1;i<=m;i++)scanf("%d",&r[i]);
sort(a+1,a+n+1);
sort(b+1,b+m+1);
int flag=1,j=m;
for(int i=n;i>=1;i--)
{
while(j>=1&&b[j].first>=a[i].first)que.push(P(r[b[j].second],b[j].second)),j--;
if(que.empty())
{
flag=0;
break;
}
ans[a[i].second]=que.top().second;
que.pop();
}
if(flag)
{
for(int i=1;i<=n;i++)
printf("%d%c",ans[i],i==n?'\n':' ');
}
else printf("impossible\n");
return 0;
}