博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 3697 Selecting courses(贪心+暴力)(2010 Asia Fuzhou Regional Contest)
阅读量:5159 次
发布时间:2019-06-13

本文共 2732 字,大约阅读时间需要 9 分钟。

Description

    A new Semester is coming and students are troubling for selecting courses. Students select their course on the web course system. There are n courses, the ith course is available during the time interval (A
i,B
i). That means, if you want to select the ith course, you must select it after time Ai and before time Bi. Ai and Bi are all in minutes. A student can only try to select a course every 5 minutes, but he can start trying at any time, and try as many times as he wants. For example, if you start trying to select courses at 5 minutes 21 seconds, then you can make other tries at 10 minutes 21 seconds, 15 minutes 21 seconds,20 minutes 21 seconds… and so on. A student can’t select more than one course at the same time. It may happen that no course is available when a student is making a try to select a course
 
You are to find the maximum number of courses that a student can select.
 

Input

There are no more than 100 test cases.
The first line of each test case contains an integer N. N is the number of courses (0<N<=300)
Then N lines follows. Each line contains two integers Ai and Bi (0<=A
i<B
i<=1000), meaning that the ith course is available during the time interval (Ai,Bi).
The input ends by N = 0.
 

Output

For each test case output a line containing an integer indicating the maximum number of courses that a student can select.

 

题目大意:给n个课程,每个课程有一个选择时间段(ai, bi),只能在这个时间段里面选择。然后你可以在任何时刻开始选课,然后每隔5分钟可以选一门课,问最多选多少门课。

思路:首先开始是越早越好,在8分开始不如在3分就开始。然后枚举5个开始时间,然后对每个时间枚举可以选择的课,选bi最小的(因为bi最小的被后面的时间选中的可能性最小,若bi能在后面被选中那么其他也能在后面被选中)。完全暴力不需要任何优化即可AC。

PS:那个(ai, bi)应该是开区间,不过我们可以选择在0分1秒的时候开始,所以可以看成是[ai, bi)。

 

代码(31MS):

1 #include 
2 #include
3 #include
4 #include
5 using namespace std; 6 7 const int MAXN = 310; 8 9 int a[MAXN], b[MAXN];10 bool sel[MAXN];11 int n;12 13 int solve() {14 int ret = 0;15 for(int st = 0; st < 5; ++st) {16 memset(sel, 0, sizeof(sel));17 int ans = 0;18 for(int i = st; i <= 1000; i += 5) {19 int best = -1;20 for(int j = 0; j < n; ++j)21 if(!sel[j] && a[j] <= i && i < b[j]) {22 if(best == -1) best = j;23 else if(b[j] < b[best]) best = j;24 }25 if(best != -1) {26 sel[best] = true;27 ++ans;28 }29 }30 if(ret < ans) ret = ans;31 }32 return ret;33 }34 35 int main() {36 while(scanf("%d", &n) != EOF && n) {37 for(int i = 0; i < n; ++i) scanf("%d%d", &a[i], &b[i]);38 printf("%d\n", solve());39 }40 }
View Code

 

转载于:https://www.cnblogs.com/oyking/p/3304612.html

你可能感兴趣的文章
内部元素一一相应的集合的算法优化,从list到hashmap
查看>>
游戏中的心理学(一):认知失调有前提条件
查看>>
SpringMVC-处理AJAX
查看>>
WHAT I READ FOR DEEP-LEARNING
查看>>
【Ruby】Ruby在Windows上的安装
查看>>
Objective C 总结(十一):KVC
查看>>
BZOJ 3747 洛谷 3582 [POI2015]Kinoman
查看>>
vue实战(7):完整开发登录页面(一)
查看>>
[转载]mysql的left,right,substr,instr截取字符串,截取
查看>>
Visual Studio自定义模板(二)
查看>>
【Mood-20】滴滤咖啡做法 IT工程师加班必备 更健康的coffee 项目经理加班密鉴
查看>>
摘抄详细的VUE生命周期
查看>>
javascript高级程序设计---js事件思维导图
查看>>
sprint计划会议
查看>>
读《构建之法-软件工程》第四章有感
查看>>
使用 Printf via SWO/SWV 输出调试信息
查看>>
.net 分布式架构之分布式锁实现(转)
查看>>
吴恩达机器学习笔记 —— 3 线性回归回顾
查看>>
Bouncy Castle内存溢出
查看>>
多线程_java多线程环境下栈信息分析思路
查看>>