栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

zoj 2603 Railroad Sort

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

zoj 2603 Railroad Sort

#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>using namespace std;bool output_flag = false;void outputs(int i,int t){ while(t--){ if(output_flag) cout<<' '<<i; else cout<<i; output_flag = true; }}class MyStack{ public: int n,d[10000]; MyStack(); bool Empty(); void Push(int i); int Pop(); int View();};MyStack::MyStack(){ memset(d,0,sizeof(d)); n = 0;}bool MyStack::Empty(){ return n <= 0;}void MyStack::Push(int i){ n++; d[n] = i;}int MyStack::Pop(){ n--; return d[n+1];}int MyStack::View(){ return d[n];}int mainw(long xx){ output_flag = false; long stack_count,i,r,maxn,lis[10000],sorted[10000],tr,sr; MyStack stacks[15]; stack_count = xx; maxn = (1<<stack_count); for(i=0;i<maxn;i++){ cin>>lis[i]; sorted[i] = lis[i]; } sort(sorted,sorted+maxn); r = 0; sr = 0; while(r<maxn){ tr = r; for(i=1;i<=stack_count;i++){ if(stacks[i].View() == sorted[r]){ stacks[i].Pop(); outputs(sorted[r],(stack_count - i)*2+1); r++; break; } } if(tr != r) continue; while(lis[sr] != sorted[r]){ for(i=1;i<=stack_count;i++){ if(stacks[i].Empty() || stacks[i].View()>lis[sr]){ outputs(lis[sr],i*2-1); stacks[i].Push(lis[sr]); sr++; break; } } } sr++; outputs(sorted[r],stack_count*2); r++; } return 0;}int main(){ long t; cin>>t; while(t!=0){ mainw(t); cout<<endl; cin>>t; }}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/377612.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号