当前位置:网站首页>贪心问题01_活动安排问题
贪心问题01_活动安排问题
2022-07-25 10:35:00 【努力的clz】
- 题目描述: 有n个活动的集合E={1,2, ……,n},每个活动i都有一个起始时间a(i)和结束时间b(i),并且a(i)<=b(i)。
如果选择活动i,则它在半开时间区间[ai,bi)内占用舞台;若区间[ai,bi)与区间[aj,bj)不相交,则称活动i与活动j是相容的。
当 ( ai>=bj ) 或 ( aj>=bi )时,活动i与活动j相容。- Input:
输入一组数据:一个整数n(n个活动);n组数据,每组两个数:活动的起始时间、结束时间;- Output:
输出活动集合中选出最大的相容活动子集合。
案例输入输出:
- Input:
111 4 3 5 0 6 5 7 3 8 5 9 6 10 8 11 8 12 2 13 12 14- Output:
活动1: 1 4活动4: 5 7活动8: 8 11活动11: 12 14
#include<iostream>
#include<algorithm>
using namespace std;
struct action{
int a,b;
int index;
};
bool cmp(const action &x,const action &y){
if(x.b<=y.b)
return true;
return false;
}
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;
}
}
}
int main()
{
int n;
action s[1025];
bool l[1025];
cin>>n;
for(int i=1; i<=n; i++){
cin>>s[i].a>>s[i].b;
s[i].index = i;
}
sort(s,s+n+1,cmp);
GreedySelector(n,s,l);
for(int i=1; i<=n; i++){
if(l[i]==true)
cout<<"活动"<<s[i].index<<": "<<s[i].a<<" "<<s[i].b<<endl;
}
return 0;
}
边栏推荐
- How to judge the performance of static code quality analysis tools? These five factors must be considered
- 只知道预制体是用来生成物体的?看我如何使用Unity生成UI预制体
- MySQL | GROUP_ The concat function concatenates the values of a column with commas
- web移动端:touchmove实现局部滚动
- Fillet big killer, use filter to build fillet and wave effect!
- Shell Chapter 7 exercise
- MySQL | GROUP_CONCAT函数,将某一列的值用逗号拼接
- 史上最全的立创元器件封装库导入AD详细教程(一直白嫖一直爽)
- 常见的几种PCB表面处理技术!
- The most detailed MySQL index analysis (mind map is attached at the end of the article)
猜你喜欢
随机推荐
tensorflow 调用多块GPU的一些错误
爬虫基础一
Esp8266 uses drv8833 drive board to drive N20 motor
Learn NLP with Transformer (Chapter 5)
[high concurrency] deeply analyze the execution process of worker threads in the thread pool through the source code
贪心问题01_活动安排代码分析
Understanding: idea uses Scala to write wordcount programs and generate jar packages
工作面试总遇秒杀?看了京东T8大咖私藏的秒杀系统笔记,已献出膝盖
Database design - Simplified dictionary table [easy to understand]
tensorflow入门
同事看了我的代码惊呼:居然是这么在Unity中用单例的
Web mobile terminal: touchmove realizes local scrolling
NowCoderTOP12-16——持续更新ing
玩游戏想记录一下自己超神的瞬间?那么就来看一下如何使用Unity截图吧
shell-第四天作业
My colleague looked at my code and exclaimed: how can I use a singleton in unity
SQL语言(四)
MySQL | GROUP_ The concat function concatenates the values of a column with commas
Leetcode sword finger offer 28. symmetric binary tree
LVS负载均衡之LVS-NAT与LVS-DR模式原理详解









