Cod sursa(job #1060386)

Utilizator chiriacandrei25Chiriac Andrei chiriacandrei25 Data 17 decembrie 2013 22:21:37
Problema Shop Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.15 kb
#include <fstream>
#include <algorithm>
#define Nmax 50

using namespace std;

int N,C,sol[Nmax];
long long L;

struct numar
{
    int poz,cant;
    long long val;
    bool operator < (const numar &A) const
    {
        return val<A.val;
    }
};
numar v[Nmax];

inline long long ExpLog(int x, int put)
{
    long long p=1;
    while(put>0)
    {
        if(put&1)
        {
            p*=x; --put;
        }
        x=x*x; put=(put>>1);
    }
    return p;
}

inline void Read()
{
    int i,exp;
    ifstream fin("shop.in");
    fin>>N>>C>>L;
    for(i=1;i<=N;++i)
    {
        fin>>exp>>v[i].cant;
        v[i].poz=i; v[i].val=ExpLog(C,exp);
    }
    fin.close();
    sort(v+1,v+N+1);
}

inline void Solve()
{
    int i,nr,solutie=0,aux;
    for(i=N;i>0 && L>0;--i)
    {
        aux=L/v[i].val;
        nr=min(v[i].cant, aux);
        sol[v[i].poz]+=nr;
        solutie+=nr;
        L-=nr*v[i].val;
    }
    ofstream fout("shop.out");
    fout<<solutie<<"\n";
    for(i=1;i<=N;++i)
        fout<<sol[i]<<" ";
    fout<<"\n";
    fout.close();
}

int main()
{
    Read();
    Solve();
    return 0;
}