码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 4288 Coder(模拟) 附:upper_bound与lower_bound的比较

时间:2015-07-27 23:02:20      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:acm   模拟   stl   

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是严格大于的第一个
(1, 1, 1, 2)中找1,返回的lower_bound是第一个位置,upper_bound是第四个.

ps.这道题还可以用线段树。。然而。。不说了。。TT


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;
}




版权声明:本文为博主原创文章,未经博主允许不得转载。

HDU 4288 Coder(模拟) 附:upper_bound与lower_bound的比较

标签:acm   模拟   stl   

原文地址:http://blog.csdn.net/qq_15714857/article/details/47091575

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!