GYM 101606 E.Education(尺取+优先队列)

253 篇文章 2 订阅
19 篇文章 0 订阅

Description

n n 批人,第i批有 ai a i 人,有 m m 个公寓,第i个公寓可以容纳 pi p i 人,租金是 ri r i ,要求从这 m m 个公寓里选出n个给这 n n 批人且总租金最小,输出第i批人应该租的公寓编号

Input

第一行两个整数 n,m n , m ,之后输入 n n 个整数ai表示每批人的人数,输入 m m 个整数pi表示每个公寓可以容纳的人数,最后输入 m m 个整数ri表示每个公寓的租金

(1nm5000,1ai,pi,ri1000) ( 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

把所有公寓按可容纳人数降序排,把n批人也按人数降序排,从人数多的开始考虑,每次把所有公寓中人数不低于当前考虑人数的公寓的租金加入优先队列,每次从优先队列里选出租金最小的公寓给当前考虑的这批人,如果考虑到某批人的时候队列为空说明无解,时间复杂度 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值