ACM Buying Hay 题目AC源代码

ACM Buying Hay 题目AC源代码
Buying Hay
时间限制(普通/Java):1000MS/10000MS 运行内存限制:65536KByte
总提交:39 测试通过:5
描述
Farmer John is running out of supplies and needs to purchase H (1 ≤ H ≤ 50,000) pounds of hay for his cows.
He knows N (1 ≤ N ≤ 100) hay suppliers conveniently numbered 1..N.Supplier i sells packages that contain P_i (1 ≤ P_i ≤ 5,000) pounds of hay at a cost of C_i (1 ≤ C_i ≤ 5,000) dollars.Each supplier has an unlimited number of packages available,and the packages must be bought whole.
Help FJ by finding the minimum cost necessary to purchase at least H pounds of hay.
输入
* Line 1:Two space-separated integers:N and H
* Lines 2..N+1:Line i+1 contains two space-separated integers:P_i and C_i
输出
* Line 1:A single integer representing the minimum cost FJ needs to pay to obtain at least H pounds of hay.
样例输入
2 15
3 2
5 3
样例输出
9
提示
FJ can buy three packages from the second supplier for a total cost of 9.
个人是通过性价比排序,选出花费最小的方式,但错了!
辰巽 1年前 已收到2个回答 举报

wxm1984 花朵

共回答了20个问题采纳率:95% 举报

不对,可以举出反例,贪心是错误的哈
正确的方法是动态规划
#include
#include
#include
using namespace std;
const int int_max=2139062144;
const int top=100001;
const int max_size=101;
int opt[top+1];
int n,h;
int w[max_size],cost[max_size];
void init()
{
memset(opt,127,sizeof(opt));
opt[0]=0;
int i;
for (i=1;i

1年前

6

665500 幼苗

共回答了1个问题 举报

这题不能用贪心,要用动态规划

1年前

2
可能相似的问题
Copyright © 2024 YULUCN.COM - 雨露学习互助 - 17 q. 0.026 s. - webmaster@yulucn.com