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

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 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.

Input

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.

Output

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.

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值