机试指南第二章-经典入门-贪心例题自解

例2.11 FatMouse‘s Trade

解题思路

贪心策略。每次都买剩余物品中性价比(即重量价格比)最高的物品,直到该物品被买完或者钱耗尽。若该物品已经被买完,则我们继续在剩余的物品中寻找性价比最高的物品

AC代码

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

struct Thing
{
    double j;
    double f;
    double s;//性价比
}t[1000];

bool cmp(Thing a, Thing b)
{
    return a.s > b.s;
}

int main()
{
    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", &t[i].j, &t[i].f);
            t[i].s = t[i].j / t[i].f;
        }
        sort(t, t + n, cmp);
        int id = 0;
        double ans = 0;
        while (m > 0 && id < n)
        {
            if (m > t[id].f)
            {
                ans += t[id].j;
                m -= t[id].f;
            }
            else
            {
                ans += t[id].j*m / t[id].f;
                m = 0;
            }
            id++;
        }
        printf("%.3lf\n", ans);
    }
    //system("pause");
    return 0;
}

例2.12 今年暑假不AC

解题思路 

在选择第x(x>=1)个节目时, 一定是选择在收看完前x-1个节目后,其它所有可以收看节目中结束时间最早的 节目,这就是我们要找的贪心策略。在每次选择节目时,都不断的利用这种贪心策略,我们就能完成最优解的求解。

AC代码

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

struct Thing
{
    int beg;
    int end;
}t[100];

bool cmp(Thing a, Thing b)
{
    return a.end < b.end;
}

int main()
{
    int n;
    while (scanf("%d", &n) != EOF)
    {
        if (n == 0)break;
        for (int i = 0; i < n; i++)scanf("%d%d", &t[i].beg, &t[i].end);
        sort(t, t + n, cmp);
        int cur = 0, ans = 0;//当前时间和节目总数
        for (int i = 0; i < n; i++)
        {
            if (cur <= t[i].beg)
            {
                cur = t[i].end;
                ans++;
            }
        }
        printf("%d\n", ans);
    }
    //system("pause");
    return 0;
}

原文地址:https://www.cnblogs.com/yun-an/p/11129460.html

时间: 2024-07-29 04:47:55

机试指南第二章-经典入门-贪心例题自解的相关文章

机试指南第二章-经典入门-查找例题自解

查找: 对于查找问题,有难有易.可能只是直接地对某个数字的查找,也可能涉及搜索等相对难度更大的算法.这里先介绍查找的基础概念和方法. 例 2.9 找 x AC代码: #include<cstring> #include<iostream> using namespace std; int num[205]; int main() { int n, m, x; memset(num, 0, sizeof(num)); while (cin >> n) { bool fla

机试指南第二章-经典入门-Hash的应用自解

Hash的应用: Hash即散列,不像数据结构与算法中讲的各种Hash方法和冲突处理等过多的阐述,以下主要介绍Hash在机试试题解答中的作用. 例2.5 统计同成绩学生人数 Hash解法AC代码:(一般想到的也是这种解法) #include<cstring> #include<iostream> using namespace std; int grade[105]; int main() { int n, m, index; memset(grade, 0, sizeof(gra

计算机考研机试指南(六) ——栈

机试指南 cha 3 栈的应用 括号匹配问题 1 #include <iostream> 2 #include <stdio.h> 3 #include <algorithm> 4 #include <queue> 5 #include <stack> 6 #include <math.h> 7 #include <string> 8 #include <string.h> 9 #include <std

第二章 算法入门 合并排序

在第二章中难的算法不多,接下来我会把稍微复杂一点的算法整理一下 #include <iostream> using namespace std; void mergeSort(int *A,int left,int mid,int right) { int *L=new int[mid-left+1]; int *R=new int[right-mid+1]; int i,j; for(i=0;i<mid-left+1;i++) { L[i]=A[left+i]; } for (j=0;

Hadoop权威指南---第二章MaxTemperature例题源码

敲了一下hadoop权威指南第二章的例题,虽然基本上是照着书上敲的,但还是把它放到这方便以后查看. 代码如下: <span style="font-size:18px;"><span style="font-size:18px;">import java.io.IOException; import java.util.StringTokenizer; import org.apache.hadoop.conf.Configuration;

算法导论(Introduction to Algorithms )— 第二章 算法入门 — 2.1 插入排序

一.插入排序:INSERTION-SORT 1.适用范围: which is an efficient algorithm for sorting a small number of elements. 对于少量元素的排序,插入排序是一种高效的算法. 2.原理: Insertion sort works the way many people sort a hand of playing cards. We start with an empty left hand and the cards

第二章Python入门

第二章 Python入门 2.1.简介 Python是著名的"龟叔"(Guido van Rossum)在1989年圣诞节期间,为了打发无聊的圣诞节而编写的一个编程语言 Python的哲学就是简单优雅,尽量写容易看明白的代码,尽量写少的代码.为我们提供了非常完善的基础代码库,覆盖了网络.文件.GUI.数据库.文本等大量内容, 2.1.1.Python适合开发哪些类型的应用呢? 云计算 机器学习 科学运算 自动化运维 自动化测试 爬虫 数据分析 GUI图形化 Web开发等 2.1.2.P

Kali Linux 无线渗透测试入门指南 第二章 WLAN 和固有的不安全性

第二章 WLAN 和固有的不安全性 作者:Vivek Ramachandran, Cameron Buchanan 译者:飞龙 协议:CC BY-NC-SA 4.0 简介 建筑越高,地基就要打得越深. – 托马斯·坎佩斯 没有什么伟大的东西能在脆弱的基础上构建.在我们的语境中,固有的不安全性之上不能构建出安全. WLAN 在设计上拥有特定的不安全性,它们可被轻易利用,例如,通过封包注入,以及嗅探(能够在很远处进行).我们会在这一章利用这些缺陷. 2.1 回顾 WLAN 帧 由于这本书处理无线方面

高可用高性能负载均衡软件HAproxy详解指南-第二章(配置文件、关键字、ACL)

第二章:HAproxy配置文件详解以及HAproxy的ACL详解 对Linux有兴趣的朋友加入QQ群:476794643 在线交流 本文防盗链:http://zhang789.blog.51cto.com 上一篇:第一章:HAproxy简介及安装配置 目录 haproxy 配置文件详解 haproxy 配置文件中的关键字参考 haproxy的ACL 附:一份完整的HAproxy的配置文件 由于字体过多分开写的,全系列文章链接 第一章:HAproxy简介及安装配置 http://zhang789.