PAT (Advanced Level) Practice - 1057 Stack(30 分)

简介: PAT (Advanced Level) Practice - 1057 Stack(30 分)

题目链接:点击打开链接

 

题目大意:略。


解题思路:multiset 技巧分为两个set,一个从小到大,一个从大到小,来控制平衡求第几大K数。

image.png


AC 代码

#include<bits/stdc++.h>#include<cmath>#define mem(a,b) memset(a,b,sizeof a)#define ssclr(ss) ss.clear(), ss.str("")#define INF 0x3f3f3f3f#define MOD 1000000007usingnamespacestd;
typedeflonglongll;
stack<int>sk;
multiset<int>stmax; // 0->1multiset<int,greater<int>>stmin; // 1->0intmid;
voidkeep()
{
intlen1=stmin.size(), len2=stmax.size();
if(len1<len2) // 1 2    {
stmin.insert(*stmax.begin());
stmax.erase(stmax.begin());
    }
elseif(len1-len2>=2) // 2 2,5 3    {
stmax.insert(*stmin.begin());
stmin.erase(stmin.begin());
    }
mid=*stmin.begin();
}
intmain()
{
intn,val,id;
charop[15];
scanf("%d",&n);
for(inti=0;i<n;i++)
    {
scanf("%s",op);
if(op[1]=='u')
        {
scanf("%d",&val);
sk.push(val);
if(val<=mid) stmin.insert(val);
elsestmax.insert(val);
keep();
        }
elseif(op[1]=='o')
        {
if(sk.size()==0) puts("Invalid");
else            {
inttp=sk.top(); sk.pop();
printf("%d\n",tp);
if(tp>*stmin.begin()) stmax.erase(stmax.find(tp));
elsestmin.erase(stmin.find(tp));
if(sk.size()!=0) keep();
            }
        }
elseif(op[1]=='e')
        {
if(sk.size()==0) puts("Invalid");
elseprintf("%d\n",mid);
        }
    }
return0;
}
目录
相关文章
|
移动开发 C语言
PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分)
PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分)
67 0
|
存储
PAT (Advanced Level) Practice - 1126 Eulerian Path(25 分)
PAT (Advanced Level) Practice - 1126 Eulerian Path(25 分)
113 0
PAT (Advanced Level) Practice - 1147 Heaps(30 分)
PAT (Advanced Level) Practice - 1147 Heaps(30 分)
96 0
PAT (Advanced Level) Practice - 1095 Cars on Campus(30 分)
PAT (Advanced Level) Practice - 1095 Cars on Campus(30 分)
111 0
|
存储
PAT (Advanced Level) Practice - 1051 Pop Sequence(25 分)
PAT (Advanced Level) Practice - 1051 Pop Sequence(25 分)
81 0
|
存储 测试技术
PAT (Advanced Level) Practice - 1131 Subway Map(30 分)
PAT (Advanced Level) Practice - 1131 Subway Map(30 分)
89 0
PAT (Advanced Level) Practice - 1062 Talent and Virtue(25 分)
PAT (Advanced Level) Practice - 1062 Talent and Virtue(25 分)
122 0
PAT (Advanced Level) Practice - 1127 ZigZagging on a Tree(30 分)
PAT (Advanced Level) Practice - 1127 ZigZagging on a Tree(30 分)
100 0
PAT (Advanced Level) Practice - 1139 First Contact(30 分)
PAT (Advanced Level) Practice - 1139 First Contact(30 分)
93 0
PAT (Advanced Level) Practice - 1060 Are They Equal(25 分)
PAT (Advanced Level) Practice - 1060 Are They Equal(25 分)
82 0