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或者-x,使得成绩最小;

思路:每次寻找绝对值最小的那个数,判断负数的个数,进行+x或者-x;

   ps:优先队列也可做;

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
const int N=2e5+,M=4e6+,inf=1e9+,mod=1e9+;
const ll INF=1e18+;
struct is
{
ll num;
int pos;
}tree[N<<];
ll ans[N];
void pushup(int pos)
{
tree[pos].num=min(tree[pos<<].num,tree[pos<<|].num);
}
void buildtree(int l,int r,int pos)
{
if(l==r)
{
tree[pos].num=abs(ans[l]);
tree[pos].pos=l;
return;
}
int mid=(l+r)>>;
buildtree(l,mid,pos<<);
buildtree(mid+,r,pos<<|);
pushup(pos);
}
void update(int p,ll c,int l,int r,int pos)
{
if(p==r&&p==l)
{
tree[pos].num=abs(c);
return;
}
int mid=(l+r)>>;
if(p<=mid)
update(p,c,l,mid,pos<<);
else
update(p,c,mid+,r,pos<<|);
pushup(pos);
}
int query(ll x,int l,int r,int pos)
{
if(l==r&&tree[pos].num==x)
return tree[pos].pos;
int mid=(l+r)>>;
if(tree[pos<<].num==x)
return query(x,l,mid,pos<<);
else
return query(x,mid+,r,pos<<|);
}
int main()
{
int n,m,k;
int flag=;
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=n;i++)
{
scanf("%lld",&ans[i]);
if(ans[i]<)flag++;
}
buildtree(,n,);
while(m--)
{
ll x=tree[].num;
int pos=query(x,,n,);
if(flag&)
{
if(ans[pos]>=)
ans[pos]+=k;
else
ans[pos]-=k;
update(pos,ans[pos],,n,);
}
else
{
if(ans[pos]>=)
{
ans[pos]=ans[pos]-k;
if(ans[pos]<)
flag++;
}
else
{
ans[pos]=ans[pos]+k;
if(ans[pos]>=)
flag--;
}
update(pos,ans[pos],,n,);
}
}
for(int i=;i<=n;i++)
printf("%lld ",ans[i]);
return ;
}

最新文章

  1. OpenCascade MeshVS Usage
  2. SQL Server 2012 数据库笔记
  3. android环境搭建
  4. xmanager远程桌面连接Linux
  5. json2form已改名为AForm
  6. php curl向远程服务器上传文件
  7. Python的类实例属性访问规则
  8. OSGi 对软件复杂度的影响
  9. 【GitHub】在Mac上配置/使用Github
  10. bigdata之hadoop and spark
  11. 淘淘商城_day08_课堂笔记
  12. [bzoj1587] [Usaco2009 Mar]Cleaning Up 打扫卫生
  13. Hadoop HDFS常用操作命令
  14. linux上部署Appach,让文件目录以网页列表形式访问
  15. eclipse经常出现——未响应!!!
  16. postman发送json格式的post请求
  17. yum 安装 jenkins
  18. django--admin组件
  19. 前端学PHP之正则表达式函数
  20. liunx trac 插件使用之DateFieldPlugin

热门文章

  1. java数据结构之枚举
  2. android菜鸟学习笔记31----Android使用百度地图API(二)获取地理位置及地图控制器的简单使用
  3. 用JS改变的元素CSS样式,css里display :none 隐藏 block 显示
  4. 数字雨(Javascript使用canvas绘制Matrix,效果很赞哦)
  5. MySQL二进制包安装简略过程
  6. Linux中变量测试与内容替换
  7. spring 编译时抱错纪录class path resource [spring/] cannot be resolved to URL because it does not exist
  8. Maven学习笔记—坐标和依赖
  9. SUBMIT RM07DOCS【MB51】 获取返回清单,抓取标准报表数据
  10. If you ever have a broken heart