Cod sursa(job #1420516)

Utilizator ionanghelinaIonut Anghelina ionanghelina Data 18 aprilie 2015 16:46:13
Problema Energii Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.91 kb
#include<cstdio>
using namespace std;
int g,w,i1;
float e[1005],c[1005],r[1005],aux;
int main()
{
    freopen("energii.in","r",stdin);
    freopen("energii.out","w",stdout);
    scanf("%d",&g);
    scanf("%d",&w);
    for (int i=1;i<=g;i++)
    {
        scanf("%f%f",&e[i],&c[i]);
        r[i]=e[i]/c[i];
    }
    bool ok=1;
    int da=g-1;
    do
    {
        ok=0;
        i1=1;
        while (i1<=da)
        {
            if (r[i1]<r[i1+1])
            {
                aux=r[i1];r[i1]=r[i1+1];r[i1+1]=aux;
                aux=e[i1];e[i1]=e[i1+1];e[i1+1]=aux;
                aux=c[i1];c[i1]=c[i1+1];c[i1+1]=aux;
                ok=1;
            }
            i1++;
        }
        da--;
    }while(ok);
    int i1=1;
    float cost=0,energie=0;
    while (energie<w)
    {
        energie+=e[i1];
        cost+=c[i1];
        i1++;
    }
    printf("%.0f",cost);
    return 0;
}