[USACO14JAN]Recording the Moolympics

题目描述

Being a fan of all cold-weather sports (especially those involving cows),

Farmer John wants to record as much of the upcoming winter Moolympics as

possible.

The television schedule for the Moolympics consists of N different programs

(1 <= N <= 150), each with a designated starting time and ending time. FJ

has a dual-tuner recorder that can record two programs simultaneously.

Please help him determine the maximum number of programs he can record in

total. 冬奥会的电视时刻表包含N (1 <= N <= 150)个节目,每个节目都有开始和结束时间。农民约翰有两台录像机,请计算他最多可以录制多少个节目。

输入输出格式

输入格式:

 

  • Line 1: The integer N.

  • Lines 2..1+N: Each line contains the start and end time of a single

program (integers in the range 0..1,000,000,000).

 

输出格式:

 

  • Line 1: The maximum number of programs FJ can record.

 

输入输出样例

输入样例#1:
6
0 3
6 7
3 10
1 5
2 8
1 9
输出样例#1:
4

说明

INPUT DETAILS:

The Moolympics broadcast consists of 6 programs. The first runs from time

0 to time 3, and so on.

OUTPUT DETAILS:

FJ can record at most 4 programs. For example, he can record programs 1

and 3 back-to-back on the first tuner, and programs 2 and 4 on the second

tuner.

Source: USACO 2014 January Contest, Silver

 

错误贪心:两遍线段覆盖

错因:推样例

正确贪心:先按右端点从小到大排序,如果这个线段两台录像机都可以给,给结束时间晚的那个

 

#include<cstdio>
#include<algorithm>
using namespace std;
struct node
{
    int s,t;
}e[151];
bool cmp(node p,node q)
{
    return p.t<q.t;
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d%d",&e[i].s,&e[i].t);
    sort(e+1,e+n+1,cmp);
    int ans=0,end1=0,end2=0;
    for(int i=1;i<=n;i++)
    {
        if(e[i].s>=end1 && e[i].s>=end2) 
        {
            end1>end2 ? end1=e[i].t : end2=e[i].t;
            ans++; 
        }
        else if(e[i].s>=end1)
        {
            end1=e[i].t;
            ans++;
        }
        else if(e[i].s>=end2)
        {
            end2=e[i].t;
            ans++;
        }
    }
    printf("%d",ans);
}

 

posted @ 2017-09-02 08:11  TRTTG  阅读(410)  评论(0编辑  收藏  举报