CodeForces - 721D Maxim and Array (greedy)

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 koperations 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 
 
Ideas:
If the number is even negative, find ways to let it become an odd number.
If you can not become odd, without any change to the next step. If so, control it just becomes odd number.
If in itself is odd, then it does not do any changes.
 
Next, k-step operation.
If the current negative number is odd, then find ways to make positive absolute values ​​of the possible large, every time the smallest absolute value plus the absolute value of x can be.
If the current negative number is even, then find ways to make a positive sum of the absolute may be small, the absolute value of each of the absolute value of the minimum minus x can be.
This operation uses a priority queue maintenance.
 
______________
(a-x) * b = a*b-b*x
Obviously <a superior when b> b when a
 
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>

#define fuck(x) cerr<<#x<<" = "<<x<<endl;
#define debug(a, x) cerr<<#a<<"["<<x<<"] = "<<a[x]<<endl;
#define ls (t<<1)
#define rs ((t<<1)|1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int loveisblue = 486;
const int maxn = 200086;
const int maxm = 100086;
const int inf = 0x3f3f3f3f;
const ll Inf = 999999999999999999;
const int mod = 1000000007;
const double eps = 1e-6;
const double pi = acos(-1);

int n,k;
struct  node{
    ll num,absnum;
    int id;
    bool operator<(const node &p)const{
        return p.absnum<absnum;
    }
}a[maxn];
priority_queue<node>q;
ll ans[maxn];
int main() {
//    ios::sync_with_stdio(false);
//    freopen("in.txt", "r", stdin);

    int n,k;
    ll x;
    scanf("%d%d%lld",&n,&k,&x);
    for(int i=1;i<=n;i++){
        ll num;
        scanf("%lld",&num);
        a[i]=node{num,abs(num),i};
    }
    sort(a+1,a+1+n);
    int fu = 0;
    for(int i=1;i<=n;i++){
        if(a[i].num<0){
            fu++;
        }
    }
    if(fu%2==0){
        if(x*k>=a[n].absnum){
            int p = min(1ll*k,a[n].absnum/x+1);
            k-=p;
            if(a[n].num<0){
                a[n].num+=p*x;
                a[n].absnum = abs(a[n].num);
            }else{
                a[n].num-=p*x;
                a[n].absnum = abs(a[n].num);
            }
        }
    }
    fu = 0;
    for(int i=1;i<=n;i++){
        if(a[i].num<0){
            fu++;
        }
    }
    for(int i=1;i<=n;i++){
        q.push(a[i]);
    }
    while (k--){
        node exa = q.top();
        q.pop();
        if(fu&1){
            if(exa.num<0){
                exa.num-=x;
                exa.absnum+=x;
            }else{
                exa.num+=x;
                exa.absnum+=x;
            }
        }else{
            if(exa.num<0){
                exa.num+=x;
                exa.absnum-=x;
            }else{
                exa.num-=x;
                exa.absnum-=x;
            }
        }
        q.push(exa);
    }
    while (!q.empty()){
        node exa = q.top();
        q.pop();
        ans[exa.id]=exa.num;
    }
    for(int i=1;i<=n;i++){
        printf("%lld ",ans[i]);
    }
    return 0;
}
View Code

Guess you like

Origin www.cnblogs.com/ZGQblogs/p/11307494.html