题目链接

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

思路

想要看的节目尽可能的多,则首先要将节目按照结束时间从早到晚排序,因为一个节目越早结束,留给后面的节目的时间就越多,也就能看到更多的节目。如果按照开始时间从早到晚排序,由于节目可能持续很长时间,所以将会导致最后的结果不准确。举个例子,有三个节目(1,10),(3,5),(6,8),按开始时间从早到晚排序,则只能看1个节目(1,10),而按照结束时间从早到晚排序,则能看2个节目(3,5)和(6,8)。

代码

 #include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
using namespace std; struct Node
{
int s, e; Node(int s, int e):s(s), e(e) {}
bool operator < (const Node& node) const
{
return e < node.e; //将节目按照结束时间从早到晚排序
}
};
vector<Node> v; int main()
{
//freopen("hdoj2037.txt", "r", stdin);
int n;
while(cin >> n && n)
{
v.clear();
int s, e;
for(int i=; i<n; i++)
{
cin >> s >> e;
v.push_back(Node(s, e));
} sort(v.begin(), v.end());
int ans = ;
int t = v[].e;
for(int i=; i<n; i++)
{
if(v[i].s >= t)
{
ans++;
t = v[i].e;
}
}
cout << ans <<endl;
}
return ;
}

最新文章

  1. 修改组策略,禁止用户修改IP
  2. Cheatsheet: 2016 09.01 ~ 09.30
  3. 不可或缺 Windows Native (15) - C++: 命名空间
  4. Ajax基本概念和原理
  5. 推荐一个学习golang的地址
  6. Json数据处理
  7. 剑指offer--面试题23
  8. Django路由
  9. Chapter 1 Mr.Sherlock Holmes
  10. Python基础教程3——教你用Python做个简单的加密程序(还基础什么呀,直接来练习吧,带源码)
  11. zabbix的各种键值
  12. editplus注册码
  13. CF1045G:AI robots(CDQ分治)
  14. 【BZOJ2839】集合计数&amp;&amp;【BZOJ3622】已经没有什么好害怕的了
  15. nginx反向代理和tomcat集群(适用于ubutnu16.04及其centos7)
  16. Using PHP as a Spring MVC View via Quercus(转)
  17. php命令行脚本 mock数据
  18. Spring Bean注册解析(二)
  19. Jenkins搭建.NET自动编译测试与发布环境
  20. python unittest框架理解与总结(二)

热门文章

  1. 3.fIddler的使用
  2. centos无法通过ssh连接的解决
  3. CPU字节序
  4. 2016-2017-20155329 《Java程序设计》第7周学习总结
  5. shell if判断中常用的a-z表达式含义
  6. 38、使用IO流进行文件拷贝
  7. Python文件操作-文件的增删改查
  8. F - Number of Connected Components UVALive - 7638 (并查集 + 思维)
  9. 推荐一本springBoot学习书籍---深入浅出springBoot2.x
  10. 【codeforces】【比赛题解】#868 CF Round #438 (Div.1+Div.2)