题目:
Allowance
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4981 | Accepted: 1967 |
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.
* 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
3 6 10 1 1 100 5 120
Sample Output
111
题意:来自vj
作为创纪录的牛奶生产的奖励,农场主约翰决定开始给Bessie奶牛一个小的每周津贴。FJ有一套硬币N种(1≤N≤20)不同的面额,每枚硬币是所有比他小的硬币面值的倍数,例如1美分硬币、5美分硬币、10美分硬币和50美分硬币。使用这些硬币,FJ每周至少给Bessie C(1 <= C <=100000000)美分。请你计算他最多能给Bessie几周
Input
* 第一行N 、 C
* 第 2..N+1行: 硬币价值 V (1 <= V <= 100,000,000) 和数量 B (1 <= B <= 1,000,000)
Output
* 第 2..N+1行: 硬币价值 V (1 <= V <= 100,000,000) 和数量 B (1 <= B <= 1,000,000)
* 使用这些硬币,每周至少给C美分的前提下的最多周数
思路:由于每周至少要给c美分,那么面值大于等于c的硬币由于不能拆分,只好每周一个这样的硬币原样给出。首先我们先把这样的数据剔除。
之后进行贪心,为了使给的周数尽可能多,那贪心目的就是使每周给出的总面值尽可能接近c(当然要大于等于c啦^_^)。于是就有:先用面值大的凑到尽可能的接近c(此时的面值一定要小于等于c),再用面值小的钱使给出的总额达到大于等于c。
(ps:先用大锤搞定,再用小锤扣缝)
代码:
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<set>
#define sd(x) scanf("%d",&x)
#define ss(x) scanf("%s",x)
#define sc(x) scanf("%c",&x)
#define sf(x) scanf("%f",&x)
#define slf(x) scanf("%lf",&x)
#define slld(x) scanf("%lld",&x)
#define me(x,b) memset(x,b,sizeof(x))
#define pd(d) printf("%d\n",d);
#define plld(d) printf("%lld\n",d);
// #define Reast1nPeace
typedef long long ll;
using namespace std;
const int INF = 0x3f3f3f3f;
struct node{
int v,b;
node(int v,int b):v(v),b(b){}
friend bool operator < (node x,node y){
return x.v > y.v;
}
};
int use[25];
vector<node> v;
int main(){
#ifdef Reast1nPeace
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ios::sync_with_stdio(false);
int n,c;
cin>>n>>c;
int ans = 0;
for(int i = 0 ; i<n ; i++){
int val,b;
cin>>val>>b;
if(val >= c){
ans += b;
}
else{
v.push_back(node(val,b));
}
}
sort(v.begin() , v.end());
int len = v.size();
bool flag;
while(1){
int sum = c;
bool flag = 0;
memset(use,0,sizeof(use));
for(int i = 0 ; i<len ; i++){
if(sum>0 && v[i].b>0){
use[i] = min(v[i].b , sum/v[i].v);
sum -= use[i]*v[i].v;
if(sum == 0){
flag = 1;
break;
}
}
}
for(int i = len-1 ; i>=0 ; i--){
if(flag) break;
if(v[i].b > use[i]){
while(v[i].b > use[i]){
sum -= v[i].v;
use[i]++;
if(sum <=0){
flag = 1;
break;
}
}
}
}
if(!flag) break;
int ci = INF;
for(int i = 0 ; i<len ; i++){
if(use[i]){
ci = min(ci , v[i].b/use[i]);
}
}
//算一下当前这一种取硬币的方式可以最多弄几次
ans += ci;
for(int i = 0 ; i<len ; i++){
v[i].b -= ci*use[i];
}
}
cout<<ans<<endl;
return 0;
}