传送门
B. Omkar and Infinity Clock
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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 a of n integers. You are also given an integer k. Lord Omkar wants you to do k operations with this array.Define one operation as the following: Set d to be the maximum value of your array.
For every i from 1 to n, replace ai with d−ai The goal is to predict the contents in the array after k 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 t(1≤t≤100). Description of the test cases follows.The first line of each test case contains two integers n and k (1≤n≤2⋅105,1≤k≤1018) – the length of your array and the number of operations to perform. The second line of each test case contains n integers a1,a2,…,an (−109≤ai≤109)– the initial contents of your array.It is guaranteed that the sum of n over all test cases does not exceed 2⋅105
Output
For each case, print the final version of array a after k operations described above.
Example
Input
3
2 1
-199 192
5 19
5 -1 4 2 0
1 2
69
Output
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] d=192After the operation, the array becomes [d−(−199),d−192]=[391,0]
题意:输入一个n,k和一个a序列,你可以进行操作,计算出a中的最大值d,然后ai=d−ai,进行k次这样的操作,求出最后的a序列。
思路:多写几组发现这个序列是交替的,最终只会有两组数组出现,只要根据K的奇偶性,来判断应该输出哪组数组即可。
AC代码
#include<bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 200010;
typedef long long ll;
typedef long double ld;
ll a[maxn],b[maxn];
int main()
{
ll t,n,k;
ll i,j;
cin>>t;
while(t--)
{
cin>>n>>k;
ll m=-1000000100;
for(i=1;i<=n;i++)
{
cin>>a[i];
m=max(m,a[i]);
}
ll f=-1000000100;
for(i=1;i<=n;i++)
{
a[i]=m-a[i];
f=max(f,a[i]);
}
for(i=1;i<=n;i++)
{
b[i]=f-a[i];
}
if(k%2==1)
{
for(i=1;i<=n;i++)
{
if(i!=1)
cout<<" "<<a[i];
else
cout<<a[i];
}
}
else
{
for(i=1;i<=n;i++)
{
if(i!=1)
cout<<" "<<b[i];
else
cout<<b[i];
}
}
cout<<endl;
}
}