L2-014. 列车调度Pat::集合
2018-03-05 21:54
274 查看
如何能使轨道数最少?就是每次进入一个数字,看存在的轨道的是有没有比当前值大的,大的话把当前值替换当前轨道即可,如果没有就需新增轨道!样例:8 4 2 5 3 9 1 6 71 2 4 83 56 97需用4个轨道即可按递减输出了!所以按以上思路:输入一个值,看当前所有轨道是否有大于前前值的,有的话替换当前轨道值,否则新增轨道这里需要用set集合实现,否则超时了。。。因为set集合中有一个函数:lower_bound() 可以查出当前集合中有没有比要查的值大的值,有的话直接返回此值,这样我们就可以将此值删除,然后将当前值加入集合,最后的集合长度即为轨道数了!参考:心向阳光2014博客代码:[cpp] view plain copy#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;
}
#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;
}
相关文章推荐
- PAT L2-014. 列车调度
- PAT 天梯赛 L2-014. 列车调度 【队列】
- PAT L2-014. 列车调度 最长上升子序列
- PAT L2-014. 列车调度
- PAT L2-014. 列车调度 最长上升子序列
- PAT L2-014. 列车调度 最长上升子序列
- PAT L2-014. 列车调度 最长上升子序列
- PAT - 团体程序设计天梯赛-练习集 - L2-014 - 列车调度
- PAT L2-014. 列车调度 最长上升子序列
- L2-014. 列车调度-PAT团体程序设计天梯赛GPLT
- PAT L2-014. 列车调度 最长上升子序列
- PAT L2-014. 列车调度 二分查找优化,递减序列
- PAT L2-014. 列车调度 最长上升子序列
- PAT L2-014. 列车调度 最长上升子序列
- PAT L2-014 列车调度(Dilworth定理)
- 【PAT L2-014】列车调度(Dilworth定理)
- PAT:L2-014. 列车调度
- PAT(天梯赛练习题) -L2-014. 列车调度
- PAT-L2-014. 列车调度
- PAT - 天梯赛 L2-014 列车调度