-
Notifications
You must be signed in to change notification settings - Fork 0
/
six_flags.cc
67 lines (60 loc) · 1.99 KB
/
six_flags.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* ----------------------------------------------------------------------- *
*
* Author: Leandro Augusto Lacerda Campos <leandro.campos@dcc.ufmg.br>
*
* Federal University of Minas Gerais (UFMG)
* Department of Computer Science
* Graduate Program in Computer Science
* Design and Analysis of Algorithms
*
* Solution for Six Flags problem using dynamic programming.
* Problem statement:
* https://www.urionlinejudge.com.br/judge/pt/problems/view/1487
*
* ----------------------------------------------------------------------- */
#include <algorithm>
#include <iostream>
#include <vector>
// Represents an attraction of the park.
struct Attraction {
int duration; // in minutes.
int score;
};
// Fills each element of the vector of attractions with information provided
// by the standard input.
inline void GetAttractionsInfo(std::vector<Attraction>& attractions) {
for (auto& a : attractions) {
std::cin >> a.duration;
std::cin >> a.score;
}
}
// Returns the highest possible score for a collection of attractions that
// respects the available time provided. An attraction may appear more than
// once in a collection.
int SelectAttractions(std::vector<Attraction>& attractions,
int available_time) {
std::vector<int> scores(available_time + 1, 0);
for (int t = 1; t <= available_time; t++) {
for (auto& a : attractions) {
if (a.duration <= t) {
scores[t] = std::max(scores[t], a.score + scores[t - a.duration]);
}
}
}
return scores[available_time];
}
int main(void) {
int num_attractions;
int available_time;
std::vector<Attraction> attractions;
int instance = 1;
while (std::cin >> num_attractions && num_attractions > 0) {
attractions.resize(num_attractions);
std::cin >> available_time;
GetAttractionsInfo(attractions);
std::cout << "Instancia " << instance++ << std::endl;
std::cout << SelectAttractions(attractions, available_time) << std::endl;
std::cout << std::endl;
}
return 0;
}