Pagini recente » Cod sursa (job #503422) | Cod sursa (job #2000357) | Cod sursa (job #2154793) | Cod sursa (job #1007442) | Cod sursa (job #438761)
Cod sursa(job #438761)
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
struct Apple {
int h, w;
friend bool operator<(Apple const& a, Apple const& b) {
return a.h < b.h or (a.h == b.h and a.w < b.w);
}
friend bool operator>(Apple const& a, Apple const& b) {
return b < a;
}
friend std::ostream& operator<<(std::ostream& s, Apple const& v) {
s << '(' << v.h << ", " << v.w << ')';
return s;
}
};
template<class T, class C, T C::*M>
struct sum {
T operator()(T const& cur, C const& next) { return cur + next.*M; }
};
int solve(std::vector<Apple> apples, int H, int U) {
if (U == 0) {
return std::accumulate(apples.begin(), apples.end(), 0, sum<int, Apple, &Apple::w>());
}
std::sort(apples.begin(), apples.end(), std::greater<Apple>());
int picked_weight = 0;
std::vector<int> available_weights;
int offset = U - H % U;
if (offset == U) offset = 0;
int top = offset - U;
while ((apples.size() or available_weights.size()) and top <= H) {
if (available_weights.size()) {
picked_weight += available_weights.front();
std::pop_heap(available_weights.begin(), available_weights.end());
available_weights.pop_back();
top += U;
}
else {
top += U * std::max(1, (apples.back().h - top) / U);
}
while (apples.size() and apples.back().h <= top) {
available_weights.push_back(apples.back().w);
std::push_heap(available_weights.begin(), available_weights.end());
apples.pop_back();
}
}
return picked_weight;
}
int main() {
FILE* fd1;
FILE* fd2;
fd1=fopen("gutui.out", "w");
fd2=fopen("gutui.in", "r");
int nomberApples, height, step;
fscanf(fd2, "%i %i %i", &nomberApples, &height, &step);
Apple data[nomberApples];
int i;
for (i = 0; i < nomberApples; i++)
fscanf(fd2, "%i %i ", &(data[i].h), &(data[i].w));
std::vector<Apple> v (data, data + nomberApples); // ce e asta?
int actual = solve(v, height, step);
fprintf(fd1, "%i ", actual);
fclose(fd2);
fclose(fd1);
}