您的位置:首页 > 其它

hdu 5063(思路题-反向操作数组)

2016-07-09 23:00 399 查看

Operation the Sequence

Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 842 Accepted Submission(s): 288


[align=left]Problem Description[/align]
You have an array consisting of n integers: a1=1,a2=2,a3=3,…,an=n. Then give you m operators, you should process all the operators in order. Each operator is one of four types:
Type1: O 1 call fun1();
Type2: O 2 call fun2();
Type3: O 3 call fun3();
Type4: Q i query current value of a[i], this operator will have at most 50.
Global Variables: a[1…n],b[1…n];
fun1() {
index=1;
for(i=1; i<=n; i +=2)
b[index++]=a[i];
for(i=2; i<=n; i +=2)
b[index++]=a[i];
for(i=1; i<=n; ++i)
a[i]=b[i];
}
fun2() {
L = 1;R = n;
while(L<R) {
Swap(a[L], a[R]);
++L;--R;
}
}
fun3() {
for(i=1; i<=n; ++i)
a[i]=a[i]*a[i];
}

[align=left]Input[/align]
The first line in the input file is an integer T(1≤T≤20), indicating the number of test cases.
The first line of each test case contains two integer n(0<n≤100000), m(0<m≤100000).
Then m lines follow, each line represent an operator above.

[align=left]Output[/align]
For each test case, output the query values, the values may be so large, you just output the values mod 1000000007(1e9+7).

[align=left]Sample Input[/align]

1
3 5
O 1
O 2
Q 1
O 3
Q 1

[align=left]Sample Output[/align]

2
4
题意:一个数组经过一系列的变换后得到一个新数组。每次询问新数组的第 k 位的数字。
题解:从已知的位置开始反向推出当前数字最开始所在的位置。计数器用来计算数字已经累乘多少次了,用两个栈维护,反向操作即可。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long LL;

int main()
{
int tcase;
scanf("%d",&tcase);
while(tcase--)
{
int n,q;
scanf("%d%d",&n,&q);
char s[5];
int opr;
stack<int> stk;
stack<int> stk1;
int num = 1;
while(q--)
{
scanf("%s%d",s,&opr);
if(s[0]=='O')
{
if(opr==1||opr==2) stk.push(opr);
else num++;
}
else
{
LL ans;
while(!stk.empty())
{
int now = stk.top();
stk.pop();
stk1.push(now);
if(now==2) opr = n-opr+1;
if(now==1)
{
if(n%2==1)
{
if(opr<=n/2+1)  ///原来是奇数位
{
opr = 2*opr - 1;
}
else
{
opr = (opr - (n/2+1))*2;
}
}
else
{
if(opr<=n/2)
{
opr = 2*opr - 1;
}
else
{
opr = 2*(opr - n/2);
}
}
}
}
while(!stk1.empty()){
stk.push(stk1.top());
stk1.pop();
}
ans = opr;
for(int i=1; i<num; i++)
{
ans = ans*ans%1000000007;
}
printf("%lld\n",ans);
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: