当前位置:网站首页>Greedy problem 01_ Activity arrangement code analysis
Greedy problem 01_ Activity arrangement code analysis
2022-07-25 11:34:00 【Hard CLZ】
One 、 How to express activities ?
- 1、 Starting time
- 2、 End time
- 3、 Activity number
struct action{
int a,b; // start 、 End time
int index; // Activity number
};
action s[1025]; // Store active content
Two 、 Data processing
Such a pile of activities are randomly input and stored in s[1025] in , It needs to be sorted to facilitate processing .
Sorting rules :
Sort by activity end time in ascending order .
bool cmp(const action &x,const action &y){
if(x.b<=y.b)
return true;
return false;
}
sort(s,s+n+1,cmp); // Subscript from 1 Start sorting
Then input data according to the previous case , Activities after sorting :
Activities 1: 1 4Activities 2: 3 5Activities 3: 0 6Activities 4: 5 7Activities 5: 3 8Activities 6: 5 9Activities 7: 6 10Activities 8: 8 11Activities 9: 8 12Activities 10: 2 13Activities 11: 12 14
3、 ... and 、 Core code
Provide the required parameters :
- int n; // Indicates that there are several activities
- action s[]; // Activity list
- bool l[]; // Subgroup array , Used to record which activities were finally selected
void GreedySelector(int n,action s[],bool l[]){
l[1] = true; // The first activity is required
int preEnd = 1; // Used to record “ The latest selected activity number ”
for(int i=2; i<=n; i++){
// Start the cycle from the second activity
if(s[i].a>=s[preEnd].b){
l[i] = true;
preEnd = i;
}
}
}
边栏推荐
- 【高并发】通过源码深度分析线程池中Worker线程的执行流程
- 全网显示 IP 归属地,是怎么实现的?
- 谣言检测文献阅读十一—Preventing rumor spread with deep learning
- Only know that the preform is used to generate objects? See how I use unity to generate UI prefabs
- Definition of information entropy
- [IJCAI 2022] parameter efficient large model sparse training method, which greatly reduces the resources required for sparse training
- shell-第五章作业
- 活动报名 | 玩转 Kubernetes 容器服务提高班正式开营!
- C# Newtonsoft.Json 高级用法
- Leetcode sword finger offer 27. image of binary tree
猜你喜欢

How can you use unity without several plug-ins? Unity various plug-ins and tutorial recommendations

Fillet big killer, use filter to build fillet and wave effect!

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

Game backpack system, "inventory Pro plug-in", research and learning ----- mom doesn't have to worry that I won't make a backpack anymore (unity3d)

SQL注入 Less17(报错注入+子查询)

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

Only know that the preform is used to generate objects? See how I use unity to generate UI prefabs

Shell 脚本参数传递时有 \r 换行符问题

Summary of combination problems of Li Kou brush questions (backtracking)

A troubleshooting record of DirectShow playback problems
随机推荐
用Unity不会几个插件怎么能行?Unity各类插件及教程推荐
小区蔬菜配送的小程序
PostgreSQL stepping on the pit | error: operator does not exist: UUID = character varying
C# Newtonsoft. Jason advanced usage
feign客户端请求之LoadBalancerLifecycle生命周期
syncronized锁升级的过程
工作面试总遇秒杀?看了京东T8大咖私藏的秒杀系统笔记,已献出膝盖
LVS负载均衡之LVS-NAT与LVS-DR模式原理详解
倍增Floyd「建议收藏」
Database design - Simplified dictionary table [easy to understand]
SQL注入 Less17(报错注入+子查询)
Leetcode sword finger offer 28. symmetric binary tree
tensorflow 调用多块GPU的一些错误
Common web attacks and defense
复习背诵整理版
谣言检测文献阅读十一—Preventing rumor spread with deep learning
贪心问题01_活动安排代码分析
SQL language (4)
贪心问题01_活动安排问题
将字符串转换为数字