PAT Advanced Level 1057
来源:互联网 发布:域名后缀产生 编辑:程序博客网 时间:2024/05/24 06:36
题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1057
解题感悟:主要解决超时问题。参考网络大神解题报告,使用到了树形数组,膜拜
代码如下:
#include<stdio.h>#include<cstring>#include<iostream>#include<string>using namespace std;const int N=100005;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){int res=0;while(pos>0){res+=c[pos];pos-=lowbit(pos);}return res;}int find(int value){int l=0,r=N-1,median,res;while(l<r-1){if((l+r)%2==0)median=(l+r)/2;elsemedian=(l+r-1)/2;res=sum(median);if(res<value)l=median;elser=median;}return l+1;}int main(){char ss[20];int stack[N],top=0,n,pos;memset(c,0,sizeof(c));scanf("%d",&n);while(n--){scanf("%s",ss);if(ss[1]=='u'){scanf("%d",&pos);stack[++top]=pos;add(pos,1);}else if(ss[1]=='o'){if(top==0){printf("Invalid\n");continue;}int out=stack[top];add(out,-1);printf("%d\n",stack[top--]);}else if(ss[1]=='e'){if(top==0){printf("Invalid\n");continue;}int res;if(top%2==0)res=find(top/2);elseres=find((top+1)/2);printf("%d\n",res);}else{printf("Invalid\n");}}return 0;}
0 0
- PAT Advanced Level 1057
- Pat(Advanced Level)Practice--1057(Stack)
- PAT (Advanced Level) Practise 1057 Stack (30)
- PAT (Advanced Level) Practise 1057 Stack (30)
- PAT Advanced Level 1077
- PAT Advanced Level 1078
- PAT Advanced Level 1079
- PAT Advanced Level 1080
- PAT Advanced Level 1010
- PAT Advanced Level 1016
- PAT Advanced Level 1018
- PAT Advanced Level 1039
- PAT Advanced Level 1060
- PAT Advanced Level 1026
- PAT Advanced Level 1029
- PAT Advanced Level 1030
- PAT Advanced Level 1033
- PAT Advanced Level 1034
- 递推——Tiling
- distinct用法和优化
- 【C++】基类和派生类构造函数的调用顺序
- 2-0. 整数四则运算(10)
- 如何在oracle中导入dmp数据库文件
- PAT Advanced Level 1057
- webStroe 快捷键
- 2-1. 厘米换算英尺英寸(15)
- eclipse4.2集成github
- 阿士大夫阿士大夫
- 2-2. 然后是几点(15)
- python 使用sqlite3
- postgres 之 initdb 源码分析 四
- hdu 2066 一个人的旅行(最短路)