第九天(2):贪心算法

T1

FatMouse’ Trade
时间限制:1 秒
内存限制:128 兆
特殊判题:否

题目描述: FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean.
The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain.

输入: The input consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then N lines follow, each contains two non-negative integers J[i] and F[i] respectively. The last test case is followed by two -1’s. All integers are not greater than 1000.

输出: For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain.

样例输入:
5 3
7 2
4 3
5 2
20 3
25 18
24 15
15 10
-1 -1

样例输出:
13.333
31.500

//
//  main.cpp
//  FatMouseTrade
//
//  Created by Apple on 2019/8/13.
//  Copyright © 2019 Apple_Lance. All rights reserved.
//

#include <iostream>
#include <stdio.h>
#include <algorithm>

using namespace std;

struct goods{
    double j;
    double f;
    double s;
    
    bool operator < (const goods & A)const{
        return s > A.s;
    }
}buf[1000];

int main(int argc, const char * argv[]) {
    // insert code here...
    double m;
    int n;
    while (scanf("%lf %d", &m, &n) != EOF) {
        if(m == -1 && n == -1)
            break;
        for(int i = 0; i < n; i++){
            scanf("%lf%lf", &buf[i].j, &buf[i].f);
            buf[i].s = buf[i].j / buf[i].f;
        }
        sort(buf, buf + n);
        int idx = 0;
        double ans = 0;
        while(m > 0 && idx < n){
            if(m > buf[idx].f){
                ans += buf[idx].j;
                m -= buf[idx].f;
            }
            else{
                ans += buf[idx].j * m / buf[idx].f;
                m = 0;
            }
            idx ++;
            
        }
        printf("%.3lf\n", ans);
    }
    return 0;
}

T2

今年暑假不 AC
时间限制:1 秒
内存限制:128 兆
特殊判题:否

题目描述: “ 今 年 暑 假 不 AC ? ”“ 是 的 。 ”“ 那 你 干 什 么 呢 ? ”“ 看 世 界 杯 呀 , 笨蛋! ”“@#$%^&*%…”确实如此, 世界杯来了, 球迷的节日也来了, 估计很多ACMer 也会抛开电脑,奔向电视作为球迷,一定想看尽量多的完整的比赛,当然,作为新时代的好青年,你一定还会看一些其它的节目,比如新闻联播(永远不要忘记关心国家大事) 、非常 6+7、超级女生,以及王小丫的《开心辞典》等等,假设你已经知道了所有你喜欢看的电视节目的转播时间表, 你会合理安排吗? (目标是能看尽量多的完整节目)

输入: 输入数据包含多个测试实例,每个测试实例的第一行只有一个整数n(n<=100),表示你喜欢看的节目的总数,然后是 n 行数据,每行包括两个数据Ti_s,Ti_e (1<=i<=n),分别表示第 i 个节目的开始和结束时间,为了简化问题,每个时间都用一个正整数表示。n=0 表示输入结束,不做处理。
输出: 对于每个测试实例, 输出能完整看到的电视节目的个数, 每个测试实例的输出占一行。
样例输入:
12
1 3
3 4
0 7
3 8
15 19
15 20
10 15
8 18
6 12
5 10
4 14
2 9
0

样例输出:
5

//
//  main.cpp
//  Summer_AC
//
//  Created by Apple on 2019/8/13.
//  Copyright © 2019 Apple_Lance. All rights reserved.
//

#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
struct program{
    int startTime;
    int endTime;
    bool operator < (const program & A)const{
        return endTime < A.endTime;
    }
}buf[1000];

int main(int argc, const char * argv[]) {
    // insert code here...
    int n;
    while(scanf("%d", &n) != EOF){
        if(n == 0)
            break;
        for(int i = 0;i<n;i++)
            scanf("%d%d", &buf[i].startTime, &buf[i].endTime);
        sort(buf, buf+n);
        int currentTime = 0, ans = 0;
        for(int i = 0;i<n;i++)
            if(currentTime <= buf[i].startTime){
                currentTime = buf[i].endTime;
                ans++;
            }
        printf("%d\n", ans);
    }
    return 0;
}
### 贪心算法思想及其应用 贪心算法是一种在每一步选择中都采取当前状态下最优策略的算法设计方法,其目标是希望最终能够达到全局最优解。然而需要注意的是,贪心算法并不总是能保证得到真正的全局最优解[^3]。 #### 贪心算法的核心特点 - **局部最优决策**:每次迭代过程中只考虑当下最有利的选择。 - **不可回溯性**:一旦做出某个决定,则不会更改该决定的结果。 下面是一个经典的活动安排问题作为例子来展示如何运用贪心算法解决实际问题: 假设我们有若干个需要调度的任务列表,每个任务都有自己的起始时间和完成时间。我们的目的是尽可能多地安排这些不重叠的任务。 ```python def activity_selection(s, f): n = len(f) i = 0 result = [] # 将第一个活动加入结果集中 result.append(i) # 遍历剩余的所有活动 for j in range(1, n): if s[j] >= f[i]: result.append(j) i = j return result # 示例数据 s = [1 , 3 , 0 , 5 , 8 , 5] f = [2 , 4 , 6 , 7 , 9 , 9] selected_activities = activity_selection(s, f) print("Selected activities indices:", selected_activities) ``` 上述代码实现了基于结束时间排序后的活动选取过程,确保所选中的每一个新活动都不会与其他已选定的活动发生冲突。 ### 第二章 算法设计与分析 作业解析 对于第二章关于算法设计与分析部分涉及的习题解答,通常会围绕以下几个方面展开讨论: 1. 明确题目要求; 2. 判断是否适合采用某种特定类型的算法(如动态规划、分治或者本节提到的贪心算法); 3. 编写伪代码并逐步优化成正式实现版本; 4. 对复杂度进行理论推导验证效率高低; 具体到某一道具体的练习题上时,可以根据实际情况调整思路方向。比如针对最大连续子序列求和这类经典问题也可以尝试利用类似的思维模式去探索解决方案。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值