Assign departments to buildings by a deterministic greedy rule after sorting students descending, matching each to the cheapest available building that fits.
Medium5GreedySortingImplementationArrayInterviewNo attempts yetTime limit5sMemory limit512 MBEduCorp opened the Bootcamp Academy of Economics to get into the private education business. Against its own early projections, the academy is growing fast.
It is growing so fast that the students no longer fit in the current building. New buildings are on the way, and until they are ready the students have to sit somewhere else.
Each department sells its original space and moves into a rented building of its own. Departments never share, so one building holds exactly one department. This is an economics academy, so the capacity and the rent of every building available nearby were already collected in a survey disguised as homework.
What is left is choosing which buildings to rent so that the total rent is as small as possible.
The first line contains the number of departments n and the number of buildings m. (1≤n≤m≤5000)
The second line contains the integers s1,…,sn, where si is the number of students in department i. (1≤si≤1000)
The third line contains the integers p1,…,pm, where pj is the capacity of building j. (1≤pj≤1000)
The fourth line contains the integers r1,…,rm, where rj is the yearly rent of building j. (1≤rj≤1000)
The numbers on one line are separated by single spaces.
If the departments cannot all be given a building, print impossible on the first line.
Otherwise print the integers v1,…,vn on one line, separated by single spaces. vi is the number of the building rented by department i. Two departments never rent the same building, and pvi≥si holds for every i.
Several assignments can reach the smallest total rent rv1+⋯+rvn. Only the one built by the following rule is accepted.
Handle the departments in order of decreasing student count, and handle departments with the same student count in increasing order of their number. On a department's turn, rent the cheapest building among the buildings that no department has rented yet and whose capacity is at least that department's student count. If several of those buildings have the same rent, rent the one with the smallest number. If a department's turn has no building left to rent, print impossible.
This rule always produces an assignment whose total rent is minimum.