贪心算法[1]

embedded/2024/10/19 5:27:56/

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

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

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

所以我们使用结构体。

上代码。

#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.ppmy.cn/embedded/43674.html

相关文章

905. 按奇偶排序数组 - 力扣

1. 题目 给你一个整数数组 nums&#xff0c;将 nums 中的的所有偶数元素移动到数组的前面&#xff0c;后跟所有奇数元素。 返回满足此条件的 任一数组 作为答案。 2. 示例 3. 分析 开辟一个数组res用来保存操作过后的元素。第一次遍历数组只插入偶数&#xff0c;第二次遍历数组…

flutter性能优化-UI重绘

在Flutter中&#xff0c;可以采取以下几种方法来减少UI的重绘&#xff1a; 使用setState()方法进行批量更新&#xff1a;当需要更新多个UI元素时&#xff0c;可以使用setState()方法进行批量更新&#xff0c;而不是在每次更新时都调用setState()。这样可以减少重绘的次数&#…

有哪些藏文翻译器在线翻译?工具分享

有哪些藏文翻译器在线翻译&#xff1f;随着全球化的推进&#xff0c;语言之间的交流变得越来越重要。藏语作为中华民族的重要语言之一&#xff0c;其翻译需求也日益增加。为了满足这一需求&#xff0c;市场上涌现出了多款藏文翻译器在线翻译工具&#xff0c;它们以其高效、准确…

Ingress controller:Kubernetes 的瑞士军刀

原文作者&#xff1a;Brian Ehlert of F5 原文链接&#xff1a;Ingress controller&#xff1a;Kubernetes 的瑞士军刀 转载来源&#xff1a;NGINX 中文官网 NGINX 唯一中文官方社区 &#xff0c;尽在 nginx.org.cn 许多人认为 Ingress controller&#xff08;Ingress 控制器&…

SpringBoot 微服务中怎么获取用户信息 token

SpringBoot 微服务中怎么获取用户信息 token 当我们写了一个A接口&#xff0c;这个接口需要调用B接口&#xff0c;但是B接口需要包含请求头内容&#xff0c;比如需要用户信息、用户id等内容&#xff0c;由于不在同一个线程中&#xff0c;使用ThreadLocal去获取数据是无法获取的…

Git常用命令操作

删除git记录,删除项目根目录下的隐藏文件夹.git/ rm -rf .git 初始化仓库 git init 添加到缓冲区 git add controller/* 提交缓冲区文件 git commit -m "新增接口" 拉取远程仓库的 master分支最新代码&#xff0c;并合并到当前所在的分支中 git pull origin…

智慧仓储新动力:EasyCVR+AI视频智能监管系统方案助力仓储安全高效管理

一、背景 随着物流行业的快速发展和智能化水平的提升&#xff0c;智慧仓储视频智能监管系统已成为现代仓储管理的重要组成部分。本系统通过综合运用物联网、视频分析、边缘计算等技术手段&#xff0c;实现对仓储环境的全面监控、智能分析和高效管理。 TSINGSEE青犀视频汇聚Ea…

STM32 定时器问题

stm32通用定时器中断问题 STM32 定时器有时一开启就进中断的问题 ///