题意:太长。。点进去自己看吧
思路:
一道模拟题,但直接模拟会卡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;
}