BNUOJ-26475 Cookie Selection 堆,线段树等

  题目链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=26475

  题意:每次输入一个操作,如果是数字,那么放入一个容器中,如果是#号,取出当前容器中的中间值。。

  数据结构基础题,显然维护两个堆就可以了,两个堆的size大小不超过1...

  其实各种数据结构都可以搞,比如线段树,先离线然后离散,然后线段树记录size,二分查找。。

 //STATUS:C++_AC_856MS_3192KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
//#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef long long LL;
typedef unsigned long long ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=1e9+,STA=;
//const LL LNF=1LL<<60;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End struct cmp{
bool operator()(const int a,const int b){
return a<b;
}
};
priority_queue<int,vector<int>,cmp > q1;
priority_queue<int,vector<int>,greater<int> > q2; int main()
{
// freopen("in.txt","r",stdin);
int i,j,a,cnt=;
char s[];
q1.push(-INF);q2.push(INF);
while(~scanf("%s",s))
{
if(s[]=='#'){
if(cnt){
printf("%d\n",q1.top());
q1.pop();
}
else {
printf("%d\n",q2.top());
q2.pop();
}
cnt^=;
}
else {
sscanf(s,"%d",&a);
q1.push(a);
cnt^=;
}
while(q1.size()>q2.size()){
q2.push(q1.top());q1.pop();
}
while(q1.size()<q2.size()){
q1.push(q2.top());q2.pop();
}
}
return ;
}
上一篇:关于 MVCC 的基础


下一篇:学习Markdown