L2-014. 列车调度【set】
来源:互联网 发布:数据库给黑客删除 编辑:程序博客网 时间:2024/04/30 08:27
题目:列车调度
思路:
如何能使轨道数最少?就是每次进入一个数字,看存在的轨道的是有没有比当前值大的,大的话把当前值替换当前轨道即可,如果没有就需新增轨道!
样例:8 4 2 5 3 9 1 6 7
1 2 4 8
3 5
6 9
7
需用4个轨道即可按递减输出了!
所以按以上思路:输入一个值,看当前所有轨道是否有大于前前值的,有的话替换当前轨道值,否则新增轨道
这里需要用set集合实现,否则超时了。。。因为set集合中有一个函数:lower_bound() 可以查出当前集合中有没有比要查的值大的值,有的话直接返回此值,这样我们就可以将此值删除,然后将当前值加入集合,最后的集合长度即为轨道数了!
参考:心向阳光2014博客
代码:
#include <iostream>#include <cstdio>#include <set>#include <algorithm>using namespace std;set<int>s;int main(){ int n,fig; scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%d",&fig); if(s.empty()) s.insert(fig); else{ if(s.lower_bound(fig) != s.end()){//找到比当前输入值大的值 s.erase(s.lower_bound(fig));//将较大的那个值删除 s.insert(fig);//将当前值插入 }else s.insert(fig);//新增轨道 } } printf("%d\n",s.size()); return 0;}
0 0
- L2-014. 列车调度【set】
- L2-014. 列车调度
- L2-014. 列车调度
- L2-014. 列车调度
- L2-014. 列车调度
- L2-014. 列车调度
- L2-014. 列车调度
- L2-014. 列车调度
- L2-014. 列车调度
- L2-014. 列车调度
- L2-014. 列车调度
- L2-014. 列车调度
- L2-014. 列车调度
- L2-014. 列车调度
- L2-014. 列车调度
- L2-014. 列车调度
- L2-014. 列车调度(线段树 || set维护最小值)
- PAT-L2-014. 列车调度
- gets()用法
- 字符串反转
- 二维数组的初始化
- 数字颠倒
- c语言动态开辟内存
- L2-014. 列车调度【set】
- CodeForces #785 c Anton and Fairy Tale
- 字符个数统计
- c语言三维数组怎么用的?
- 文章标题
- 字符编码笔记:ASCII,Unicode和UTF-8
- Html文件打开中文出现乱码
- Spring MVC form表单post提交出现乱码
- Linux netstat命令详解