题目

代码
#include <iostream>
#include <iostream>
#include <cstring>
#include <stack>
#include <algorithm>
#include <queue>
#include <vector>
#include <string>
#define MAXX 10010
using namespace std;
vector<int> v;
vector<int> tt;
vector<int>::iterator it;
int main()
{
int n;
cin >> n;
string s;
while (n--)
{
cin >> s;
if (s == "Pop")
{
if (v.empty())
cout<<"Invalid"<<endl;
else
{
cout<<v.back()<<endl;
it = lower_bound(tt.begin(),tt.end(),v.back());
tt.erase(it);
v.pop_back();
}
}
else if (s == "Push")
{
int x;
cin >> x;
v.push_back(x);
it = lower_bound(tt.begin(),tt.end(),x);
tt.insert(it,x);
}
else
{
if (v.empty())
cout<<"Invalid"<<endl;
else
{
int tmp = tt.size();
if (tmp%2==0)
tmp/=2;
else
tmp = (tmp+1)/2;
cout<<tt[tmp-1]<<endl;
}
}
}
return 0;
}
总结:
1. low_bound()的使用方法

#include<bits/stdc++.h>
using namespace std;
int main() {
int a[] = {1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4};
cout << (lower_bound(a, a + 12, 4) - a) << endl;
cout << (upper_bound(a, a + 12, 4) - a) << endl;
cout << (lower_bound(a, a + 12, 5) - a) << endl;
cout << (upper_bound(a, a + 12, 5) - a) << endl;
cout << (lower_bound(a, a + 12, 0) - a) << endl;
cout << (upper_bound(a, a + 12, 0) - a) << endl;
return 0;
}
- 在升序序列中 lower_bound 进行二分查找,返回第一个大于等于 参数val 的序列值的迭代器,然后减去首迭代器 a,就是两个迭代器的距离了(在这里也就是数组中下标)
- 在升序序列中 upper_bound 进行二分查找,返回第一个大于 参数val 的 序列值的迭代器,如果找不到,返回一个尾迭代器(在这里是数组越界后的那一个元素的指针),它在数组中下标为 12
vector使用方法
v.push_back(a);
vector<int>::iterator it;
for(it=vec.begin();it!=vec.end();it++)
cout<<*it<<endl;
v.insert(v.begin()+i,x);
v.erase(v.begin()+i);
v.erase(v.begin()+i,v.begin()+j);
bool cmp(const int &a,const int &b)
{
return a>b;
}
sort(vec.begin(),vec.end(),cmp);