zoukankan      html  css  js  c++  java
  • 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(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
    ",ans);
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    安装apache服务
    基于mysqld_multi实现MySQL 5.7.24多实例多进程配置
    linux安装lolcat实现彩色文字输出信息
    haproxy+keepalived实现高可用
    LVS DR模拟实验
    nginx+keepalived实现高可用
    cpu相关信息查看
    LVS集群
    session之memcache
    tomcat之redis
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5656833.html
Copyright © 2011-2022 走看看