Codeforces F. Maxim and Array (greedy configuration)

Subject description:

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 *a**i* either with *a**i + x* or with *a**i - 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. img) 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, ..., *a**n* (img) — the elements of the array found by Maxim.

Output

Print n integers b1, b2, ..., *b**n* in the only line — the array elements after applying no more than k operations to the array. In particular, img 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

Copy

5 3 1
5 4 3 5 2

Output

Copy

5 4 3 5 -1 

Input

Copy

5 3 1
5 4 3 5 5

Output

Copy

5 4 0 5 5 

Input

Copy

5 3 1
5 4 4 5 5

Output

Copy

5 1 4 5 5 

Input

Copy

3 2 7
5 4 2

Output

Copy

5 11 -5 

Ideas:

The subject is asked to give a series, k a number of operations on the plus or minus x, so that the product of the minimum number of columns. Since the number of positive and negative, to divide the discussion.

If a negative number is even now, is the product of a positive number, should make it into a negative phase approach, let the absolute value of the minimum change, because its distance from the nearest zero. If you want to change it is positive, decrements x, if you want to change is negative, then add x, if not let product number can also make the product smaller.

If the current negative number is odd, the product is a negative number, the absolute value of the product we want to make the product more smaller. Becomes the smallest absolute value, is to increase the number of positive x, is negative decrements x, because when the size of the pile closer to the number, the greater the number of the product of this pile.

Note that the number to be updated every time a change is negative, to quickly obtain the absolute value of the smallest element, to maintain a priority queue structure, we need to override <operator in the definition of the structure's.

At first I used two sand sculpture queue, a store integer, a negative deposit, but also relatively small absolute value of each election, \ (IF-the else \) writing a lot and finally wrong. See -_- || code behind.

Code:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
#define max_n 200005
using namespace std;
int n,k,x;
long long a[max_n];
struct node
{
    int id;
    long long val;
    bool operator<(const node& a) const
    {
        return abs(val-0)>abs(a.val-0);
    }
};
int cnt = 0;
priority_queue<node> que;
int main()
{
    cin >> n >> k >> x;
    for(int i = 0;i<n;i++)
    {
        cin >> a[i];
        node nw;
        nw.val = a[i];
        nw.id = i;
        if(a[i]<0)
        {
            cnt++;
        }
        que.push(nw);
    }
    while(k)
    {
        node nw = que.top();
        int id = nw.id;
        if(cnt%2==0)
        {
            if(a[id]<0)
            {
                a[id] += x;
                if(a[id]>=0)
                {
                    cnt--;
                }
            }
            else
            {
                a[id] -= x;
                if(a[id]<0)
                {
                    cnt++;
                }
            }
        }
        else
        {
            if(a[id]<0)
            {
                a[id] -= x;
            }
            else
            {
                a[id] += x;
            }
        }
        nw.val = a[id];
        que.pop();
        que.push(nw);
        k--;
    }
    for(int i = 0;i<n;i++)
    {
        cout << a[i] << " ";
    }
    cout << endl;
}

Where the wrong code is unknown:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
#define max_n 200005
using namespace std;
long long n,k,x;
long long a[max_n];
struct node
{
    int id;
    long long val;
    bool operator<(const node& a) const
    {
        return abs(val-0)>abs(a.val-0);
    }
};
priority_queue<node> fque;
priority_queue<node> zque;
int main()
{
    cin >> n >> k >> x;
    for(int i = 0;i<n;i++)
    {
        cin >> a[i];
        node nw;
        nw.val = a[i];
        nw.id = i;
        if(a[i]<0)
        {

            fque.push(nw);
        }
        else
        {
            zque.push(nw);
        }
    }
    //cout << "input " << endl;
    while(k)
    {
        /*for(int i = 0;i<n;i++)
        {
            cout << a[i] << " ";
        }
        cout << endl;*/
        if(fque.size()%2==0)
        {
            if(fque.size()==0)
            {
                int id = zque.top().id;
                a[id] -= x;
                node nw;
                nw.id = id;
                nw.val = a[id];
                if(a[id]<0)
                {
                    zque.pop();
                    fque.push(nw);
                }
                else
                {
                    zque.pop();
                    zque.push(nw);
                }
            }
            else if(zque.size()==0)
            {
                int id = fque.top().id;
                a[id] += x;
                node nw;
                nw.id = id;
                nw.val = a[id];
                if(a[id]>=0)
                {
                    fque.pop();
                    zque.push(nw);
                }
                else
                {
                    fque.pop();
                    fque.push(nw);
                }
            }
            else
            {
                int gapz = abs(zque.top().val-0);
                int idz = zque.top().id;
                int gapf = abs(fque.top().val-0);
                int idf = fque.top().id;
                if(gapz<gapf)
                {
                    a[idz] -= x;
                    node nw;
                    nw.id = idz;
                    nw.val = a[idz];
                    if(a[idz]<0)
                    {
                        zque.pop();
                        fque.push(nw);
                    }
                    else
                    {
                        zque.pop();
                        zque.push(nw);
                    }
                }
                else
                {
                    a[idf] += x;
                    node nw;
                    nw.id = idf;
                    nw.val = a[idf];
                    if(a[idf]>=0)
                    {
                        fque.pop();
                        zque.push(nw);
                    }
                    else
                    {
                        fque.pop();
                        fque.push(nw);
                    }
                }
            }
        }
        else
        {
            if(zque.size()==0)
            {
                int id = fque.top().id;
                a[id] -= x;
                node nw;
                nw.id = id;
                nw.val = a[id];
                fque.pop();
                fque.push(nw);
            }
            else
            {
                int gapz = abs(zque.top().val-0);
                int idz = zque.top().id;
                int gapf = abs(fque.top().val-0);
                int idf = fque.top().id;
                if(gapz<gapf)
                {
                    a[idz] += x;
                    zque.pop();
                    node nw;
                    nw.id = idz;
                    nw.val = a[idz];
                    zque.push(nw);
                }
                else
                {
                    a[idf] -= x;
                    fque.pop();
                    node nw;
                    nw.id = idf;
                    nw.val = a[idf];
                    fque.push(nw);
                }
            }
        }
        k--;
    }
    for(int i = 0;i<n;i++)
    {
        cout << a[i] << " ";
    }
    cout << endl;
    return 0;

}

Reference article:

Wood flow beasts of burden, D. Maxim and Array, https://www.cnblogs.com/thunder-110/p/9340279.html

Guess you like

Origin www.cnblogs.com/zhanhonhao/p/11365873.html