zoukankan      html  css  js  c++  java
  • 【codeforces 764B】Timofey and cubes

    time limit per test1 second
    memory limit per test256 megabytes
    inputstandard input
    outputstandard output
    Young Timofey has a birthday today! He got kit of n cubes as a birthday present from his parents. Every cube has a number ai, which is written on it. Timofey put all the cubes in a row and went to unpack other presents.

    In this time, Timofey’s elder brother, Dima reordered the cubes using the following rule. Suppose the cubes are numbered from 1 to n in their order. Dima performs several steps, on step i he reverses the segment of cubes from i-th to (n - i + 1)-th. He does this while i ≤ n - i + 1.

    After performing the operations Dima went away, being very proud of himself. When Timofey returned to his cubes, he understood that their order was changed. Help Timofey as fast as you can and save the holiday — restore the initial order of the cubes using information of their current location.

    Input
    The first line contains single integer n (1 ≤ n ≤ 2·105) — the number of cubes.

    The second line contains n integers a1, a2, …, an ( - 109 ≤ ai ≤ 109), where ai is the number written on the i-th cube after Dima has changed their order.

    Output
    Print n integers, separated by spaces — the numbers written on the cubes in their initial order.

    It can be shown that the answer is unique.

    Examples
    input
    7
    4 3 7 6 9 1 2
    output
    2 3 9 6 7 1 4
    input
    8
    6 1 4 2 5 6 9 2
    output
    2 1 6 2 5 4 9 6
    Note
    Consider the first sample.

    At the begining row was [2, 3, 9, 6, 7, 1, 4].
    After first operation row was [4, 1, 7, 6, 9, 3, 2].
    After second operation row was [4, 3, 9, 6, 7, 1, 2].
    After third operation row was [4, 3, 7, 6, 9, 1, 2].
    At fourth operation we reverse just middle element, so nothing has changed. The final row is [4, 3, 7, 6, 9, 1, 2]. So the answer for this case is row [2, 3, 9, 6, 7, 1, 4].

    【题目链接】:http://codeforces.com/contest/764/problem/B

    【题解】

    如果从初始数列到目标数列
    操作依次为
    [1..倒1]翻转
    [2..倒2]翻转
    [3..倒3]翻转

    [i..倒i]翻转
    前面的翻转区间覆盖了后面的翻转区间;
    所以如果是偶数次翻转,那么翻转的区间的两端那个元素是不会改变顺序的;还是原来的位置;
    而如果是奇数次翻转,两端的元素就会交换位置;
    根据这个规则处理出原序列就好;
    (reverse函数和sort函数都是左闭右开区间…..)

    【完整代码】

    #include <bits/stdc++.h>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define mp make_pair
    #define pb push_back
    #define fi first
    #define se second
    #define rei(x) scanf("%d",&x)
    #define rel(x) scanf("%I64d",&x)
    
    typedef pair<int,int> pii;
    typedef pair<LL,LL> pll;
    
    const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
    const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
    const double pi = acos(-1.0);
    const int MAXN = 2e5+100;
    
    int a[MAXN];
    int n;
    
    int main()
    {
        //freopen("F:\rush.txt","r",stdin);
        rei(n);
        rep1(i,1,n)
            rei(a[i]);
        int cnt = 0;
        for (int i = 1;i<=n;i++)
        {
            int j = n-i+1;
            if (i <= j)
            {
                cnt++;
                if (cnt&1)
                    swap(a[i],a[j]);
            }
        }
        rep1(i,1,n)
            {
                printf("%d",a[i]);
                if (i==n)
                    puts("");
                else
                    putchar(' ');
            }
        return 0;
    }
  • 相关阅读:
    css3实现轮播2
    css3实现轮播1
    读阮一峰ES6笔记4:字符串的新增方法
    读阮一峰ES6笔记3:字符串的扩展
    应用流策略与检查配置结果
    配置流策略
    配置流行为
    配置流分类
    "流量监管"和"流量整形"的区别
    802.1p 优先级与内部优先级的映射关系
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626641.html
Copyright © 2011-2022 走看看