Recently Maxim has found an array of n integers, needed by no one. He immediately come up with idea of changing it: he invented positive integer x and decided to add or subtract it from arbitrary array elements. Formally, by applying single operation Maxim chooses integer i (1 ≤ i ≤ n) and replaces the i-th element of array ai either with ai + x or with ai - x. Please note that the operation may be applied more than once to the same position.
Maxim is a curious minimalis, thus he wants to know what is the minimum value that the product of all array elements (i.e. ) can reach, if Maxim would apply no more than k operations to it. Please help him in that.
The first line of the input contains three integers n, k and x (1 ≤ n, k ≤ 200 000, 1 ≤ x ≤ 109) — the number of elements in the array, the maximum number of operations and the number invented by Maxim, respectively.
The second line contains n integers a1, a2, ..., an () — the elements of the array found by Maxim.
Print n integers b1, b2, ..., bn in the only line — the array elements after applying no more than k operations to the array. In particular, should stay true for every 1 ≤ i ≤ n, but the product of all array elements should be minimum possible.
If there are multiple answers, print any of them.
5 3 1 5 4 3 5 2
5 4 3 5 -1
5 3 1 5 4 3 5 5
5 4 0 5 5
5 3 1 5 4 4 5 5
5 1 4 5 5
3 2 7 5 4 2
5 11 -5
题意:
有n个数,有k次操作,每次操作能选一个数+或者-x;
要求n个数的乘积最小;
思路:
贪心;
当负数为偶数个数的时候:
绝对值最小的值为正数: -x
绝对值最小的值为负数: +x
当负数为奇数个数的时候:
绝对值最小的值为正数: +x
绝对值最小的值为负数: -x
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define LL __int64
struct node {
int id;
LL num;
bool operator < (const node &a)const{
return abs(num)>abs(a.num);
}
};
priority_queue <node>Q;
const int maxn =200005;
LL data[maxn];
int main(){
int n,k,x;
scanf("%d%d%d",&n,&k,&x);
int i,j;
int flag=1;
for(i=1;i<=n;i++){
scanf("%I64d",&data[i]);
if(data[i]<0)
flag=-flag;
Q.push( (node){i,data[i]} );
}
for(i=1;i<=k;i++){
node nflag=Q.top();
Q.pop();
if(nflag.num<0){
if(flag<0){
nflag.num-=x;
data[nflag.id]-=x;
}
else{
nflag.num+=x;
data[nflag.id]+=x;
if(nflag.num>=0)
flag*=-1;
}
}
else{
if(flag<0){
nflag.num+=x;
data[nflag.id]+=x;
}
else{
nflag.num-=x;
data[nflag.id]-=x;
if(nflag.num<0)
flag*=-1;
}
}
Q.push(nflag);
}
for(i=1;i<=n;i++)
printf("%I64d ",data[i]);
printf("\n");
return 0;
}