首页 技术 正文
技术 2022年11月17日
0 收藏 570 点赞 4,259 浏览 1202 个字

题目https://pintia.cn/problem-sets/994805342720868352/problems/994805417945710592

题意:对一个栈进行push, pop和找中位数三种操作。

思路:

好久没写题。感觉傻逼题写多了稍微有点数据结构的都不会写了。

pop和push操作就不说了。

找中位数的话就二分去找某一个数前面一共有多少小于他的数,找到那个小于他的数刚好等于一半的。

找的过程中要用到前缀和,所以自然而然就应该上树状数组。

要注意树状数组的界应该是1e5而不是当前数的最大值。

 //#include<bits/stdc++>
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<stdlib.h>
#include<queue>
#include<map>
#include<stack>
#include<set> #define LL long long
#define ull unsigned long long
#define inf 0x7f7f7f7f using namespace std;
const int maxn = 1e5 + ;
int n, size;
int sum[maxn];
stack<int>sss;
int small = , big = 1e5; void add(int x, int val)
{
while(x < 1e5){
sum[x] += val;
x += (x & -x);
}
} int query(int x)
{
int ans = ;
while(x){
ans += sum[x];
x -= (x & -x);
}
return ans;
} int main()
{
scanf("%d", &n); while(n--){
string op;
cin>>op;
if(op == "Push"){
int key;
cin>>key;
sss.push(key);
//small = min(small, key);
//big = max(big, key);
add(key, );
}
else if(op == "Pop"){
if(sss.size() == ){
printf("Invalid\n");
}
else{
add(sss.top(), -);
printf("%d\n", sss.top());
sss.pop();
}
}
else if(op == "PeekMedian"){
if(sss.size() == ){
printf("Invalid\n");
}
else{
int st = small, ed = big;
int ans = ;
while(st <= ed){
int mid = (st + ed) / ;
size = sss.size();
int res = query(mid);
if(res < (size + ) / ){
st = mid + ;
}
else{
ans = mid;
ed = mid - ;
}
}
printf("%d\n", ans);
}
}
} return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,023
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,360
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,143
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,774
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,852