Codeforces Round #374 (Div. 2) D - Maxim and Array

来源:互联网 发布:预测算法有哪些 编辑:程序博客网 时间:2024/05/21 15:01
D. Maxim and Array
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 integerx and decided to add or subtract it from arbitrary array elements. Formally, by applying single operation Maxim chooses integeri (1 ≤ i ≤ n) and replaces thei-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 thank operations to it. Please help him in that.

Input

The first line of the input contains three integers n, k andx (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.

Output

Print n integers b1, b2, ..., bn in the only line — the array elements after applying no more thank operations to the array. In particular, should stay true for every 1 ≤ i ≤ n, but the product of all array elements should beminimum possible.

If there are multiple answers, print any of them.

Examples
Input
5 3 15 4 3 5 2
Output
5 4 3 5 -1 
Input
5 3 15 4 3 5 5
Output
5 4 0 5 5 
Input
5 3 15 4 4 5 5
Output
5 1 4 5 5 
Input
3 2 75 4 2
Output
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 __int64struct 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;}



0 0
原创粉丝点击