博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心
阅读量:6366 次
发布时间:2019-06-23

本文共 2184 字,大约阅读时间需要 7 分钟。

D. Maxim and Array

题目连接:

Description

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.

Sample Input

5 3 1

5 4 3 5 2

Sample Output

5 4 3 5 -1

Hint

题意

给你n个数,你可以操作k次,每次使得一个数增加x或者减小x

你要使得最后所有数的乘积最小,问你最后这个序列长什么样子。

题解:

贪心,根据符号的不同,每次贪心的使得一个绝对值最小的数减去x或者加上x就好了

这个贪心比较显然。

假设当前乘积为ANS,那么你改变a[i]的大小的话,那么对答案的影响为ANS/A[i]/*X

然后找到影响最大的就好了。

代码

#include
using namespace std;const int maxn = 2e5+7;long long a[maxn],b[maxn];int n,k,x;set
>S;int main(){ scanf("%d%d%d",&n,&k,&x); int sig = 0; for(int i=1;i<=n;i++) { scanf("%lld",&a[i]); if(a[i]<0)sig^=1; S.insert(make_pair(abs(a[i]),i)); } for(int i=1;i<=k;i++) { int pos = S.begin()->second; S.erase(S.begin()); if(a[pos]<0)sig^=1; if(sig)a[pos]+=x; else a[pos]-=x; if(a[pos]<0)sig^=1; S.insert(make_pair(abs(a[pos]),pos)); } for(int i=1;i<=n;i++) cout<
<<" "; cout<

转载地址:http://uyrma.baihongyu.com/

你可能感兴趣的文章
python遗传算法(GA)DEAP-Overview学习摘要
查看>>
直接插入排序
查看>>
高流量网站如何做出高性能?
查看>>
[Translate] CockroachDB: 创建安全证书
查看>>
fir.im Weekly - iOS开发中的Git流程
查看>>
scope in Angularjs
查看>>
强大的strtotime函数
查看>>
阿里获邀加入 JCP ,参与制定 Java 全球标准和技术规范
查看>>
HexoClient 1.2.6 发布,支持 hexo front-matter 特性
查看>>
翻译 Meteor React 制作 Todos - 08 - 模板UI的状态
查看>>
Jsp-九大内置对象
查看>>
两大核心能力助力,中小险企破局生态建设——保险生态建设 ...
查看>>
中国数字支付战况:B端业务异军突起,C端流量让位场景 ...
查看>>
理解神经网络:从神经元到RNN、CNN、深度学习
查看>>
阿里P7高级架构师分享8年多的Java工作经验(跳槽涨薪必备) ...
查看>>
dokuwiki安装问题
查看>>
【资料下载】Python第八讲——寻找知乎最美小姐姐 ...
查看>>
linux 显示系统所有用户
查看>>
Synchronized锁在Spring事务管理下,为啥还线程不安全?
查看>>
全志 A64开发板Linux内核定时器编程
查看>>