当前位置: 首页 > news >正文

贪心算法[1]

首先用最最最经典的部分背包问题来引入贪心的思想。 

 由题意可知我们需要挑选出价值最大的物品放入背包,价值即单位价值。

我们需要计算出每一堆金币中单位价值。金币的属性涉及两个特征,重量和价值。

所以我们使用结构体。

上代码。

#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
struct Item {int c, w;
};//定义结构体,c代表价值,w代表重量
Item item[1010];//创建结构体变量
bool cmp(Item a, Item b){//定义排序方式return a.w * b.c > b.w * a.c;//单价的转换形式
}//排序函数,说白了就是比性价比
int main() {int N, V;cin >> N >> V;for (int i = 1; i <= N; i++) {cin >> item[i].c >> item[i].w;}sort(item + 1, item + N + 1, cmp);//输入后排序double ans = 0;for(int i=1; i<=N; i++){if(V <= item[i].c){ans += (double)item[i].w / item[i].c * V;//double强转V = 0;break;}else{ans += item[i].w;V -= item[i].c;}}printf("%.2lf", ans);return 0;
}

 第二种写法 

 

class Item {//定义一个类里面包含价值和重量两个参数,以方便创建vector数组
public:int w, v;//变量Item(int w, int v) :w(w), v(v) {//列表初始化}};
double solve(vector<int>& wei, vector<int>& val,int t)
{vector<Item>ans;//声明类型为Item的vector数组,每一个元素包含两个变量for (int i = 0; i < wei.size(); i++){ans.push_back(Item(wei[i], val[i]));//将价值和重量填入创建的Item类型数组}sort(ans.begin(), ans.end(), [](Item& a, Item& b) {return(double)a.v / a.w > (double)b.v / b.w; });//对vector数组进行排序,lamba表达式【】为定义排序的格式,这里也可以定义一个bool函数来实现排序的方式double res = 0;for (auto& items : ans)//遍历{if (items.w <= t)//如果第一堆金币总重量小于背包重量全部放入{res += items.v;t -= items.w;}else {res +=(double)items.v / items.w * t;//将剩余的重量用最大的价值单价填入break;}}return res;
}int main()
{int n, t,w,v;cin >> n >> t;vector<int>wei;//创建重量数组vector<int>val;//创建价值数组for (int i = 0; i < n; i++){cin >> w >> v;wei.push_back(w);val.push_back(v);}double ans = solve(wei, val,t);printf("%.2lf", ans);
}

 这一题选自洛谷p1223题,根据题意我们可以知道要想得到最短的等待时间得先让排队时间少的先接水。下面介绍两种方法进行解决。

由于题目既要有接水时间又要有序号且这两个元素是对应同一个人,所以我们第一种方法使用结构体。

上代码。

#include <iostream>
#include <vector>
#include <algorithm>
#include<cstdio.h>using namespace std;
struct human {int b, num;//输出的两个变量有联系,用结构体
};
bool cmp(human a, human x)//定义比较的方式
{return a.b < x.b;
}
int main()
{struct human ans[1001];int n, i, j;double time = 0;cin >> n;for (int i = 1; i <= n; i++){cin >> ans[i].b;//每个人的时间ans[i].num = i;//每个人对应的序号}sort(ans + 1, ans + n + 1, cmp);for (int i = 1; i <= n; i++){cout << ans[i].num << " ";}cout << endl;for (j = n - 1; j >= 1; j--){i = n - j;//此时的总人数time += ans[i].b * j;//当前人的等待时间要乘以此时的总人数}printf("%.2lf",time);return 0;
}

第二种方法,不使用结构体,使用vector+pair。

#include <iostream>//洛谷p1223
#include <vector>
#include <algorithm>using namespace std;
int main() {int n;double sum = 0;cin >> n;vector<pair<int, int>> a(n);//既要记录每个人的序号也要记录每个人的时间,定义vector的元素类型为pairfor (int i = 0; i < n; i++) {cin >> a[i].first;//第一个为时间,调用每一个为pair类型元素的firsta[i].second = i + 1;//第二个为序号,调用每一个为pair类型元素的second}sort(a.begin(), a.end());//排序,升序for (int i = 0; i < n; i++) {sum += a[i].first * (n - i - 1);//先排上的人后面所有人都要等待cout << a[i].second << " ";}printf("\n%.2f", sum / n);return 0;
}

 

http://www.lryc.cn/news/356451.html

相关文章:

  • 卢文岩博士受邀参与中国科学院大学校友论坛 解码DPU核心价值
  • 2024年上半年软件设计师试题及答案(回忆版)
  • QGIS使用python代码导出给定坐标图片
  • 看花眼,眼花缭乱的主食冻干到底应该怎么选?靠谱的主食冻干分享
  • 开源VS闭源:谁更能推动AI技术的普及与发展?
  • 前端面试题日常练-day28 【面试题】
  • 好消息!DolphinScheduler官网集成LLM模型问答AI kapa.ai
  • 【软考】下篇 第19章 大数据架构设计理论与实践
  • 创新指南|降低 TikTok CPA 的 9 项专家策略
  • jmeter服务器性能监控分析工具ServerAgent教程
  • 工作纪实50-Idea下载项目乱码
  • 37. 解数独 - 力扣(LeetCode)
  • 使用uniapp编写的微信小程序进行分包
  • 设计模式19——观察者模式
  • C++算术运算和自增自减运算
  • Python深度学习:【模型系列】一文搞懂Transformer架构的三种注意力机制
  • 微服务架构中Java的应用
  • 【强训笔记】day25
  • 知识产权与标准化
  • 【LeetCode:2769. 找出最大的可达成数字 + 模拟】
  • 编程5年的老哥说:我代码里从来不用锁,谁爱...
  • CogAgent:开创性的VLM在GUI理解和自动化任务中的突破
  • C++容器之位集(std::bitset)
  • 《Ai学习笔记》自然语言处理 (Natural Language Processing):常见机器阅读理解模型(上)02
  • 老师如何在线发布期末考试成绩查询?
  • TensorBoard相关学习
  • 敏感数据处理的艺术:安全高效的数据提取实践与挑战
  • 使用Python操作excel单元格——获取带公式单元格的值
  • PHP开发入门
  • HBase分布式数据库入门到精通