codechef Fun with Rotation
来源:互联网 发布:hadoop大数据平台架构 编辑:程序博客网 时间:2024/04/28 05:12
Problem Description
You are given an array A of N integers. You are to fulfill M queries. Each query has one of the following three types:
- C d : Rotate the array A clockwise by d units.
- A d : Rotate the array A anticlockwise by d units.
- R d : Query for the value of the element, currently being the d-th in the array A.
Input
The first line contains two numbers - N and M respectively.
The next line contains N space separated Integers, denoting the array A.
Each of the following M lines contains a query in the one of the forms described above.
Output
For each query of type R output the answer on a separate line.
Constraints
- 1 ≤ N ≤ 100000
- 1 ≤ M ≤ 100000
- 1 ≤ d ≤ N, in all the queries
- 1 ≤ elements of A ≤ 1000000
- The array A and the queries of the type R are 1-based.
Example
Input:5 55 4 3 3 9R 1C 4R 5A 3R 2Output:533
Explanation
The initial array : 5 4 3 3 9
The answer for R 1 : 5
The array after C 4 : 9 5 4 3 3
The answer for R 5 : 3
The array after A 3 : 4 3 3 9 5
The answer for R 2 : 3
题解
话说这道题还蛮容易让人想到链表的,看这数据范围,链表很虚啊。有时候就是这么简单。
#include<cstdio>#include<cstring>#include<cstdlib>#include<iostream>#include<cmath>#include<algorithm>using namespace std;int m,n,a[100002];char ch[3];void init(){scanf("%d%d",&n,&m);int i;for(i=1;i<=n;i++) scanf("%d",&a[i]);}void work(){int i,lr=0,rr=0,x,st=1,w;for(i=1;i<=m;i++) {scanf("%s%d",ch,&x); if(ch[0]=='C') lr+=x; else if(ch[0]=='A') rr+=x; else {if(lr>rr) {w=(lr-rr)%n; st=(st+w)%n; if(st==0) st=n; }else {w=(rr-lr)%n; st=(st+n-w)%n; if(st==0) st=n; }w=(st+x-1)%n;if(w==0) w=n;printf("%d\n",a[w]);lr=rr=0; } }}int main(){init(); work();return 0;}
0 0
- codechef Fun with Rotation
- SoTimerSensor with rotation animation
- Fun with layer
- Have Fun with Numbers
- Fun with Ettercap Filters
- Fun With Ettercap
- Fun with pointer!
- Have Fun with Numbers
- Have Fun with Numbers
- ios rotation animation with CGAffineTransformRotate
- A bit of fun: fun with bits
- A bit of fun: fun with bits
- Have Fun with Microsoft Popfly
- Working with Fun and Interests
- 1023. Have Fun with Numbers
- 1023. Have Fun with Numbers
- PAT_1023: Have Fun with Numbers
- 1023. Have Fun with Numbers
- 加州理工学院公开课:机器学习与数据挖掘_神经网络(第十课)
- php函数
- 网站自动登录功能的设计
- HDU 2089【数位DP】
- Effective C++ 34
- codechef Fun with Rotation
- 一个开始
- KMP next[]小贴士
- hdu 2899 hdu 3400 三分/几何
- 小白学c++之effective c++条款12实现代码
- Win32 下 使用CMarkup类遇到的问题
- 面向对象的PHP(一):向类中增加新的public属性
- 【DP】 HDOJ 3516 Tree Construction
- 数据结构之线性表