【codeforces 721D】【贪心 堆】D. Maxim and Array 【给出n个数,k次机会,每次使得任意一个数字减少或者加上x,使得最后的乘积最小,最后的n个数大小】

传送门: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个数,每个数是多少。

思路:

贪心思路是讨论这列数中负数的个数,如果为偶数,那么把数列中绝对值最小的数使其往0的方向前进。
如果为奇数,同样选择绝对值最小的数,使其往背离0的方向前进。

实现需要基本功,做题时需要思路清晰。

代码:

#include <bits/stdc++.h>
#define pr(x) cout << #x << "= " << x << "  " ;
#define pl(x) cout << #x << "= " << x << endl;
#define ll __int64
using  namespace  std;

template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
    for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
    F && (num=-num);
}

const int N=2e5+10;

struct node{
  ll num;
  int id;
  bool operator < (const node& tmp)const{
    return abs(num)>abs(tmp.num);
  }
};
priority_queue<node> q;
ll a[N];
int n,k;
ll x;

int  main(){
/*  #ifndef ONLINE_JUDGE
  freopen("in.txt","r",stdin);
  #endif*/

  read(n);read(k);read(x);
  int flag=1;
  for(int i=1; i<=n; i++){
    read(a[i]);
    if(a[i]<0)flag=-flag;//偶数个负数为1,奇数个为-1
    q.push((node){a[i],i});
  }
  while(k--){
    node tmp=q.top();q.pop();
    if(a[tmp.id]<0){
      if(flag==-1)a[tmp.id]-=x;
      else a[tmp.id]+=x;
      if(a[tmp.id]>=0)flag=-flag;
    }
    else{
      if(flag==-1)a[tmp.id]+=x;
      else a[tmp.id]-=x;
      if(a[tmp.id]<0)flag=-flag;
    }
    q.push((node){a[tmp.id],tmp.id});
  }
  for(int i=1; i<=n; i++){
    printf("%I64d%c",a[i],i==n?'\n':' ');
  }
  return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值