Description
有 n n 批人,第批有 ai a i 人,有 m m 个公寓,第个公寓可以容纳 pi p i 人,租金是 ri r i ,要求从这 m m 个公寓里选出个给这 n n 批人且总租金最小,输出第批人应该租的公寓编号
Input
第一行两个整数 n,m n , m ,之后输入 n n 个整数表示每批人的人数,输入 m m 个整数表示每个公寓可以容纳的人数,最后输入 m m 个整数表示每个公寓的租金
(1≤n≤m≤5000,1≤ai,pi,ri≤1000) ( 1 ≤ n ≤ m ≤ 5000 , 1 ≤ a i , p i , r i ≤ 1000 )
Output
如果不存在合法方案则输出 impossible i m p o s s i b l e ,否则输出 n n 个整数表示每批人租的公寓编号
Sample Input
2 5
40 200
1000 199 201 10 50
600 300 400 200 800
Sample Output
2 3
Solution
把所有公寓按可容纳人数降序排,把批人也按人数降序排,从人数多的开始考虑,每次把所有公寓中人数不低于当前考虑人数的公寓的租金加入优先队列,每次从优先队列里选出租金最小的公寓给当前考虑的这批人,如果考虑到某批人的时候队列为空说明无解,时间复杂度 O(mlog2m) O ( m l o g 2 m )
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;
}