D. Maxim and Array

题目连接:

http://codeforces.com/contest/721/problem/D

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<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+7; long long a[maxn],b[maxn];
int n,k,x;
set<pair<long long,long long> >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<<a[i]<<" ";
cout<<endl; }

Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心的更多相关文章

  1. Codeforces Round #374 (Div. 2) D. Maxim and Array —— 贪心

    题目链接:http://codeforces.com/problemset/problem/721/D D. Maxim and Array time limit per test 2 seconds ...

  2. Codeforces Round #374 (Div. 2) D. Maxim and Array 线段树+贪心

    D. Maxim and Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  3. Codeforces Round #374 (Div. 2) D. Maxim and Array

    传送门 分析:其实没什么好分析的.统计一下负数个数.如果负数个数是偶数的话,就要尽量增加负数或者减少负数.是奇数的话就努力增大每个数的绝对值.用一个优先队列搞一下就行了. 我感觉这道题的细节极为多,非 ...

  4. Codeforces Round #297 (Div. 2)C. Ilya and Sticks 贪心

    Codeforces Round #297 (Div. 2)C. Ilya and Sticks Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx  ...

  5. Codeforces Round #374 (div.2)遗憾题合集

    C.Journey 读错题目了...不是无向图,结果建错图了(喵第4样例是变成无向就会有环的那种图) 并且这题因为要求路径点尽可能多 其实可以规约为限定路径长的拓扑排序,不一定要用最短路做 #prag ...

  6. Codeforces Round #374 (Div. 2) A B C D 水 模拟 dp+dfs 优先队列

    A. One-dimensional Japanese Crossword time limit per test 1 second memory limit per test 256 megabyt ...

  7. 拓扑序+dp Codeforces Round #374 (Div. 2) C

    http://codeforces.com/contest/721/problem/C 题目大意:给你有向路,每条路都有一个权值t,你从1走到n,最多花费不能超过T,问在T时间内最多能访问多少城市? ...

  8. Codeforces Round #374 (Div. 2) C. Journey DP

    C. Journey 题目连接: http://codeforces.com/contest/721/problem/C Description Recently Irina arrived to o ...

  9. Codeforces Round #374 (Div. 2) B. Passwords 贪心

    B. Passwords 题目连接: http://codeforces.com/contest/721/problem/B Description Vanya is managed to enter ...

随机推荐

  1. Dubbo学习笔记5:Dubbo整体框架分析

    Dubbo的分层架构 本文将简单介绍Dubbo的分层架构设计,如下图是Dubbo官方的整体架构图: Dubbo官方提供的该架构图很复杂,一开始我们没必要深入细节,下面我们简单介绍下其中的主要模块. 其 ...

  2. 你真的理解js的赋值语句么

    之前谢亮兄和我一起讨论的一个问题: var a = {}; a.x = a = 3; a 的值是什么. 其实当执行赋值语句的时候,js 的 = 左侧不是原始变量地址,而是一个新值.怎么理解这句话呢? ...

  3. 基于序列化技术(Protobuf)的socket文件传输

    好像好久都没更博文了,没办法,最近各种倒霉事情,搞到最近真的没什么心情,希望之后能够转运吧. 言归正传,这次我要做的是基于序列化技术的socket文件传输来无聊练一下手. 一.socket文件传输 之 ...

  4. 20155201 2016-2017-2 《Java程序设计》第五周学习总结

    20155201 2016-2017-2 <Java程序设计>第五周学习总结 教材学习内容总结 第八章 异常处理 程序设计本身的错误,建议使用Exception或其子类实例来表现,称错误处 ...

  5. linux - 流量切分线路

    流量切分线路方式 # 程序判断进入IP线路,设置服务器路由规则控制返回 vi /etc/iproute2/rt_tables #添加一条策略 bgp2 #注意策略的序号顺序 ip route add ...

  6. 二维码扫描开源库ZXing定制化【转】

    转自:http://www.cnblogs.com/sickworm/p/4562081.html 最近在用ZXing这个开源库做二维码的扫描模块,开发过程的一些代码修改和裁剪的经验和大家分享一下. ...

  7. IIS 无法识别的属性“targetFramework”---解决之道

    在安装VS2010后,应用.NET Framework 4创建的网站放在IIS(7.0)下会出现如下的错误: 其中的“版本信息”中告诉了我们.NET Framework和ASP.NET的版本都是2.0 ...

  8. windows安装 Microsoft Visual c++

    第一种方法: 第二种方法: 参考链接 直接给一个2015版本的下载地址 https://blogs.msdn.microsoft.com/pythonengineering/2016/04/11/un ...

  9. Go语言规格说明书 之 词汇元素(Lexical elements)

    go version go1.11 windows/amd64 本文为阅读Go语言中文官网的规则说明书(https://golang.google.cn/ref/spec)而做的笔记,完整的介绍Go语 ...

  10. MongoDB:数据导入CSV文件之错误记录

    测试主机1:Windows 10,MongoDB 3.6.3,WPS 10.1,Notepad++ 7.5.3, 测试主机2:Ubuntu 16.04,MongoDB 4, 今天测试了将数据从文件—— ...