hdu 2390 Olympic Games【贪心】

来源:互联网 发布:mfc编程入门 编辑:程序博客网 时间:2024/05/21 14:09

Olympic Games

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 884    Accepted Submission(s): 398

Problem Description

Mike loves the olympic games. What he hates about olympic games – besides them taking place only once every four years – is that the individual events are scheduled concurrently. That way it is never possible to watch all competitions live. Mike always tries to plan his personal schedule in order to maximize the number of events he can attend to. However, at the end he is never sure whether or not his schedule actually is optimal. Help him!

Given the days and times on which events take place, determine the maximum number of events Mike can watch. Mike never leaves during an event, so it is not possible to partially attend to events.

 

 

Input

The input starts with a line containing a single integer n, the number of test cases.
Each test case starts with a line containing an integer 1 <= m <= 50000 that specifies the number of different events. The next m lines describe one event with three integers d, s, e. The event takes place on day d of the olympics, it starts at s and it ends at e, where the times are given in the form hhmm. All events are assumed to conclude the same day they started.

 

 

Output

The output for every test case begins with a line containing “Scenario #i:”, where i is the number of the test case counting from 1. Then, output a single line containing the number of events Mike can attend to at most. The time for getting from one event to another can be neglected. Events starting at time t can be scheduled after events ending at t as long as there are no other conflicts. Terminate each test case with an empty line.

 

 

Sample Input

2

10

1 1220 1340

2 1155 1220

2 1220 1340

3 1220 1240

1 1200 1320

2 1250 1310

2 1330 1550

3 1030 1130

3 1130 1300

3 1240 1330

3

1 0500 2200

1 0000 0700

1 2000 2359

 

 

Sample Output

Scenario #1:

7

 

Scenario #2:

2

 

 题目大意:主人公非常喜欢奥林匹克游戏,所以他希望看更多的节目,一共给出n个节目,每个节目都有其在第几天,开始时间和结束时间,问他最多能看到多少节目。


思路:贪心。

对天数进行从小到大排序,再对结束时间从小到大排序,统计即可。


AC代码:
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;struct T_T{    int d,s,e;}a[500000];int cmp(T_T a,T_T b){    if(a.d==b.d)    {        if(a.e!=b.e)return a.e<b.e;        else return a.s<b.s;    }    else return a.d<b.d;}int main(){    int t;    int kase=0;    scanf("%d",&t);    while(t--)    {        int n;        scanf("%d",&n);        for(int i=0;i<n;i++)        {            scanf("%d%d%d",&a[i].d,&a[i].s,&a[i].e);        }        sort(a,a+n,cmp);        int output=0;        int day,end;        for(int i=0;i<n;i++)        {           // printf("%d %d %d\n",a[i].d,a[i].s,a[i].e);            if(i==0||a[i].d!=day)            {                output++;                day=a[i].d;                end=a[i].e;            }            else            {                if(a[i].s>=end)                {                    end=a[i].e;                    output++;                }            }        }        printf("Scenario #%d:\n",++kase);        printf("%d\n",output);        printf("\n");    }}






0 0
原创粉丝点击