超时算法,利用2的特殊性,用2个multiset来维护。单个multiset维护没法立即找到中位数。
其实也可以只用1个multiset,用一个中位指针,++,--来维护中位数。
#include<iostream> #include<stdio.h> #include<math.h> #include<string.h> #include<stdlib.h> #include<algorithm> #include<queue> #include<vector> #include<string> #include<map> #include<stack> #include<set> using namespace std; typedef long long ll; const int maxn = 1e4 + 7; int n; multiset<int> up, lo; stack<int> sta; void adapt() { int cnt = up.size() + lo.size(); int upsz = ceil(cnt / 2.0); while (up.size() < upsz) { up.insert(*lo.begin()); lo.erase(lo.begin()); } while (up.size() > upsz) { int x = *up.rbegin(); lo.insert(x); up.erase(up.find(x)); } } void push(int x) { up.insert(x); adapt(); } int peek() { return *(up.rbegin()); } void pop(int x) { if (up.find(x) != up.end()) up.erase(up.find(x)); else lo.erase(lo.find(x)); adapt(); } int main() { freopen("in.txt", "r", stdin); cin >> n; while (sta.empty() == false) sta.pop(); up.clear(), lo.clear(); char cmd[13]; int x; while (n--) { cin >> cmd; if (cmd[1] != 'u') { if (lo.empty() && up.empty()) { puts("Invalid"); continue; } if (cmd[1] == 'o') { int x = sta.top(); sta.pop(); cout << x << endl; pop(x); } else if (cmd[1] == 'e') { cout << peek() << endl; } } else { cin >> x; sta.push(x); push(x); } } return 0; }
此题正解树状数组
#include<stdio.h> #include<cstring> #include<iostream> #include<string> using namespace std; ; int c[N]; int lowbit(int i){ return i&(-i); } void add(int pos,int value){ while(pos<N){ c[pos]+=value; pos+=lowbit(pos); } } int sum(int pos){ ; ){ res+=c[pos]; pos-=lowbit(pos); } return res; } int find(int value){ ,r=N-,median,res; ){ ==) median=(l+r)/; else median=(l+r-)/; res=sum(median); if(res<value) l=median; else r=median; } ; } int main(){ //freopen("D://test.txt","r",stdin); ]; ,n,pos; memset(c,,sizeof(c)); scanf("%d",&n); while(n--){ scanf("%s",ss); ]=='u'){ scanf("%d",&pos); stack[++top]=pos; add(pos,); }]=='o'){ ){ printf("Invalid\n"); continue; } int out=stack[top]; add(); printf("%d\n",stack[top--]); }]=='e'){ ){ printf("Invalid\n"); continue; } int res; ==) res=find(top/); else res=find((top+)/); printf("%d\n",res); }else{ printf("Invalid\n"); } } ; }
类似题目:zoj3612