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

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(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.

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;
}
时间: 2024-12-09 23:32:06

hdu 5063(思路题-反向操作数组)的相关文章

hdu 4908(思路题)

BestCoder Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1614    Accepted Submission(s): 566 Problem Description Mr Potato is a coder.Mr Potato is the BestCoder. One night, an amazing

hdu 5101(思路题)

Select Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1599    Accepted Submission(s): 443 Problem Description One day, Dudu, the most clever boy, heard of ACM/ICPC, which is a very interesting

hdu 4859(思路题)

Goffi and Squary Partition Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1171    Accepted Submission(s): 402 Problem Description Recently, Goffi is interested in squary partition of integers.

hdu 5400(思路题)

Arithmetic Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1445    Accepted Submission(s): 632 Problem Description A sequence b1,b2,?,bn are called (d1,d2)-arithmetic sequence if and on

HDU 5301 思路题

给出n*m的矩阵,里面有一个坏点,不覆盖这个坏点的矩阵填满n*m的矩阵,使得这些矩阵的最大面积最小,并输出最小面积 先把矩阵转换为n<=m的矩阵,并把坏点通过镜像转移到左上方 ans=MAX(矩阵最中心点到两边距离的最小值,MIN(坏点下方的点到矩阵左端和下端的最小值)); #include "stdio.h" #include "string.h" int ans,n,m,x,y; int Max(int a,int b) { if (a<b) ret

【转载】 HDU 动态规划46题【只提供思路与状态转移方程】

1.Robberies 连接 :http://acm.hdu.edu.cn/showproblem.php?pid=2955 背包;第一次做的时候把概率当做背包(放大100000倍化为整数):在此范围内最多能抢多少钱  最脑残的是把总的概率以为是抢N家银行的概率之和- 把状态转移方程写成了f[j]=max{f[j],f[j-q[i].v]+q[i].money}(f[j]表示在概率j之下能抢的大洋); 正确的方程是:f[j]=max(f[j],f[j-q[i].money]*q[i].v)  其

hdu 5030 Rabbit&#39;s String(后缀数组)

题目链接:hdu 5030 Rabbit's String 题目大意:给定k和一个字符串,要求将字符串拆分成k个子串.然后将每个子串中字典序最大的子串选出来,组成一个包含k个字符串的集合,要求这个集合中字典序最大的字符串字典序最小. 解题思路:网赛的时候试图搞了一下这道题,不过水平还是有限啊,后缀数组也是初学,只会切一些水题.赛后看了一下别人的题解,把这题补上了. 首先对整个字符串做后缀数组,除了处理出sa,rank,height数组,还要处理处f数组,f[i]表示说以0~sa[i]开头共有多少

HDU 3854 Glorious Array(树状数组)

题意:给一些结点,每个结点是黑色或白色,并有一个权值.定义两个结点之间的距离为两个结点之间结点的最小权值当两个结点异色时,否则距离为无穷大.给出两种操作,一种是将某个结点改变颜色,另一个操作是询问当前距离小于K的结点有多少对,K是一个定值. 思路:先求最初时候小于k的结点有多少对,然后每次改变颜色的时候,统计该点左侧和右侧各有多少同色和异色的结点(这一步使用树状数组),分别处理就行.另外需要预处理离某个结点最近的两个距离小于K的结点的位置. 代码写的略乱. #include<cstdio> #

hdu 5030 Rabbit&#39;s String(后缀数组&amp;二分)

Rabbit's String Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 288    Accepted Submission(s): 108 Problem Description Long long ago, there lived a lot of rabbits in the forest. One day, the