#The 9th Hunan Collegiate Programming Contest#B.Boxes in a Line,双向链表的操作。。。
来源:互联网 发布:激活python2.7 知乎 编辑:程序博客网 时间:2024/05/18 21:39
题意就是给你1~n,n个数按顺序排好,然后4个操作,
t=1时,把x放到y左边;
t=2时,把x放到y右边;
t=3时,把x跟y换一下;
t=4时,整个数列倒序;
最后问从第一个数开始奇数位置的数之和
如果用链表,1,2,3,都容易实现,但是t=4时用链表怎么搞呢。。。
不用怕。。。倒序就相当于左右变换一下嘛,
那么,就容易解决啦~
#include<cstdio>#include<iostream>#include<cstring>#include<cstdlib>#include<cmath>#include<algorithm>#define LL long longusing namespace std;int a[2][200000];//a[0][x]表示x左边的数,a[1][x]表示x右边的数int n,m;void init(){ a[0][n+1]=n; for(int i=0;i<=n;i++) { a[0][i]=i-1; a[1][i]=i+1; }}void swapa(int x,int y){ if(a[1][x]==y) { int xl,yr; xl=a[0][x]; yr=a[1][y]; a[1][xl]=y; a[0][y]=xl; a[1][y]=x; a[0][x]=y; a[1][x]=yr; a[0][yr]=x; return; } if(a[0][x]==y) { int xr,yl; xr=a[1][x]; yl=a[0][y]; a[1][yl]=x; a[0][x]=yl; a[1][x]=y; a[0][y]=x; a[1][y]=xr; a[0][xr]=y; return; } int xr,xl,yr,yl; xr=a[1][x]; xl=a[0][x]; yr=a[1][y]; yl=a[0][y]; a[0][x]=yl; a[1][x]=yr; a[0][yr]=x; a[1][yl]=x; a[0][y]=xl; a[1][y]=xr; a[0][xr]=y; a[1][xl]=y;}void turn(int x,int y,int p){ if(a[p][y]==x) return; int xr,xl,yp; xr=a[1][x]; xl=a[0][x]; yp=a[p][y]; a[0][xr]=xl; a[1][xl]=xr; a[1-p][yp]=x; a[p][y]=x; a[1-p][x]=y; a[p][x]=yp;}int main(){ //freopen("1.in","r",stdin); int cas=1; while(scanf("%d %d",&n,&m)==2) { int t,p=0; int x,y; init(); while(m--) { scanf("%d",&t); if(t==4) p=1-p; else { scanf("%d %d",&x,&y); if(t==1) turn(x,y,p); if(t==2) turn(x,y,1-p); if(t==3) swapa(x,y); } } if((n&1)) p=0; x=a[1][0]; if(p) x=a[0][n+1]; t=1; LL ans=0; while(t<=n) while(x>0 && x<=n) { //printf("x=%d,t=%d\n",x,t); if(t&1) ans+=(LL)x; t++; x=a[1-p][x]; } printf("Case %d: %lld\n",cas++,ans); } return 0;}
- #The 9th Hunan Collegiate Programming Contest#B.Boxes in a Line,双向链表的操作。。。
- The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple(A+B+C+D)
- ZOJ-The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple-A~B
- The 12th Zhejiang Provincial Collegiate Programming Contest - A
- The 9th Zhejiang Provincial Collegiate Programming Contest->Problem A:A - Taxi Fare
- The 9th Zhejiang Provincial Collegiate Programming Contest->Problem A:A - Taxi Fare
- 一二三(The Seventh Hunan Collegiate Programming Contest)
- 盒子游戏(The Seventh Hunan Collegiate Programming Contest)
- The Seventh Hunan Collegiate Programming Contest 盒子游戏
- The Seventh Hunan Collegiate Programming Contest 一二三
- 【组队补题题】The Seventh Hunan Collegiate Programming Contest Semilive
- The 9th Zhejiang Provincial Collegiate Programming Contest
- The 9th Zhejiang Provincial Collegiate Programming Contest
- (双向链表之数组模拟)Boxes in a Line,第九届湖南省赛,B题
- UVA12657 Boxes in a Line【双向链表】【数组模拟】
- UVA - 12657 Boxes in a Line(双向链表)
- UVA 12657 Boxes in a Line 【双向链表】
- Uva-12657 Boxes in a Line(双向链表)
- STL--迭代器
- PHP高效编程53个方法
- 说说我在家乡山东日照的面试经历以及对家乡互联网产业的一些认识吧
- cg编程之-RayCast实现
- Spring MVC之@RequestMapping 详解
- #The 9th Hunan Collegiate Programming Contest#B.Boxes in a Line,双向链表的操作。。。
- Erlang/OTP 监督者(Supervisor)
- 阿里巴巴实习生招聘骰子题目
- 自己写Lucene分词器原理篇——CJKAnalyzer简单讲解
- note : Web browser
- 18-GridView笑脸显示
- 数值计算——矩阵求逆误差测试(n=2000)
- Spring MVC之@RequestParam @RequestBody @RequestHeader 等详解
- Palindrome Partitioning