【HDU2037】今年暑假不AC

http://acm.hdu.edu.cn/showproblem.php?pid=2037

“今年暑假不AC?”
“是的。”
“那你干什么呢?”
“看世界杯呀,笨蛋!”
“@#$%^&*%...”

确实如此,世界杯来了,球迷的节日也来了,估计很多ACMer也会抛开电脑,奔向电视了。
作为球迷,一定想看尽量多的完整的比赛,当然,作为新时代的好青年,你一定还会看一些其它的节目,比如新闻联播(永远不要忘记关心国家大事)、非常6+7、超级女生,以及王小丫的《开心辞典》等等,假设你已经知道了所有你喜欢看的电视节目的转播时间表,你会合理安排吗?(目标是能看尽量多的完整节目)

 没什么戏~~~~~

Solution

贪心,按左端点排序,扫一遍,记录右指针,当l>=右指针(ans++)

// This file is made by YJinpeng,created by XuYike's black technology automatically.
// Copyright (C) 2016 ChangJun High School, Inc.
// I don't know what this program is.

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#define MOD 1000000007
#define INF 1e9
using namespace std;
typedef long long LL;
const int MAXN=110;
inline int gi() {
	register int w=0,q=0;register char ch=getchar();
	while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
	if(ch=='-')q=1,ch=getchar();
	while(ch>='0'&&ch<='9')w=w*10+ch-'0',ch=getchar();
	return q?-w:w;
}
struct node{
    int l,r;
    bool operator<(node a)const{
        return a.l==l?r<=a.r:l<a.l;
    }
}a[MAXN];
int main()
{
	freopen("2037.in","r",stdin);
	freopen("2037.out","w",stdout);
    int n;
	while(n=gi(),n){
        for(int i=1;i<=n;i++)
            a[i]=(node){gi(),gi()};
        sort(a+1,a+1+n);int ans=0;
        for(int i=1,to=-1;i<=n;i++){
            if(a[i].l>=to)ans++,to=a[i].r;
            to=min(a[i].r,to);
        }printf("%d
",ans);
    }
	return 0;
}

  

原文地址:https://www.cnblogs.com/YJinpeng/p/5974301.html