HDU 3697 Selecting courses(贪心)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3697

Problem 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 (Ai,Bi). 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<=Ai<Bi<=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.

Sample Input

2
1 10
4 5
0

Sample Output

2

Source

2010 Asia Fuzhou Regional Contest

题意:

有n门课程(n<=300),每门课程有个选课的时间段s,e,只能在s之后,在e之前选择该门课程。每位同学可以选择任意一个开始时间,然后每5分钟有一次选课机会,问每位同学最多可以选多少门课。

PS:

贪心,枚举开始的5个人时间!每次选择课程结束时间最早的!

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 317;
struct sub
{
    int s;
    int e;
};
bool cmp(sub x, sub y)
{
    if(x.e == y.e)
    {
        return x.s < y.s;
    }
    return x.e < y.e;
}
int main()
{
    sub a[maxn];
    int t;
    int n;
    int vis[maxn];
    while(scanf("%d",&n) && n)
    {
        for(int i = 0; i < n; i++)
        {
            scanf("%d%d",&a[i].s,&a[i].e);
        }
        sort(a,a+n,cmp);
        int ans = 0, cont = 0;
        for(int i = 0; i < 5; i++)
        {
            memset(vis,0,sizeof(vis));
            cont = 0;
            for(int j = i; j < a[n-1].e; j += 5)
            {
                for(int k = 0; k < n; k++)
                {
                    //printf("%d %d %d\n",j,a[k].s,a[k].e);
                    if(!vis[k] && j>=a[k].s && j<a[k].e)
                    {
                        cont++;
                        vis[k] = 1;
                        break;
                    }
                }
            }
            if(ans < cont)
            {
                ans = cont;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-10-15 16:14:32

HDU 3697 Selecting courses(贪心)的相关文章

hdu 3697 Selecting courses (暴力+贪心)

Selecting courses Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 62768/32768 K (Java/Others) Total Submission(s): 1856    Accepted Submission(s): 469 Problem Description A new Semester is coming and students are troubling for selecting cours

HDU 3697 Selecting courses 选课(AC代码)贪心

题意:一个学生要选课,给出一系列课程的可选时间(按分钟计),在同一时刻只能选一门课程(精确的),每隔5分钟才能选一次课,也就是说,从你第一次开始选课起,每过5分钟,要么选课,要么不选,不能隔6分钟再选.在给出的课程的事件Ai~Bi内,Bi起的那分钟是不能够选的了,就是说截止到(Bi-1)分钟59秒还能选,Bi就不能选了. 思路:由于n最大才300,那就可以使用暴力解法.开始时刻可以从0~4分钟这5个时刻开始,因为每5分钟是个周期,比如0分没选,而5分才选了,这和从5分才开始选是一样的.每隔5分钟

Hdu 3697 Selecting courses(贪心+暴力)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3697 思路:将课程按照右端点从小到大排序,相同时按照左端点从小到大排序.选课开始时间只能是0,1,2,3,4,开始时间确定后每次选课时间确定,暴力枚举即可. #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const in

Hdoj 3697 Selecting courses 【贪心】

Selecting courses Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 62768/32768 K (Java/Others) Total Submission(s): 2082    Accepted Submission(s): 543 Problem Description A new Semester is coming and students are troubling for selecting cours

hdu 3697 10 福州 现场 H - Selecting courses

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

Poj 2239 Selecting Courses 【二分匹配】

Selecting Courses Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9007 Accepted: 4010 Description It is well known that it is not easy to select courses in the college, for there is usually conflict among the time of the courses. Li Ming i

hdu 4296 Buildings(贪心)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4296 Buildings Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1822    Accepted Submission(s): 722 Problem Description Have you ever heard the sto

POJ 2239 Selecting Courses(二分匹配)

题目链接:http://poj.org/problem?id=2239 Description It is well known that it is not easy to select courses in the college, for there is usually conflict among the time of the courses. Li Ming is a student who loves study every much, and at the beginning

hdu 4864 Task (贪心)

# include <stdio.h> # include <algorithm> # include <string.h> using namespace std; struct node { int t; int v; int yy; }; struct node a[100010],b[100010]; bool cmp(node a1,node a2) { if(a1.t==a2.t)//先按时间从大到小 return a1.v>a2.v;//再按水平从大