Pagini recente » Cod sursa (job #1504763) | Cod sursa (job #1488613) | Cod sursa (job #1952322) | Cod sursa (job #1442827) | Cod sursa (job #1425942)
#include <fstream>
#include <cmath>
#include <algorithm>
using namespace std;
ifstream f("shop.in");
ofstream g("shop.out");
const int nmax=35;
struct coin{long long int value,type,amount;};
coin a[nmax];
long long int n,i,j,base,exponent,used[nmax],sum,camm,totalcoins;
// we apply the greedy method , sorting coins after decresingly after their value
// this can be done because of power's "anti-triangle" property , which states :
// "if there are 3 sticks , of lenght x^a , x^b , x^c (x,a,b,c being natural numbers , with a , b and c being different from 0 and 1) all of the following are correct : a+b<=c , a+c<=b , b+c<=a"
bool cmp (coin A , coin B)
{
return A.value>B.value;
}
int main()
{
f>>n>>base>>sum;
for (i=1;i<=n;i++)
{
f>>exponent>>camm;
a[i].type=i;
a[i].value=pow(base,exponent);
a[i].amount=camm;
}
sort(a+1,a+n+1,cmp);
i=1;
while (sum && i<=n)
{
while (sum>=a[i].value && a[i].amount>0)
{
used[a[i].type]++;
sum-=a[i].value;
a[i].amount--;
}
i++;
}
for (i=1;i<=n;i++)
totalcoins+=used[i];
g<<totalcoins<<'\n';
for (i=1;i<=n;i++)
g<<used[i]<<" ";
return 0;
}