2018暑期SICNU-ACM组集训报告(2)

题目:
Mishka got an integer array aa of length nn as a birthday present (what a surprise!).

Mishka doesn't like this present and wants to change it somehow. He has invented an algorithm and called it "Mishka's Adjacent Replacements Algorithm". This algorithm can be represented as a sequence of steps:

Replace each occurrence of 1 in the array aa with 2;
Replace each occurrence of 2 in the array aa with 1;
Replace each occurrence of 3 in the array aa with 4;
Replace each occurrence of 4 in the array aa with 3;
Replace each occurrence of 5 in the array aa with 6;
Replace each occurrence of 6 in the array aa with 5;
……
Replace each occurrence of 10^9−1 in the array aa with 10^9;
Replace each occurrence of 10^9 in the array aa with 10^9−1.
Note that the dots in the middle of this algorithm mean that Mishka applies these replacements for each pair of adjacent integers (2i−1,2i2i−1,2i) for each i∈{1,2,…,5⋅108}i∈{1,2,…,5⋅108} as described above.

For example, for the array a=[1,2,4,5,10]a=[1,2,4,5,10], the following sequence of arrays represents the algorithm:

[1,2,4,5,10][1,2,4,5,10] →→ (replace all occurrences of 1 with 2) →→ [2,2,4,5,10][2,2,4,5,10] →→ (replace all occurrences of 2 with 1) →→ [1,1,4,5,10][1,1,4,5,10] →→ (replace all occurrences of 3 with 4) →→ [1,1,4,5,10][1,1,4,5,10] →→ (replace all occurrences of 4 with 3) →→ [1,1,3,5,10][1,1,3,5,10] →→ (replace all occurrences of 5 with 6) →→ [1,1,3,6,10][1,1,3,6,10] →→ (replace all occurrences of 6 with 5) →→ [1,1,3,5,10][1,1,3,5,10] →→ …… →→ [1,1,3,5,10][1,1,3,5,10] →→ (replace all occurrences of 10 with 9) →→ [1,1,3,5,9][1,1,3,5,9]. The later steps of the algorithm do not change the array.

Mishka is very lazy and he doesn't want to apply these changes by himself. But he is very interested in their result. Help him find it.

Input
The first line of the input contains one integer number nn (1≤n≤10001≤n≤1000) — the number of elements in Mishka's birthday present (surprisingly, an array).

The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the elements of the array.

Output
Print nn integers — b1,b2,…,bnb1,b2,…,bn, where bibi is the final value of the ii-th element of the array after applying "Mishka's Adjacent Replacements Algorithm" to the array aa. Note that you cannot change the order of elements in the array.

Examples
Input
5
1 2 4 5 10
Output
1 1 3 5 9
Input
10
10000 10 50605065 1 5 89 5 999999999 60506056 1000000000
Output
9999 9 50605065 1 5 89 5 999999999 60506055 999999999
Note
The first example is described in the problem statement.

[原题链接]:http://codeforces.com/problemset/problem/1006/A

题意:把规定长度数组中的值按照1→2、2→1、3→4、4→3的规律进行交换,以此类推到10^9 →10^9−1 .

解题思路:乍一看上去是有一点点想要暴力的意思,但是我觉得遍历完可能就超时了,所以我选择看看规律。

不出所料的,在仔细观察后,得到如下规律:数组中奇数不变,偶数减一。这下就是一道水题了,不过关键还是思路,不能只想着暴力破解

AC代码:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int a[1000],n,i;
    cin>>n;
    for(i=0;i<n;i++){
        cin>>a[i];
        if(a[i]%2==0){
            a[i]=a[i]-1;
        }
    }
    for(i=0;i<n;i++){
        cout<<a[i]<<" ";
    }
    return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,448评论 0 10
  • 惯性思维, 是亲友在我们记忆中对应的标签。 当互动中一旦发生交错式沟通, 我们会不假思索, 马上用这个标签去定义T...
    守望者22阅读 170评论 4 1
  • 今天你又做了什么?计划?目标?行动? 一转眼今天是2008年的一月八号了,早上十一点多了…… ...
    諄儿阅读 152评论 0 0