Cod sursa(job #2136127)

Utilizator MateiAruxandeiMateiStefan MateiAruxandei Data 19 februarie 2018 17:55:49
Problema Shop Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.3 kb
#include <fstream>
#include <algorithm>

using namespace std;

ifstream fin("shop.in");
ofstream fout("shop.out");

struct shop{
    int ai, bi, tip;
}v[35];

inline bool cmp(shop x, shop y){
    return x.ai > y.ai;
}

long long putere[35];
int cnt[35];

void precalculare(long long baza, long long l){
    short i = 2;
    putere[0] = 1;
    putere[1] = baza;
    while(1){
        putere[i] = putere[i - 1] * baza;
        if(putere[i] >= l)
            break;
        ++i;
    }
}

int main()
{
    long long n, c, l;
    fin >> n >> c >> l;

    for(int i = 1; i <= n; ++i){
        fin >> v[i].ai >> v[i].bi;
        v[i].tip = i;
    }

    sort(v + 1, v + n + 1, cmp);

    precalculare(c, l);

    int sum = 0;
    for(int i = 1; i <= n; ++i){
        if(putere[v[i].ai]){
            long long times = l / putere[v[i].ai];
            if(times >= v[i].bi){
                l -= (putere[v[i].ai] * v[i].bi);
                cnt[v[i].tip] = v[i].bi;
            }
            else {
                l -= (putere[v[i].ai] * times);
                cnt[v[i].tip] = times;
            }
            sum += cnt[v[i].tip];
        }
    }

    fout << sum << '\n';
    for(int i = 1; i <= n; ++i)
        fout << cnt[i] << ' ';
    fout << '\n';
    return 0;
}