Pagini recente » Cod sursa (job #1878463) | Cod sursa (job #1769038) | Cod sursa (job #2099073) | Cod sursa (job #508832) | Cod sursa (job #2055549)
#include <bits/stdc++.h>
using namespace std;
ifstream f ("rucsac.in");
ofstream g ("rucsac.out");
int n, gMax;
struct obiect {
int greutate;
int profit;
float raport;
};
obiect obiecte[5005];
void citire() {
int profit;
f >> n >> gMax;
for (int i = 1; i <= n; ++i) {
f >> obiecte[i].greutate;
f >> obiecte[i].profit;
obiecte[i].raport = (float)obiecte[i].profit/obiecte[i].greutate;
}
}
bool compare(obiect leftValue, obiect rightValue) {
return (leftValue.profit > rightValue.profit) ||
((leftValue.profit == rightValue.profit) &&
(leftValue.raport > rightValue.raport));
}
void procesare() {
int greutate = 0;
float profit = 0;
//sorteaza bine (primul obiect e 9)
sort(obiecte + 1, obiecte + 1 + n, compare);
for (int i = 1; i <= n; ++i) {
if (greutate + obiecte[i].greutate <= gMax) {
greutate += obiecte[i].greutate;
profit += obiecte[i].profit;
//cout << obiecte[i].greutate << ' ' << obiecte[i].profit << '\n';
}
}
g << profit;
}
void afiseazaSir() {
for (int i = 1; i <= n; ++i) {
//cout << obiecte[i].greutate << ' ' << obiecte[i].profit << ' '
//<< obiecte[i].raport << '\n';
}
}
int main()
{
citire();
procesare();
afiseazaSir();
return 0;
}