1057. Stack (30)-PAT

本文探讨了在给定数据集上实现堆排序并优化中位数计算的方法,利用二叉索引树(Binary Indexed Tree)进行高效查找与更新操作,避免了传统排序方法在大规模数据集上的性能瓶颈。

1057. Stack (30)-中位数-BIT

时间限制
100 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard

Stack is one of the most fundamental data structures, which is based on the principle of Last In First Out (LIFO). The basic operations include Push (inserting an element onto the top position) and Pop (deleting the top element). Now you are supposed to implement a stack with an extra operation: PeekMedian -- return the median value of all the elements in the stack. With N elements, the median value is defined to be the (N/2)-th smallest element if N is even, or ((N+1)/2)-th if N is odd.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (<= 105). Then N lines follow, each contains a command in one of the following 3 formats:

Push  key
Pop
PeekMedian

where key is a positive integer no more than 105.

Output Specification:

For each Push command, insert key into the stack and output nothing. For each Pop or PeekMedian command, print in a line the corresponding returned value. If the command is invalid, print "Invalid" instead.

Sample Input:
17
Pop
PeekMedian
Push 3
PeekMedian
Push 2
PeekMedian
Push 1
PeekMedian
Pop
Pop
Push 5
Push 4
PeekMedian
Pop
Pop
Pop
Pop
Sample Output:
Invalid
Invalid
3
2
2
1
2
4
4
5
3
Invalid
推荐指数※※
来源:http://pat.zju.edu.cn/contests/pat-a-practise/1057
这道题如果不考虑时间限制,那是比较简单的,一开始就直接使用一个数组计数,使用二分法搜索,但都是超时。
这样只能用上统计累计频次比较好的Bianry Indexed Tree(http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=binaryIndexedTrees).
还有这里cin,cout还是不要用了,效率不如printf和scanf。
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
#define Maxval 100009
int tree[Maxval+1];
int stack[Maxval+1];
void update(int idx,int val){
	while(idx<Maxval){
		tree[idx]+=val;
		idx+=(idx&-idx);
	}
}
int read(int idx){
	int sum=0;
	while(idx>0){
		sum+=tree[idx];
		idx-=(idx&-idx);
	}
	return sum;
}
int find_cumulate(int cumfre){
	int low=0,high=Maxval,mid;
	while(low<high-1){
		if((low+high)&1==1)
			mid=(low+high-1)/2;
		else
			mid=(low+high)/2;
		int sum=read(mid);
		if(sum>=cumfre)
			high=mid;
		else
		  	low=mid;
	}
	return high;
}
int main(){
	int n,i;
	char str[20];
	scanf("%d",&n);
	long tmp,top;
	memset(tree,0,Maxval*sizeof(int));
	memset(stack,0,Maxval*sizeof(int));
	top=0;
	for(i=0;i<n;i++){
		scanf("%str",str);
		if(str[1]=='o'){//POP
			if(top>0){
				tmp=stack[top--];
				printf("%d\n",tmp);
				update(tmp,-1);
			}
			else
				printf("Invalid\n");
		}
		else if(str[1]=='u'){//push
			cin>>tmp;
			stack[++top]=tmp;
			update(tmp,1);
		}
		else if(str[1]=='e'){//media
			if(top>0){
				if(top&1==1)
					tmp=(top+1)/2;
				else
					tmp=top/2;
				tmp=find_cumulate(tmp);
				printf("%d\n",tmp);
			}
			else
				printf("Invalid\n");
		}
		else{
			printf("Invalid\n");
		}
	}
	return 0;
}

这是我超时的代码,比较的朴素
#include<iostream>
#include<string.h>
#include<string>
#include<stack>
#include<vector>
#include<set>
#include<algorithm>
#define  N 100000
#define  Hash_N 10000
using namespace std;
long find_median(long *arr,long first,long last,long val){
	long mid;
	while(first<last&&first!=0&&last!=Hash_N-1){
		mid=(first+last)/2;
		if(arr[mid]==val){
			return mid;
		}else if(arr[mid]>val)
			last=mid;
		else
			first=mid;
	}
	return first;
}
int main()
{
	long n,i,j;
	char str[20];
	long totalnum,num[N],hash_num[Hash_N];
	stack<long> st;
	cin>>n;
	totalnum=0;
	long tmp;
	memset(num,0,N*sizeof(int));
	memset(hash_num,0,Hash_N*sizeof(int));
	long fhash=N/Hash_N;
	for(i=0;i<n;i++){
		cin>>str;
		if(strcmp(str,"Pop")==0){//POP
			if(!st.empty()){
				tmp=st.top();
				cout<<tmp<<endl;
				st.pop();
				totalnum--;
				num[tmp]--;
				for(j=tmp/fhash;j<Hash_N;j++)
					hash_num[j]--;
			}
			else
				cout<<"Invalid"<<endl;
		}
		else if(strcmp(str,"Push")==0){//push
			cin>>tmp;
			st.push(tmp);
			totalnum++;
			num[tmp]++;
			hash_num[tmp/fhash]++;
		}
		else{//media
			if(!st.empty()){
				if(totalnum&1==1)
					tmp=(totalnum+1)/2;
				else
					tmp=totalnum/2;
				long tmp_sum=0,k;
				k=find_median(hash_num,0,Hash_N,tmp);
				if(k>0)
					tmp_sum=hash_num[k-1];
				else
					tmp_sum=0;
				for(j=k*fhash;j<N;j++){
					tmp_sum+=num[j];
					if(tmp_sum>=tmp){
						cout<<j<<endl;
						break;
					}
				}
			}
			else
				cout<<"Invalid"<<endl;
		}
	}
	return 0;
}


 
 
 
 
 
内容概要:本文系统阐述了采用二维时域有限差分法(2D FDTD)对光子晶体90度弯曲波导进行仿真研究的方法,利用Matlab编程实现了电磁波在该特殊结构中的传播特性分析。研究重点涵盖光场的空间分布、透射率与反射率等关键光学参数的数值模拟,旨在深入理解弯曲结构引起的传输损耗机制,并为高性能光子器件的设计与优化提供理论依据和技术支持。文中配套提供了完整的Matlab仿真代码,方便读者复现结果并进行二次开发与拓展研究。; 适合人群:具备电磁场与电磁波、光子学基础理论知识,以及熟练Matlab编程能力的研究生、科研人员和从事集成光学、光通信器件研发的工程技术人员。; 使用场景及目标:①掌握FDTD方法的基本原理及其在光子晶体波导仿真中的具体应用流程;②深入分析光子晶体90度弯道结构中的光传输损耗来源与模式转换机制;③通过亲手运行和调试仿真代码,提升对数值计算方法和光子器件设计的实践能力; 阅读建议:建议读者结合经典电磁理论与FDTD算法教材,仔细研读并逐行解析所提供的Matlab代码,特别关注空间网格剖分、时间步进迭代、周期性边界条件或完美匹配层(PML)的设置、高斯脉冲源的引入以及最终的光场和频谱可视化等核心环节,以期达到深刻理解仿真全过程并具备独立修改和构建类似模型的能力。
内容概要:本文是一份关于经济学期刊论文复现的研究资料,聚焦“数字化转型能否促进企业的高质量发展”这一核心命题,重点考察数字化转型对中国上市公司全要素生产率(TFP)的影响机制与实际效果。研究基于实证分析框架,采用固定效应模型(FE)、OP法、LP法、GMM等多种计量经济学方法测算企业TFP,并结合Matlab提供的完整代码、数据集及复现材料,系统还原论文的技术路径。内容涵盖变量构造、内生性处理、稳健性检验等关键环节,旨在帮助研究者深入理解数字化转型对企业生产效率的作用渠道及其经济含义。; 适合人群:具备扎实的经济学理论基础和计量分析能力,熟悉Matlab或Stata等统计软件的操作流程,适用于从事经济管理类研究的研究生、高校教师、科研院所研究人员及政策分析人员。; 使用场景及目标:①用于高水平学术论文的复现与方法验证,掌握企业层面全要素生产率的主流测算技术;②探究数字化转型提升企业高质量发展的内在机制与异质性效应;③支撑国家社科基金等课题申报、学位论文撰写以及实证经济学课程的教学实践。; 阅读建议:建议读者在学习过程中同步运行所提供的Matlab代码,对照原始数据逐步调试模型,重点关注TFP测算过程中的样本选择偏误、因果识别策略及工具变量构建等难点,以全面提升独立开展严谨实证研究的能力。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值