Problem EEducation
Seeking to cash in on the lucrative private education business, EduCorp recently establishedthe prestigious ”Bootcamp Academy of Economics” and, counter to their early projections, isgrowing rapidly.
So rapidly, in fact, that the student body is already overflowing the small (but prestigious) campusbuilding and now needs to be contained somewhere else while more new (and prestigious)buildings are built.
Each department will sell off its original space and then move into its own new rented building.As departments are deeply territorial, buildings must not be shared. Because this is an economicsacademy, the capacities and rents of each of all the local available buildings were easy to find bydisguising the task as homework.
However, it still remains to choose which buildings to rent so as to minimise total budget. Thisis where you can help.
Input
• one line containing the integers n and m (1 ≤ n ≤ m ≤ 5000), the number of departmentsand buildings respectively.
• one line containing n integers s1 . . . sn (1 ≤ si ≤ 1000 for each i), where siis the numberof students in department i.
• one line containing m integers p1 . . . pm (1 ≤ pi ≤ 1000 for each i), where piis thecapacity of building i.
• one line containing m integers r1 . . . rm (1 ≤ ri ≤ 1000 for each i), where riis the yearlyrental cost of building i.
Output
If it is not possible to rent enough buildings for all the departments, output impossible.
Otherwise, output n unique, space-separated integers v1. . . vn, where the i-th number is thebuilding to be rented by the i-th department so as to minimise the total spend on rent. If thereare multiple equally good answers, you may print any.
题意:
n个团体,m个房子。一个房子最多只能住一个团体。
n行:每个团体人数。
m行:每个房子能住多少人。
m行:每个房子价钱。
在花费最小的情况下,输出第i个团体住的房子。
思路:
贪心
代码:
#include<iostream>
#include<algorithm>
#define inf 0x3f3f3f3f
using namespace std;
int vis[5005]={0};
int b[5005];
struct node
{
int num;
int mon;
int flag;
}s[5005],a[5005];
int cmpp(node a,node b)
{
return a.num>b.num;
}
int cmp(node a,node b)
{
return a.mon<b.mon;
}
int main()
{
int n,m;
cin>>n>>m;
for(int i=0;i<n;i++)
{
cin>>s[i].num;
s[i].flag=i;
}
sort(s,s+n,cmpp);
for(int i=0;i<m;i++)
{
cin>>a[i].num;
a[i].flag=i+1;
}
for(int i=0;i<m;i++)
cin>>a[i].mon;
sort(a,a+m,cmp);
int cnt;
for(int i=0;i<n;i++)
{
cnt=0;
for(int j=0;j<m;j++)
{
if(a[j].num>=s[i].num&&!vis[j])
{
cnt=1;
vis[j]=1;
b[s[i].flag]=a[j].flag;
break;
}
}
if(!cnt)
{
cout<<"impossible"<<endl;
break;
}
}
if(cnt)
{
for(int i=0;i<n-1;i++)
{
cout<<b[i]<<" ";
}
cout<<b[n-1]<<endl;
}
return 0;
}