Pagini recente » Cod sursa (job #1349192) | Cod sursa (job #2967919) | Cod sursa (job #3193019) | Cod sursa (job #2479352) | Cod sursa (job #958205)
Cod sursa(job #958205)
#include <fstream>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <limits>
#define MAX_WATTS 5002
#define MAX_GEN 1002
using namespace std;
int energy[2][MAX_WATTS];
int main()
{
int w, g;
int maxPrice = numeric_limits<int>::max() / 2;
pair<int, int> generators[MAX_GEN];
fstream fin("energii.in", fstream::in);
fstream fout("energii.out", fstream::out);
fin >> g >> w;
//cout << g << " " << w << endl;
for (int i=0; i<g; ++i)
{
fin >> generators[i].first >> generators[i].second;
}
//sort(generators, generators + g);
fill_n(energy[0], w + 1, maxPrice);
fill_n(energy[1], w + 1, maxPrice);
int current = 0;
energy[0][0] = energy[1][0] = 1;
for (int i=0; i<g; ++i)
{
//cout << generators[i].first << " " << generators[i].second << endl;
for (int j=0; j<w; ++j)
{
int energyIndex = min(w, j + generators[i].first);
energy[current][j] = min(energy[current][j], energy[!current][j]);
energy[current][energyIndex] = min(energy[current][energyIndex], energy[!current][energyIndex]);
if (energy[current][j] < maxPrice)
{
energy[current][energyIndex] = min(energy[current][energyIndex], generators[i].second + energy[!current][j]);
}
}
/*ostream_iterator<int> itOut(cout, " ");
copy_n(energy[current], w + 1, itOut);
cout << endl;*/
//copy_n(energy[current], w + 1, energy[!current]);
current = !current;
}
if (energy[!current][w] < maxPrice)
{
fout << energy[!current][w] - 1 << "\n";
return 0;
}
fout << -1 << "\n";
return 0;
}