CodeForces - 721D Maxim and Array (贪心思维+模拟)

Maxim and Array

 

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 

 

题目链接:http://codeforces.com/problemset/problem/721/D

题目大意:n,k,x,长度为n的数列,可以操作k次,每次操作可以把数组中的一个数加上x或者减去x。问k次操作后的最小的数列乘积 。

思路:首先我们知道乘积为负数肯定比正数小,所以我们先把乘积尽可能调成负数。

1.如果数组中的负数为偶数个,那就把数组中绝对值最小的数的符号改变,使负数变成奇数个。如果不能改变其符号,就把这个数的绝对值减到最小,得到的乘积就是最小的。

2.数组中的负数为奇数个,把数组中绝对值最小的数的绝对值增加x,不改变符号

这样处理k次后得到的乘积一定是最小的。

其中最关键的就是最小绝对值的维护,比赛的时候整体思路都想出来了,但是一直没想到怎么维护最小的绝对值。

可能平时都没怎么用优先队列,所以一直没想到。

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
#define ll long long
const int N=200005;
ll e[N];
struct node
{
    ll id,a,b;
    node(ll _id=0,ll _a=0,ll _b=0):id(_id),a(_a),b(_b){}
    bool operator<(const node &r)const
    {
        return b>r.b;
    }
};
int main()
{
    ll n,k,x;
    scanf("%lld%lld%lld",&n,&k,&x);
    ll y,z;
    priority_queue<node>q;
    int ans=0;
    for(ll i=1;i<=n;i++)
    {
        scanf("%lld",&y);
        z=y;
        if(y<0)
        {
            ans++;
            z=-y;
        }
        q.push(node(i,y,z));
    }
    node tmp;
    if(ans%2==0)
    {
        tmp=q.top();
        q.pop();
        y=tmp.b;
        z=y/x+1;
        if(z<=k)  //如果可以把绝对值最小的数的符号改变
        {
            y=z*x-y;
            if(tmp.a>=0) tmp.a=-y;
            else tmp.a=y;
            tmp.b=y;
            q.push(tmp);
            k-=z;
        }
        else if(z>k) //如果不可以,就把这个数的绝对值减到最小
        {
            y=y-k*x;
            if(tmp.a>=0) tmp.a=y;
            else tmp.a=-y;
            tmp.b=y;
            q.push(tmp);
            k=0;
        }
    }
    while(k--) //把绝对值最小的数,绝对值+x
    {
        tmp=q.top();
        q.pop();
        tmp.b+=x;
        if(tmp.a<0) tmp.a=-tmp.b;
        else tmp.a=tmp.b;
        q.push(tmp);
    }
    while(!q.empty())
    {
        tmp=q.top();
        q.pop();
        e[tmp.id]=tmp.a;
    }
    for(int i=1;i<=n;i++)
        printf("%lld%c",e[i],i==n?'\n':' ');
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值