Initial commit
This commit is contained in:
BIN
2020/sector/condition_moda.jpg
Normal file
BIN
2020/sector/condition_moda.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 257 KiB |
BIN
2020/sector/condition_prince.jpg
Normal file
BIN
2020/sector/condition_prince.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 163 KiB |
108
2020/sector/moda.cpp
Normal file
108
2020/sector/moda.cpp
Normal file
@@ -0,0 +1,108 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
ifstream input_stream;
|
||||
ofstream output_stream;
|
||||
|
||||
|
||||
class Order {
|
||||
public:
|
||||
int hour;
|
||||
int value;
|
||||
int pos;
|
||||
Order (int O, int V, int P) {
|
||||
hour = O;
|
||||
value = V;
|
||||
pos = P;
|
||||
}
|
||||
Order () {
|
||||
hour = -1;
|
||||
value = -1;
|
||||
pos = -1;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
bool compValue(Order &a, Order &b) {
|
||||
/**
|
||||
Sort orders by value
|
||||
*/
|
||||
if (a.value < b.value) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
input_stream.open("input.txt");
|
||||
output_stream.open("output.txt");
|
||||
|
||||
int N;
|
||||
input_stream >> N;
|
||||
|
||||
// plain orders array is used as a tmp-holder to read data
|
||||
Order orders[N];
|
||||
// Data from orders per hour is gradually appended to order queue
|
||||
vector<Order> orders_by_hour[24];
|
||||
|
||||
for (int i=0; i < 24; i++) {
|
||||
orders_by_hour[i].reserve(16);
|
||||
}
|
||||
|
||||
int gain = 0;
|
||||
int loss = 0;
|
||||
for (int i=0; i < N; i++) {
|
||||
// Read data for each order, calculate max possible gain
|
||||
input_stream >> orders[i].hour;
|
||||
|
||||
input_stream >> orders[i].value;
|
||||
gain += orders[i].value;
|
||||
|
||||
orders[i].pos = i + 1;
|
||||
|
||||
orders_by_hour[orders[i].hour - 1].push_back(orders[i]);
|
||||
}
|
||||
|
||||
// Queue is appended for each hour, then it is sorted by value
|
||||
vector<Order> order_queue;
|
||||
order_queue.reserve(N);
|
||||
|
||||
// Resulting sequence of orders in reverse.
|
||||
vector<Order> order_sequence;
|
||||
order_sequence.reserve(N);
|
||||
|
||||
for (int i = 23; i >= 0; i--) {
|
||||
// Add orders from this hour to queue
|
||||
order_queue.insert(order_queue.end(), orders_by_hour[i].begin(), orders_by_hour[i].end());
|
||||
|
||||
// If queue is not empty, add the most expensive order from queue to sequence
|
||||
if(order_queue.size() > 0) {
|
||||
sort(order_queue.begin(), order_queue.end(), compValue);
|
||||
order_sequence.push_back(order_queue.back());
|
||||
order_queue.pop_back();
|
||||
}
|
||||
}
|
||||
|
||||
// Adjust gain/loss based on the orders left in queue
|
||||
for (auto i: order_queue) {
|
||||
loss += i.value;
|
||||
gain -= i.value;
|
||||
}
|
||||
|
||||
cout << gain << " " << loss << endl;
|
||||
|
||||
for (auto i = order_sequence.rbegin(); i != order_sequence.rend(); i++) {
|
||||
cout << (*i).pos << endl;
|
||||
}
|
||||
|
||||
|
||||
input_stream.close();
|
||||
output_stream.close();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user