14.会场安排问题(L4)
- 描述
- 学校的小礼堂每天都会有许多活动,有时间这些活动的计划时间会发生冲突,需要选择出一些活动进行举办。小刘的工作就是安排学校小礼堂的活动,每个时间最多安排一个活动。现在小刘有一些活动计划的时间表,他想尽可能的安排更多的活动,请问他该如何安排。
- 输入
- 第一行是一个整型数m(m<100)表示共有m组测试数据。
每组测试数据的第一行是一个整数n(1<n<10000)表示该测试数据共有n个活动。
随后的n行,每行有两个正整数Bi,Ei(0<=Bi,Ei<10000),分别表示第i个活动的起始与结束时间(Bi<=Ei) - 输出
- 对于每一组输入,输出最多能够安排的活动数量。
每组的输出占一行 - 样例输入
-
2
2
1 10
10 11
3
1 10
10 11
11 20 - 样例输出
-
1
2 - 提示
- 注意:如果上一个活动在t时间结束,下一个活动最早应该在t+1时间开始
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct act{
int s;
int e;
}STACT;
int compare(const void *a, const void *b){
STACT *acts1 = (STACT *)a;
STACT *acts2 = (STACT *)b;
return (acts1->e - acts2->e);
}
int main()
{
//m:num of test,n:num of activities
int m,n;
int i;
scanf("%d", &m);
getchar();
while (m--) {
scanf("%d", &n);
getchar();
STACT *ats=(STACT *)malloc(sizeof(STACT)*n);
memset(ats, 0x00, sizeof(STACT)*n);
for (i = ; i < n; ++i) {
scanf("%d%d", &ats[i].s, &ats[i].e);
}
qsort(ats, n, sizeof(ats[]), compare);
int sum=;
int curTime=-;
for (i = ; i < n; ++i) {
if(curTime < ats[i].s){
++sum;
curTime = ats[i].e;
}
else {
continue;
}
}
printf("%d\n", sum);
if(ats != NULL){
free(ats);
ats=NULL;
}
}
return ;
}
Time: 172ms Space:316
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct act{
int s;
int e;
}actnode[];
int compare(const void *a, const void *b){
act *acts1 = (act *)a;
act *acts2 = (act *)b;
return (acts1->e - acts2->e);
}
int main()
{
//m:num of test,n:num of activities
int m,n;
int i;
scanf("%d", &m);
getchar();
while (m--) {
scanf("%d", &n);
getchar(); memset(&actnode , 0x00, sizeof(actnode));
for (i = ; i < n; ++i) {
scanf("%d%d", &actnode[i].s, &actnode[i].e);
}
qsort(actnode, n, sizeof(actnode[]), compare);
int sum=;
int curTime=-;
for (i = ; i < n; ++i) {
if(curTime < actnode[i].s){
++sum;
curTime = actnode[i].e;
}
else {
continue;
}
}
printf("%d\n", sum); }
return ;
}
212ms 392
选择结束时间最早的,贪心算法
解体思路
有误:
#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>
using namespace std;
#define debug(x) cout << #x << " at line " << __LINE__ << " is: " << x << endl struct noac{
int Ti_s;
int Ti_e;
bool operator<(const noac& nc) const // DESC by Ti_s
{
return (Ti_s > nc.Ti_s);
}
friend void operator<<(ostream &os, const noac &nc)
{
os << nc.Ti_s << ' ' << nc.Ti_e << endl;
}
}tms[]; int main(){
int m,n;
scanf("%d", &m);
while (m--) {
scanf("%d", &n);
memset(&tms, , 0x00);
for (int i = ; i < n; ++i)
{
scanf("%d%d", &tms[i].Ti_s, &tms[i].Ti_e);
}
sort(tms, tms + n); int maxac = ;
for (int i = ; i < n; ++i)
{
int idx = i;
int sum = ;
for (int j = i+; j < n; ++j)
{
if(tms[idx].Ti_s >= tms[j].Ti_e)
{
++sum;
++idx;
}else
{
continue;
}
}
maxac = max(maxac, sum);
}
printf("%d\n", maxac);
}
return ;
}
比如说:
6
1 5
2 3
10 11
6 7
8 9
4 5
output: 4,而实际输出为5
14.会场安排问题(L4)的更多相关文章
- nyoj 14 会场安排问题(贪心专题)java
会场安排问题 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 学校的小礼堂每天都会有许多活动,有时间这些活动的计划时间会发生冲突,需要选择出一些活动进行举办.小刘的工 ...
- nyoj 14 会场安排问题(贪心专题)
会场安排问题 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 学校的小礼堂每天都会有许多活动,有时间这些活动的计划时间会发生冲突,需要选择出一些活动进行举办.小刘的工 ...
- nyoj 14 会场安排问题
会场安排问题 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 学校的小礼堂每天都会有许多活动,有时间这些活动的计划时间会发生冲突,需要选择出一些活动进行举办.小刘的工 ...
- NYOJ 14 会场安排问题(也算是经典问题了)
会场安排问题 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 学校的小礼堂每天都会有许多活动,有时间这些活动的计划时间会发生冲突,需要选择出一些活动进行举办.小刘的工作就 ...
- nyoj 题目14 会场安排问题
会场安排问题 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 学校的小礼堂每天都会有许多活动,有时间这些活动的计划时间会发生冲突,需要选择出一些活动进行举办.小刘的工 ...
- 基于贪心算法的几类区间覆盖问题 nyoj 12喷水装置(二) nyoj 14会场安排问题
1)区间完全覆盖问题 问题描述:给定一个长度为m的区间,再给出n条线段的起点和终点(注意这里是闭区间),求最少使用多少条线段可以将整个区间完全覆盖 样例: 区间长度8,可选的覆盖线段[2,6],[1, ...
- 会场安排问题--nyoj题目14
会场安排问题 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 学校的小礼堂每天都会有许多活动,有时间这些活动的计划时间会发生冲突,需要选择出一些活动进行举办.小刘的工 ...
- ACM 会场安排问题
会场安排问题 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 学校的小礼堂每天都会有许多活动,有时间这些活动的计划时间会发生冲突,需要选择出一些活动进行举办.小刘的工 ...
- NYOJ 14 场地安排
/* 中国标题的含义: 中国的翻译: 标题效果:寻求预定场地的最大数量,只有一个活动可以安排时间 解决问题的思路:然后使用结构数.之后再构建一个排序,排序结束时间活动.然后基于开始时间为大于一个事件的 ...
随机推荐
- 一波三折Miz702终于能显示桌面上网啦
先上两张图,总结之后再说啦---
- 汇编 AND 运算
AND运算 逻辑与&& 按位与& 一.AND运算 1&&12=1; 12&&0=0; 0&&1=0; 0040100 ...
- Bluedroid协议栈HCI线程分析
蓝牙进程中有多个线程,其中HCI 线程是负责处理蓝牙主机端和控制器的数据处理和收发的工作. 本篇文章就是分析一下该线程的数据处理流程. 1.跟HCI相关的接口 首先看看hci的相关的接口:在hci_l ...
- 使用 spring-boot-devtools 进行热部署
2019/3/5 更新: 发现热部署不生效,出现页面显示error的错误,然后在 application.properties 中注释了下面两行成功实现热部署(直接删掉也可以) #spring.dev ...
- python面试题(四)
一.数据类型 1.字典 1.1 现有字典 dict={‘a’:24,‘g’:52,‘i’:12,‘k’:33}请按字典中的 value 值进行排序? sorted(dict.items(),key=l ...
- Egret(白鹭引擎)——“TypeError: Cannot read property 'asCom' of null”
前言 相信我,这个错误新手都不陌生:TypeError: Cannot read property 'asCom' of null 还有,一定要看我上一篇,哦不(人家应该是报了这个错,才找到看到这篇文 ...
- 设计模式 笔记 责任链模式 chain of responsibility
//---------------------------15/04/25---------------------------- //Chain of responsibility 责任链----- ...
- [转载] 相机越贵画质越好?聊聊CMOS设计
似乎在很多人心目中,个位数机身就代表了品牌最强成像素质,这或许有“人不识货钱识货”的道理在作祟,但事实上如佳能1DX2或尼康D5,又或是索尼A9这种旗舰机真的就一定能代表本家的画质巅峰么?这一切都得从 ...
- Seay源代码审计系统的配置和安装
2014年7月31日 Seay源代码审计系统2.1 时隔刚好一年之久,源代码审计系统再次更新,这次主要优化审计体验,优化了漏洞规则,算是小幅更新,原来使用者打开程序会提示自动更新. 1.优化原有规则, ...
- eclipse中设置项目的编码方式
1.windows->Preferences...打开"首选项"对话框,左侧导航树,导航到general->Workspace,右侧Text file encoding ...