Pagini recente » Cod sursa (job #17757) | Cod sursa (job #2595800) | Cod sursa (job #2633058) | Cod sursa (job #1189646) | Cod sursa (job #1254567)
#include <stdio.h>
#include <algorithm>
using namespace std;
struct gutui
{
long w, h;
};
bool gsortCond(gutui g1, gutui g2)
{
if (g1.h == g2.h)
return g1.w > g2.w;
else
return g1.h > g2.h;
}
void readData(const char *fileName, gutui **gParam, long *gmax, long *H, long *U)
{
FILE *file = NULL;
gutui *g = NULL;;
int i;
file = fopen(fileName, "r");
if (file)
{
fscanf(file, "%i%i%i", gmax, H, U);
g = new gutui[*gmax];
for (i=0; i<*gmax; ++i)
fscanf(file, "%li%li", &g[i].h, &g[i].w);
}
else
{
printf("File %s not found.\n", fileName);
return;
}
fclose(file);
sort(g, g+*gmax, gsortCond);
*gParam = g;
}
void writeData(const char *fileName, const long long w)
{
FILE *file = NULL;
file = fopen(fileName, "w");
fprintf(file, "%lli", w);
fclose(file);
}
void gSolve(gutui *g, long gmax, long H, long U, long long *w)
{
long i;
for (i=0, *w=0; i<gmax; ++i)
{
if (g[i].h <= H)
{
*w += g[i].w;
H -= U;
}
}
}
int main()
{
gutui *g = NULL;
long H, U, gmax;
long long w;
readData("gutui.in", &g, &gmax, &H, &U);
gSolve(g, gmax, H, U, &w);
writeData("gutui.out", w);
return 0;
}