KPI
Time Limit: 20 Sec Memory Limit: 256 MB
题目连接
http://acdream.info/problem?pid=1754
Description
Input
Output
对于每组数据,先输出一行
Case #i: 然后每一次"query",输出当前管道内重要值的中间值。
Sample Input
6 in 874 query out in 24622 in 12194 query
Sample Output
Case #1: 874 24622
HINT
题意
题解:
用一个set大法!
代码:
//qscqesze #include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <fstream> #include <map> #include <stack> typedef long long ll; using namespace std; //freopen("D.in","r",stdin); //freopen("D.out","w",stdout); #define sspeed ios_base::sync_with_stdio(0);cin.tie(0) #define test freopen("test.txt","r",stdin) #define maxn 2000001 #define mod 10007 #define eps 1e-9 int Num; char CH[20]; const int inf=0x3f3f3f3f; const ll infll = 0x3f3f3f3f3f3f3f3fLL; inline ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } inline void P(int x) { Num=0;if(!x){putchar('0');puts("");return;} while(x>0)CH[++Num]=x%10,x/=10; while(Num)putchar(CH[Num--]+48); puts(""); } //************************************************************************************** queue<ll> q; set<ll> ss; ll t,n,mid; char cmd[100]; void insert(ll x) { ss.insert(x); if(mid==-1) { mid=x; } else { if(x>mid) if(ss.size()%2==0) mid=*ss.upper_bound(mid); else { if(ss.size()%2==1) mid=*(--ss.lower_bound(mid)); } } q.push(x); } void dequeue() { ll num=q.front(); q.pop(); ss.erase(num); if(ss.empty()) { mid=-1; } else { if(num>mid) { if(ss.size()%2==1) mid=*(--ss.lower_bound(mid)); else if(num<mid) { if(ss.size()%2==0) mid=*ss.upper_bound(mid); } else { if(ss.size()%2==0) mid=*ss.upper_bound(mid); else mid=*(--ss.lower_bound(mid)); } } } } void solve() { n=read(); mid=-1; ss.clear(); while(!q.empty()) q.pop(); for(int i=1;i<=n;i++) { scanf("%s",cmd); if(cmd[0]=='i') { int x=read(); insert(x); } if(cmd[0]=='o') dequeue(); if(cmd[0]=='q') printf("%lld ",mid); } } int main() { //test; //freopen("1.out","w",stdout); int t=1; for(int cas=1;cas<=t;cas++) { printf("Case #%d: ",cas); solve(); } }