码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-07-09 23:42:03      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

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


Problem Description
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];
}
 

 

Input
The first line in the input file is an integer T(1T20), indicating the number of test cases.
The first line of each test case contains two integer n(0<n100000), m(0<m100000).
Then m lines follow, each line represent an operator above.
 

 

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

 

Sample Input
1 3 5 O 1 O 2 Q 1 O 3 Q 1
 

 

Sample Output
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;
}

 

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

标签:

原文地址:http://www.cnblogs.com/liyinggang/p/5656833.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!