传送门
题意: 现有一个序列a,每次操作可以取整个序列的max,将每个元素变成max-a[i],试找到k次操作后的序列为多少。
思路:
- 由于n可达2e5,而k也能达到1e18,显然不能直接模拟来解决,需要找到规律。
- 若序列只有1个元素且k不为零,那么第一次操作后就变成了0。
- 若序列有负数,那么需要操作一次找到真正的最大值maxx,再考虑k的奇偶最后确定结果序列。
- 若序列最小值是正数,直接分奇偶讨论即可。
- 具体操作见代码。
代码实现:
#include<bits/stdc++.h>
#define endl '\n'
#define null NULL
#define ll long long
#define int long long
#define pii pair<int, int>
#define lowbit(x) (x &(-x))
#define ls(x) x<<1
#define rs(x) (x<<1+1)
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
using namespace std;
const int inf = 0x7fffffff;
const double PI = acos(-1.0);
const double eps = 1e-6;
const ll mod = 1e9 + 7;
const int N = 2e5 + 5;int t, n, k, a[N];signed main()
{IOS;cin >> t;while(t --){cin >> n >> k;int minn = inf, maxx = 0;for(int i = 1; i <= n; i ++){cin >> a[i];minn = min(minn, a[i]);}if(n==1 && k) cout << 0 << endl;else if(minn < 0){minn = -minn;for(int i = 1; i <= n; i ++){a[i] += minn;maxx = max(maxx, a[i]);}if(k%2) for(int i = 1; i <= n; i ++) cout << maxx-a[i] << " \n"[i==n];else for(int i = 1; i <= n; i ++) cout << a[i] << " \n"[i==n];}else{for(int i = 1; i <= n; i ++) maxx = max(maxx, a[i]);if(k%2) for(int i = 1; i <= n; i ++) cout << maxx-a[i] << " \n"[i==n];else{int maxx1 = 0;for(int i = 1; i <= n; i ++) a[i] = maxx-a[i], maxx1 = max(maxx1, a[i]);for(int i = 1; i <= n; i ++) cout << maxx1-a[i] << " \n"[i==n];}}}return 0;
}