CISC/CMPE-365 Fall 2015 Week 5 Congratulations! You have been

advertisement
CISC/CMPE-365
Fall 2015
Week 5
Congratulations! You have been hired as chief fundraiser for the Brine Party of Elbonia,
which is founded on the principle of equal rights for marine invertebrates. Your new boss
(who happens to be a lobster) is eager to get the party on a firm financial footing (clawing?),
and it is your job to apply to all the various funding agencies and potential sponsors. Time is
of the essence – your contract ends after just n days.
Each application takes exactly one working day to complete (you have to visit the agency, fill
out forms, engage in interviews, etc.) Each funding agency has set a deadline for
applications – applications received after the deadline are automatically rejected. Also, each
funding agency offers a fixed amount of funding, which is guaranteed as long as you apply
on or before the deadline. Thus each agency can be represented by a pair of integers (d,v) in
which d represents the deadline, and v represents the value.
The problem is that there are more funding agencies than days in your contract, and many of
the deadlines are early. Your job is to choose a subset of the applications, and schedule them
so as to obtain the maximum possible total amount of funding. Hint: use a greedy algorithm
based on the values of the grants.
Example:
Suppose you have been given 4 days for your task and there are 5 funding agencies,
represented by deadlines and values as shown in this table:
agency
1
2
3
4
5
d
3
3
2
1
2
v (in $1000s)
6
4
5
3
4
One solution would be
Day 1: Agency 4: value 3
Day 2: Agency 5: value 4
Day 3: Agency 2: value 4
Day 4: rest
with a total value of 11.
However, a better solution would be
Day 1: Agency 2: value 4
Day 2: Agency 3: value 5
Day 3: Agency 1: value 6
Day 4: rest
with a total value of 15.
Input and Output
Input to your program consists of a sequence of lines, each containing 2 integers:
On the first line, the first integer is n, the number of days in your contract. The second integer
is m, the number of funding agencies.
The m following lines contain a d value and a v value, in that order. The lines representing
the agencies are in no particular order.
The input for the example problem above would be
4
3
3
2
1
2
5
6
4
5
3
4
Output should be a presentation of the planned schedule and the total value, something like
the solutions shown above (only the best solution need be reported, of course).
Download