Cod sursa(job #1255696)

Utilizator devilz05Orzan Alexandru devilz05 Data 5 noiembrie 2014 01:33:48
Problema Gutui Scor 10
Compilator cpp Status done
Runda teme_upb Marime 1.67 kb
#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, "%li%li%li", 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, wTemp;
    for (i=0, *w=0; i<gmax; ++i)
    {
        if (g[i].h <= H)
        {
            wTemp = g[i].w;
            if (g[i].h + U > H)
            {
                while (g[++i].h + U > H)
                    if (wTemp < g[i].w)
                        wTemp = g[i].w;
                --i;
            }
            *w += wTemp;
            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;
}