HDU 4288 Coder(模拟) 附:upper_bound与lower_bound的比较
来源:互联网 发布:淘宝怎么改默认地址 编辑:程序博客网 时间:2024/05/22 07:43
HDU 4288
题意:太长。。点进去自己看吧
思路:
一道模拟题,但直接模拟会卡TLE,所以进行些许优化,将复杂度/5.
简而言之就是用一个有序数组来模拟set。
优化是利用lower_bound函数,这里简介下lower_bound 与 upper_bound 的区别:
摘自:http://blog.csdn.net/weiguang_123/article/details/7987823
lower_bound返回[First,last)中,可以插入value的第一个位置,使得插入后仍旧满足按照operator<排序的顺序
upper_bound返回[First,last)中,比value大的第一个元素的位置
当[First,last)中没有value这个数时,lower_bound和upper_bound返回的位置是同一个位置
注意这两个函数返回的都是迭代器
总结来说就是:
lower_bound是大于等于的第一个
upper_bound是严格大于的第一个
ps.这道题还可以用线段树。。然而。。不说了。。TT(1, 1, 1, 2)中找1,返回的lower_bound是第一个位置,upper_bound是第四个.
code:
/** @author Novicer* language : C++/C*/#include<iostream>#include<sstream>#include<fstream>#include<vector>#include<list>#include<deque>#include<queue>#include<stack>#include<map>#include<set>#include<bitset>#include<algorithm>#include<cstdio>#include<cstdlib>#include<cstring>#include<cctype>#include<cmath>#include<ctime>#include<iomanip>using namespace std;const double eps(1e-8);typedef long long lint;vector<int> s;int main(){//freopen("input.txt","r",stdin);int n;while(cin >> n){s.clear();vector<int>::iterator it;for(int i = 1 ; i <= n ; i++){char op[10];scanf("%s",op);if(op[0] == 'a'){int tmp;scanf("%d",&tmp);it = lower_bound(s.begin(),s.end(),tmp);s.insert(it,tmp);}else if(op[0] == 'd'){int tmp;scanf("%d",&tmp);it = lower_bound(s.begin(),s.end(),tmp);s.erase(it);}else if(op[0] == 's'){lint sum = 0;int i ;for( i = 2 ; i < s.size() ; i+=5){sum += s[i];}cout << sum << endl;}}}return 0;}
0 0
- HDU 4288 Coder(模拟) 附:upper_bound与lower_bound的比较
- HDU 4288 Coder(vector + upper_bound, lower_bound)
- hdu 4288 Coder(线段树+离散化,lower_bound&&upper_bound的运用)
- hdu 4288(lower_bound&&upper_bound的运用)
- lower_bound()与upper_bound()的区别
- lower_bound与upper_bound的用法
- lower_bound()与upper_bound()函数的使用
- lower_bound与upper_bound还有fill的使用
- 二分lower_bound()与upper_bound()的运用
- 关于lower_bound与upper_bound的实用技巧
- upper_bound()与lower_bound()使用方法
- upper_bound() 与 lower_bound() 使用方法
- upper_bound()与lower_bound…
- upper_bound()与lower_bound()使用方法
- lower_bound与upper_bound
- C++ lower_bound与upper_bound
- lower_bound 与 upper_bound
- lower_bound()与upper_bound()
- 如何修改Ubuntu环境下的命令提示符显示效果
- 指针与二维数组
- Where is the Marble?
- HDOJ 1237 简单计算器(堆栈)
- Array.prototype.slice.call(arguments)
- HDU 4288 Coder(模拟) 附:upper_bound与lower_bound的比较
- iOS反射机制
- virtual empty() const中的const有的作用
- HDU 1272--小希的迷宫【并查集,有值得注意的地方】
- hbase 增删改查 api 简单操作
- 结构体大小计算
- UI学习笔记一 UIView和UILabel
- iOS开发分分钟搞定C语言——函数
- cocos2d3.2 渲染优化机制