当前位置:网站首页>贪心问题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;
}
}
}
边栏推荐
- MLX90640 红外热成像仪测温模块开发笔记(五)
- Understanding: idea uses Scala to write wordcount programs and generate jar packages
- [recursion] 938. Range and of binary search tree
- Some errors of tensorflow calling multiple GPUs
- Several common PCB surface treatment technologies!
- The most detailed MySQL index analysis (mind map is attached at the end of the article)
- Getting started with tensorflow
- BGP federal experiment
- MySQL advanced statement (I) (there is always someone who will make your life no longer bad)
- Learn NLP with Transformer (Chapter 1)
猜你喜欢

Nowcodertop12-16 - continuous updating

Smart cloud IOT platform STM32 esp8266-01s simple wireless light control

The most detailed MySQL index analysis (mind map is attached at the end of the article)

SQL语言(一)

如何判断静态代码质量分析工具的性能?这五大因素必须考虑

NowCoderTOP7-11——持续更新ing

HCIA experiment (06)

只知道预制体是用来生成物体的?看我如何使用Unity生成UI预制体

What is MySQL transaction

Want to record your supernatural moments when playing games? Let's take a look at how to use unity screenshots
随机推荐
学习路之PHP--TP5.0使用中文当别名,报“不支持的数据表达式”
The B2B2C multi merchant system has rich functions and is very easy to open!!!
Hcip experiment (02)
Learn NLP with Transformer (Chapter 1)
tensorflow 调用多块GPU的一些错误
一篇看懂:IDEA 使用scala 编写wordcount程序 并生成jar包 实测
[树] 100. 相同的树
HCIP (01)
全网显示 IP 归属地,是怎么实现的?
STM32CubeMX学习记录--安装配置与使用
ESP8266 使用 DRV8833驱动板驱动N20电机
Why should the hashcode () method be rewritten when rewriting the equals () method
Redis 入门
MySQL master-slave replication and read-write separation
Learn PHP -- phpstudy tips mysqld Exe: Error While Setting Value ‘NO_ ENGINE_ Solution of substitution error
MySQL | GROUP_CONCAT函数,将某一列的值用逗号拼接
The most detailed MySQL index analysis (mind map is attached at the end of the article)
PostgreSQL踩坑 | ERROR: operator does not exist: uuid = character varying
Learn NLP with Transformer (Chapter 5)
Motivation of enterprises to practice open source