POJ3040 Allowance

POJ3040 Allowance

Description

As a reward for record milk production, Farmer John has decided to start paying Bessie the cow a small weekly allowance. FJ has a set of coins in N (1 <= N <= 20) different denominations, where each denomination of coin evenly divides the next-larger denomination (e.g., 1 cent coins, 5 cent coins, 10 cent coins, and 50 cent coins).Using the given set of coins, he would like to pay Bessie at least some given amount of money C (1 <= C <= 100,000,000) every week.Please help him ompute the maximum number of weeks he can pay Bessie.

Input

* Line 1: Two space-separated integers: N and C

* Lines 2..N+1: Each line corresponds to a denomination of coin and contains two integers: the value V (1 <= V <= 100,000,000) of the denomination, and the number of coins B (1 <= B <= 1,000,000) of this denomation in Farmer John's possession.

Output

* Line 1: A single integer that is the number of weeks Farmer John can pay Bessie at least C allowance

Sample Input

1
2
3
4
3 6
10 1
1 100
5 120

Sample Output

1
111

Hint

INPUT DETAILS: FJ would like to pay Bessie 6 cents per week. He has 100 1-cent coins,120 5-cent coins, and 1 10-cent coin.

OUTPUT DETAILS: FJ can overpay Bessie with the one 10-cent coin for 1 week, then pay Bessie two 5-cent coins for 10 weeks and then pay Bessie one 1-cent coin and one 5-cent coin for 100 weeks.

Answer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <cstdio>
#include <utility>
#include <algorithm>
using namespace std;

typedef pair<int, int> P;
P a[22];
int use[22];

int main() {
int n, c;
scanf("%d %d", &n, &c);
for (int i = 0; i < n; ++i) {
int x, y;
scanf("%d %d", &a[i].first, &a[i].second);
}
sort(a, a+n);
int ans = 0;
for (int i = 0; i < n; ++i) {
if (a[i].first >= c) {
ans += a[i].second;
a[i].second = 0;
}
}

while (true) {
int flag = 0;
int tmp = c;

fill(use, use+22, 0);
for (int i = n - 1; i >= 0; --i) {
if(a[i].second) {
int mi = min(a[i].second, tmp / a[i].firs);
tmp -= mi * a[i].first;
use[i] = mi;
if (tmp <= 0) {
flag = 1;
break;
}
}
}

if (tmp > 0) {
for (int i = 0; i < n; ++i) {
if (a[i].second > use[i]) {
while (use[i] < a[i].second) {
tmp -= a[i].first;
++use[i];
if (tmp <= 0) {
flag = 1;
break;
}
}
if (tmp <= 0)
break;
}
}
}

if (!flag) break;
int mx = 1000000;
for (int i = 0; i < n; ++i) {
if (use[i])
mx = min(mx, a[i].second / use[i]);
}
ans += mx;
for (int i = 0; i < n; ++i) {
if (use[i])
a[i].second -= mx * use[i];
}
}
printf("%d\n", ans);
}

Analysis

  1. 题意就是你有很多张面额不同的纸币,你每个星期要给奶牛至少c元,问你用现在的钱最多给奶牛多少周。
  2. 首先,将大于等于c的面额的钱直接每个星期给奶牛一张,将面额大于等于c的钱去除。
  3. 然后从大到小开始选择,要选择的金额尽可能的接近c,如果刚好能够凑足c就作为当前的一种方案,如果不能凑足c那就再从小的开始选,要选出一种的总额不少于c但尽量接近c作为当前的方案。
  4. 然后计算如果按照如此方案最多可以给奶牛多少周,然后按照相同的方法再选方案,一直选到选择的金额不能凑足c为止。
  5. .https://blog.csdn.net/yopilipala/article/details/79607185
# POJ

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×