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 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

题意是:每次选一个起始点,以后只能在+5这些点上进行选课。因为数据范围比较小,可以暴力。

枚举0、1、2、3、  4这5个起点,对于每个确定的起点可以得到一些可以进行选课的点,把选课时间段以终点进行从小到大排序,枚举可得最优解。另外,区间(a,b)可以变为[a,b),因为对于a分钟来说,a分01秒是可以选的。

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
#define N 1005
const int inf=0x3fffffff;
int mark[N];
struct node
{
    int l,r;
}g[305];
bool cmp(node a,node b)
{
    return a.r<b.r;
}
int main()
{
    int i,j,k,r,n;
    while(scanf("%d",&n),n)
    {
        r=0;
        for(i=0;i<n;i++)
        {
            scanf("%d%d",&g[i].l,&g[i].r);
            r=max(r,g[i].r);
        }
        sort(g,g+n,cmp);
        int ans=0;
        for(i=0;i<5;i++)
        {
            memset(mark,0,sizeof(mark));
            for(j=i;j<r;j+=5)
                mark[j]=1;
            int tmp=0;
            for(j=0;j<n;j++)
            {
                for(k=g[j].l;k<g[j].r;k++)
                {
                    if(mark[k])
                    {
                        mark[k]=0;
                        tmp++;
                        break;
                    }
                }
            }
            ans=max(ans,tmp);
        }
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-11-29 03:37:08

hdu 3697 Selecting courses (暴力+贪心)的相关文章

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

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 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 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

hdu 4825 Xor Sum(trie+贪心)

hdu 4825 Xor Sum(trie+贪心) 刚刚补了前天的CF的D题再做这题感觉轻松了许多.简直一个模子啊...跑树上异或x最大值.贪心地让某位的值与x对应位的值不同即可. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <cmath> 6 #define CLR(a,b) memset((a)

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 1009 FatMouse&#39; Trade (贪心选取)

链接:click here~~ 题意:老鼠准备了M磅猫食,准备拿这些猫食跟猫交换自己喜欢的食物.有N个房间,每个房间里面都有食物.你可以得到J[i]单位的食物,但你需要付出F[i]单位的的猫食. 计算M磅猫食可以获得最多食物的重量. [解题思路]贪心算法,求最优解.将J[i]/F[i]的值从大到小排列,每次取最大的,局部最优,达到全局最优,从而获得最大值. 代码: // 贪心策略,优先选择投资最大的房间,每选择一次,交换次数依次减少,最后的次数用于价值最小的 //注意精度转化:1.0*(int

HDU 4952 Poor Mitsui(贪心)

HDU 4957 Poor Mitsui 题目链接 思路:利用相邻交换法去贪心即可,注意容积为0的情况,这是个坑点 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 45; struct SB { int a, b; } sb[N]; bool cmp(SB x, SB y) { return x.b * y.a < x.