当前位置:网站首页>贪心问题01_活动安排代码分析
贪心问题01_活动安排代码分析
2022-07-25 10:35:00 【努力的clz】
本博客对应的代码内容:https://blog.csdn.net/qq_43468008/article/details/105927468
一、怎么表示活动?
- 1、起始时间
- 2、结束时间
- 3、活动编号
struct action{
int a,b; // 起始、结束时间
int index; // 活动编号
};
action s[1025]; // 存储活动内容
二、数据处理
这么一堆活动随机输入并存储到 s[1025] 中,需要对其排序方便处理。
排序规律:
按活动的结束时间升序排序。
bool cmp(const action &x,const action &y){
if(x.b<=y.b)
return true;
return false;
}
sort(s,s+n+1,cmp); // 下标从1开始排序
那么按照之前给的案例输入数据,排序过后的活动:
活动1: 1 4活动2: 3 5活动3: 0 6活动4: 5 7活动5: 3 8活动6: 5 9活动7: 6 10活动8: 8 11活动9: 8 12活动10: 2 13活动11: 12 14
三、核心代码
需要提供的参数:
- int n; // 表示有几个活动
- action s[]; // 活动列表
- bool l[]; // 辅组数组,用来记录哪些活动被最后选中
void GreedySelector(int n,action s[],bool l[]){
l[1] = true; // 第一个活动时必选的
int preEnd = 1; // 用来记录“最新选中的活动编号”
for(int i=2; i<=n; i++){
// 从第二个活动开始循环
if(s[i].a>=s[preEnd].b){
l[i] = true;
preEnd = i;
}
}
}
边栏推荐
- NowCoderTOP12-16——持续更新ing
- Stm32cubemx learning record -- installation, configuration and use
- Want to record your supernatural moments when playing games? Let's take a look at how to use unity screenshots
- [flask advanced] deeply understand the application context and request context of flask from the source code
- 如何判断静态代码质量分析工具的性能?这五大因素必须考虑
- Shell - Chapter 8 exercise
- Learn NLP with Transformer (Chapter 6)
- shell- 第七章练习
- JS convert pseudo array to array
- Several common PCB surface treatment technologies!
猜你喜欢
随机推荐
Shell fourth day homework
只知道预制体是用来生成物体的?看我如何使用Unity生成UI预制体
Dataframe print 省略号问题
一篇看懂:IDEA 使用scala 编写wordcount程序 并生成jar包 实测
【IJCAI 2022】参数高效的大模型稀疏训练方法,大幅减少稀疏训练所需资源
STM32CubeMX学习记录--安装配置与使用
JS 将伪数组转换成数组
Nowcodertop7-11 - continuous updating
[flask advanced] solve the classic error reporting of flask by combining the source code: working outside of application context
txt转csv文件,隔行出现空行
leetcode 剑指 Offer 28. 对称的二叉树
HCIA experiment (06)
The most detailed MySQL index analysis (mind map is attached at the end of the article)
shell- 第七章练习
[动态规划] 70. 爬楼梯
[IJCAI 2022] parameter efficient large model sparse training method, which greatly reduces the resources required for sparse training
Redis之压缩列表ziplist
为什么重写equals()方法必须要重写hashCode()方法
How can you use unity without several plug-ins? Unity various plug-ins and tutorial recommendations
Details of the list of state products that Apple announced to be eligible for the sales tax holiday in the United States









