hdu4091(贪心枚举)

先考虑性价,假设宝石1性价比高于宝石2,则宝石2最多不可能拿超过lcm(s1, s2) / s2个

枚举性价比较低的宝石拿多少个即可

 1 #include <bits/stdc++.h>
 2
 3 long long n, s1, s2, v1, v2;
 4
 5 long long gcd(long long a, long long b)
 6 {
 7     return b ? gcd(b, a%b) : a;
 8 }
 9
10 int main()
11 {
12     int T_T, t_t;
13     scanf("%d", &T_T);
14     for (t_t = 1; t_t <= T_T; ++t_t) {
15         scanf("%I64d%I64d%I64d%I64d%I64d", &n, &s1, &v1, &s2, &v2);
16         if (v1*s2 < v2*s1) {
17             std::swap(v1, v2);
18             std::swap(s1, s2);
19         }
20         long long max_cnt = std::min(n/s2, s1/gcd(s1, s2));
21         long long ans = 0;
22         for (long long i = 0; i <= max_cnt; ++i)
23             ans = std::max(ans, i * v2 + (n-i*s2)/s1*v1);
24         printf("Case #%d: %I64d\n", t_t, ans);
25     }
26 }
时间: 2024-11-03 21:41:10

hdu4091(贪心枚举)的相关文章

POJ 2965-The Pilots Brothers&#39; refrigerator(贪心+枚举)

The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19464   Accepted: 7462   Special Judge Description The game "The Pilots Brothers: following the stripy elephant" has a quest where a player needs to o

HDU 5303 Delicious Apples (贪心 枚举 好题)

Delicious Apples Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) Total Submission(s): 199    Accepted Submission(s): 54 Problem Description There are n apple trees planted along a cyclic road, which is L metres l

贪心+枚举/哈希表 HDOJ Trouble

题目传送门 题意:5个集合,每个集合最多200个数字,问是否每个集合挑一个数加起来和为0. 分析:显然n^5的程序果断超时,甚至n^3logn的二分也过不了.想n^3的方法,既然判断有没有,那么可以将两个两个的集合合并成两个大集合,再枚举最后一个集合,两个大集合排完序之后一个指针从开头最小开始,另一个从最大的开始,>0,大指针往左移,<0,小指针往右移,那么可以在线性时间求解,这贪心方法很巧妙! 另一种做法算是暴力+优化了,哈希表储存一个两个集合合并后大集合的数字,n^3暴力询问是否哈希表内存

POJ2718 Smallest Difference 【贪心+枚举】

Smallest Difference Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4639   Accepted: 1290 Description Given a number of distinct decimal digits, you can form one integer by choosing a non-empty subset of these digits and writing them in

HDU 5303 Delicious Apples (2015多校第二场 贪心 + 枚举)

Delicious Apples Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) Total Submission(s): 321    Accepted Submission(s): 95 Problem Description There are  apple trees planted along a cyclic road, which is  metres lon

hdu 4091 Zombie’s Treasure Chest 贪心+枚举

转自:http://blog.csdn.net/a601025382s/article/details/12308193 题意: 输入背包体积n,绿宝石体积s1,价值v1,蓝宝石体积s2,价值v2,宝石数目无限,问背包里能放下的最大价值? 题解: 看过去很像完全背包,可数据很大(虽然没给出,也能猜到,不然太水了),所以不能用背包求.又只有两种物品,想到了贪心,将价值与体积比大(称为价值比)的优先放入.但体积限制,这样还不可以,还需要枚举减少价值比大的宝石个数,是否可以增大所求价值.又我们可以知道

Delicious Apples (hdu 5303 贪心+枚举)

Delicious Apples Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) Total Submission(s): 395    Accepted Submission(s): 122 Problem Description There are n apple trees planted along a cyclic road, which is L metres

hihoCoder 1051补提交卡(贪心 枚举)

http://hihocoder.com/problemset/problem/1051 既然要选择最长连续提交天数,那么提交卡必须连续使用才有可能得到最优解,这样贪心,然后从头到尾扫一遍求出最大值. 5 1 数组为a[i] 下标从1开始. 34 77 82 83 84 假如 提交卡用在 第一个数那么连续提交天数变成 a[2]-a[0]-1,第二个数 a[3]-a[1]-1,以此类推. 1 #include<cstdio> 2 #include<cstring> 3 int a[1

poj 2010 Moo University - Financial Aid(优先队列(最小堆)+ 贪心 + 枚举)

Description Bessie noted that although humans have many universities they can attend, cows have none. To remedy this problem, she and her fellow cows formed a new university called The University of Wisconsin-Farmside,"Moo U" for short. Not wish