集合操作
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define int long long
using namespace std;
inline int read() {
int f = 1, ans = 0;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-')
f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
ans = ans * 10 + c - '0';
c = getchar();
}
return f * ans;
}
const int MAXN = 1e6 + 11;
int N, K, P, A[MAXN], Ans[MAXN];
int Get(int x) {
if (x >= 0)
return x / P;
return (x + 1) / P - 1;
}
priority_queue<int> que;
signed main() {
N = read(), K = read(), P = read();
for (int i = 1; i <= N; i++)
A[i] = read();
sort(A + 1, A + N + 1);
if (!P) {
for (int i = 1; i <= N; i++)
printf("%lld ", A[i]);
printf("\n");
return 0;
}
int now = 1;
//通过一个for循环把A数组里面的值都约束在一个p的区间内,求出边界值now
for (int i = N; i >= 2; i--) {
if (Get(A[i]) == Get(A[i - 1]))
continue;
int cnt = N - i + 1, del = Get(A[i]) - Get(A[i - 1]);
int sum = cnt * del;
if (K > sum) {
K -= sum;
continue;
}
now = i;
break;
}
int ee = Get(A[now]);
for (int i = now; i <= N; i++) {
int ss = Get(A[i]);
A[i] -= (ss - ee) * P;
}
int res = 0;
for (int i = 1; i <= N; i++)
if (Get(A[i]) == ee)
res++;
int t = K / res;
K %= res;
for (int i = 1; i <= N; i++)
if (Get(A[i]) == ee)
A[i] -= t * P;
for (int i = 1; i <= N; i++)
que.push(A[i]);
while (K--) {
int xx = que.top();
que.pop();
que.push(xx - P);
}
while (!que.empty()) {
Ans[++Ans[0]] = que.top();
que.pop();
}
for (int i = Ans[0]; i >= 1; i--)
printf("%lld ", Ans[i]);
printf("\n");
return 0;
}
/*
5 3 7
29 36 97 59 6
*/
题解: