Cod sursa(job #1060391)

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

using namespace std;

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

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

inline long long ExpLog(long long x, int put)
{
    long long p=1;
    while(put>0)
    {
        if(put&1)
        {
            p=1LL*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;
    long long solutie=0,nr,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;
}