Codeforces Global Round 10 B. Omkar and Infinity Clock

Being stuck at home, Ray became extremely bored. To pass time, he asks Lord Omkar to use his time bending power: Infinity Clock! However, Lord Omkar will only listen to mortals who can solve the following problem:

You are given an array 𝑎 of 𝑛 integers. You are also given an integer 𝑘. Lord Omkar wants you to do 𝑘 operations with this array.

Define one operation as the following:

Set 𝑑 to be the maximum value of your array.
For every 𝑖 from 1 to 𝑛, replace 𝑎𝑖 with 𝑑−𝑎𝑖.
The goal is to predict the contents in the array after 𝑘 operations. Please help Ray determine what the final sequence will look like!

Input
Each test contains multiple test cases. The first line contains the number of cases 𝑡 (1≤𝑡≤100). Description of the test cases follows.

The first line of each test case contains two integers 𝑛 and 𝑘 (1≤𝑛≤2⋅105,1≤𝑘≤1018) – the length of your array and the number of operations to perform.

The second line of each test case contains 𝑛 integers 𝑎1,𝑎2,…,𝑎𝑛 (−109≤𝑎𝑖≤109) – the initial contents of your array.

It is guaranteed that the sum of 𝑛 over all test cases does not exceed 2⋅105.

Output
For each case, print the final version of array 𝑎 after 𝑘 operations described above.

Example
inputCopy
3
2 1
-199 192
5 19
5 -1 4 2 0
1 2
69
outputCopy
391 0
0 6 1 3 5
0
Note
In the first test case the array changes as follows:

Initially, the array is [−199,192]. 𝑑=192.

After the operation, the array becomes [𝑑−(−199),𝑑−192]=[391,0].

题意:
给一个序列,操作为选择序列的最大数d,把每个 a [ i ] a[i] a[i]替换成 d − a [ i ] d-a[i] da[i],求 k k k次替换后的序列。

思路:
可以发现,第一次替换后,最大值的位置会成为0。那么第二次替换0就会成为最大值,最大值位置就会成为0,所以最大值不会再改变。第一次操作后,每个位置的数在两个数字间轮流变换,所以只要看 k − 1 k-1 k1是奇数还是偶数就好了。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <unordered_map>
#include <bitset>

using namespace std;

typedef long long ll;
const int maxn = 2e5 + 7;
const int mod = 998244353;

int a[maxn];

int main() {
    int T;scanf("%d",&T);
    while(T--) {
        ll n,k;scanf("%lld%lld",&n,&k);
        int mx = -1e9;
        for(int i = 1;i <= n;i++) {
            scanf("%d",&a[i]);
            mx = max(mx,a[i]);
        }
        k--;
        for(int i = 1;i <= n;i++) {
            a[i] = mx - a[i];
        }
        mx = -1e9;
        for(int i = 1;i <= n;i++) {
            mx = max(mx,a[i]);
        }
        for(int i = 1;i <= n;i++) {
            if(k & 1) {
                a[i] = mx - a[i];
            }
            printf("%d ",a[i]);
        }
        printf("\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值