-
Notifications
You must be signed in to change notification settings - Fork 0
/
6.cpp
57 lines (53 loc) · 1.38 KB
/
6.cpp
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
#include <iostream>
#include <algorithm>
using namespace std;
struct ticket {
int id, validity, price;
};
int cutDown(ticket tickets[], int count) {
for(int validity = 0; validity <= min(30, count - 1); validity++) {
int ticketsCount = 0;
for(int i = 0; i < count; i++) {
if(tickets[i].validity == validity) {
ticketsCount++;
}
}
while(ticketsCount > validity) {
int cheapestTicketPosition = -1;
int cheapestTicketPrice = 101;
for(int i = 0; i < count; i++) {
if(tickets[i].validity == validity && tickets[i].price < cheapestTicketPrice) {
cheapestTicketPrice = tickets[i].price;
cheapestTicketPosition = i;
}
}
tickets[cheapestTicketPosition] = tickets[--count];
ticketsCount--;
}
}
return count;
}
bool comparator(const ticket t1, const ticket t2) {
return t1.id < t2.id;
}
int main() {
int count;
cin >> count;
ticket tickets[count];
for(int i = 0; i < count; i++) {
tickets[i].id = i;
cin >> tickets[i].validity >> tickets[i].price;
}
count = cutDown(tickets, count);
sort(tickets, tickets + count, comparator);
int maxProfit = 0;
do {
int profit = 0;
for(int i = 0; i < count; i++) {
profit += tickets[i].validity > i ? tickets[i].price * (tickets[i].validity - i) : 0;
}
maxProfit = max(profit, maxProfit);
} while(next_permutation(tickets, tickets + count, comparator));
cout << maxProfit << endl;
return 0;
}