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. pandas重置索引的几种方法探究

    pandas重置索引的几种方法探究 reset_index() reindex() set_index() 函数名字看起来非常有趣吧! 不仅如此. 需要探究. http://nbviewer.jupy ...

  2. Linux iptables常用命令的使用

    为什么会有本文 因为最近帮一个朋友布署一个上网梯子,他那边本来用的是v2ray,但是他想用ssr,但是安装配置ssr过程中出了很多问题,比如linux内核版本4.9有点老,不支持bbr加速.无法连接s ...

  3. Tensorflow数据读取的方式

    深度学习既然是基于数据的方法,先不管多抽象,那总归是有读取数据的方法的吧,这里的数据应该是一个统称,包含我们讲的数据集和变量tensor. tf读取数据一共有3种方法: 供给数据(Feeding): ...

  4. Hive笔记之严格模式(strict mode)

    Hive有一个严格模式,在严格模式下会对可能产生较大查询结果的语句做限制,禁止其提交执行. 一.切换严格模式 查看当前的模式: hive> set hive.mapred.mode; hive. ...

  5. VS 多工程代码编写

    VS工作目录,输出目录 C++项目,解决方案总文件夹下就只包含解决方案配置文件sln和一个项目总文件夹和一个Debug文件夹以及一个Release文件夹(共四个东东,其中Debug和Release文件 ...

  6. java中final、finally、finalized使用方法

    首先需要明白  final和finally是关键字,finalize是一个方法. 1. final关键字 final可以修饰类.方法.变量, 修饰类表示类不可以被继承 修饰方法表示此方法不可以被重写( ...

  7. 春夏秋冬又一春之Redis持久化

    历史文章推荐: 一只准程序猿的唠叨 可能是最漂亮的Spring事务管理详解 Java多线程学习(八)线程池与Executor 框架 面试中关于Redis的问题看这篇就够了 非常感谢<redis实 ...

  8. LOJ 2249: 洛谷 P2305: 「NOI2014」购票

    题目传送门:LOJ #2249. 题意简述: 有一棵以 \(1\) 号节点为根节点的带边权的树. 除了 \(1\) 号节点的所有节点上都有人需要坐车到达 \(1\) 号节点. 除了 \(1\) 号节点 ...

  9. linux 串口驱动(三) 【转】

    转自:http://blog.chinaunix.net/uid-27717694-id-3495825.html 三.串口的打开在用户空间执行open操作的时候,就会执行uart_ops->o ...

  10. MVC 控制器中传递dynamic(对象) 给视图

    有时候不想重新定义一个实体,则使用 dynamic 来定义匿名类型. //匿名类型 传递到前台 Model dynamic viewModel = new { UserID = 5016 }; ret ...