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 koperations 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
  1. 5 3 1
    5 4 3 5 2
Output
  1. 5 4 3 5 -1
 
思路:
如果负数个数为偶数,想办法让其变成奇数.
如果不能变为奇数,则不做任何变化,进入下一步.如果可以,控制其刚好变为奇数即可.
如果本身就是奇数,则不做任何变化.
 
接下来进行k步操作.
如果当前负数个数为奇数,则想办法使绝对值之积极可能大,每次使绝对值最小的数字的绝对值加上x即可.
如果当前负数个数为偶数,则想办法使绝对值之积极可能小,每次使绝对值最小的数字的绝对值减去x即可.
此操作使用优先队列维护.
 
______________
(a-x) * b = a*b-b*x
显然 当a<b时优于 a>b
 
  1. #include<iostream>
  2. #include<algorithm>
  3. #include<vector>
  4. #include<stack>
  5. #include<queue>
  6. #include<map>
  7. #include<set>
  8. #include<cstdio>
  9. #include<cstring>
  10. #include<cmath>
  11. #include<ctime>
  12.  
  13. #define fuck(x) cerr<<#x<<" = "<<x<<endl;
  14. #define debug(a, x) cerr<<#a<<"["<<x<<"] = "<<a[x]<<endl;
  15. #define ls (t<<1)
  16. #define rs ((t<<1)|1)
  17. using namespace std;
  18. typedef long long ll;
  19. typedef unsigned long long ull;
  20. const int loveisblue = ;
  21. const int maxn = ;
  22. const int maxm = ;
  23. const int inf = 0x3f3f3f3f;
  24. const ll Inf = ;
  25. const int mod = ;
  26. const double eps = 1e-;
  27. const double pi = acos(-);
  28.  
  29. int n,k;
  30. struct node{
  31. ll num,absnum;
  32. int id;
  33. bool operator<(const node &p)const{
  34. return p.absnum<absnum;
  35. }
  36. }a[maxn];
  37. priority_queue<node>q;
  38. ll ans[maxn];
  39. int main() {
  40. // ios::sync_with_stdio(false);
  41. // freopen("in.txt", "r", stdin);
  42.  
  43. int n,k;
  44. ll x;
  45. scanf("%d%d%lld",&n,&k,&x);
  46. for(int i=;i<=n;i++){
  47. ll num;
  48. scanf("%lld",&num);
  49. a[i]=node{num,abs(num),i};
  50. }
  51. sort(a+,a++n);
  52. int fu = ;
  53. for(int i=;i<=n;i++){
  54. if(a[i].num<){
  55. fu++;
  56. }
  57. }
  58. if(fu%==){
  59. if(x*k>=a[n].absnum){
  60. int p = min(1ll*k,a[n].absnum/x+);
  61. k-=p;
  62. if(a[n].num<){
  63. a[n].num+=p*x;
  64. a[n].absnum = abs(a[n].num);
  65. }else{
  66. a[n].num-=p*x;
  67. a[n].absnum = abs(a[n].num);
  68. }
  69. }
  70. }
  71. fu = ;
  72. for(int i=;i<=n;i++){
  73. if(a[i].num<){
  74. fu++;
  75. }
  76. }
  77. for(int i=;i<=n;i++){
  78. q.push(a[i]);
  79. }
  80. while (k--){
  81. node exa = q.top();
  82. q.pop();
  83. if(fu&){
  84. if(exa.num<){
  85. exa.num-=x;
  86. exa.absnum+=x;
  87. }else{
  88. exa.num+=x;
  89. exa.absnum+=x;
  90. }
  91. }else{
  92. if(exa.num<){
  93. exa.num+=x;
  94. exa.absnum-=x;
  95. }else{
  96. exa.num-=x;
  97. exa.absnum-=x;
  98. }
  99. }
  100. q.push(exa);
  101. }
  102. while (!q.empty()){
  103. node exa = q.top();
  104. q.pop();
  105. ans[exa.id]=exa.num;
  106. }
  107. for(int i=;i<=n;i++){
  108. printf("%lld ",ans[i]);
  109. }
  110. return ;
  111. }

CodeForces - 721D Maxim and Array (贪心)的更多相关文章

  1. CodeForces 721D Maxim and Array

    贪心,优先队列. 先看一下输入的数组乘积是正的还是负的. ①如果是负的,也就是接下来的操作肯定是让正的加大,负的减小.每次寻找一个绝对值最小的数操作就可以了. ②如果是正的,也是考虑绝对值,先操作绝对 ...

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

    D. Maxim and Array 题目连接: http://codeforces.com/contest/721/problem/D Description Recently Maxim has ...

  3. 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 ...

  4. Codeforces F. Maxim and Array(构造贪心)

    题目描述: Maxim and Array time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  5. 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 ...

  6. Codeforces 442C Artem and Array(stack+贪婪)

    题目连接:Codeforces 442C Artem and Array 题目大意:给出一个数组,每次删除一个数.删除一个数的得分为两边数的最小值,假设左右有一边不存在则算作0分. 问最大得分是多少. ...

  7. Codeforces Round #504 D. Array Restoration

    Codeforces Round #504 D. Array Restoration 题目描述:有一个长度为\(n\)的序列\(a\),有\(q\)次操作,第\(i\)次选择一个区间,将区间里的数全部 ...

  8. codeforces Gym 100338E Numbers (贪心,实现)

    题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...

  9. [Codeforces 1214A]Optimal Currency Exchange(贪心)

    [Codeforces 1214A]Optimal Currency Exchange(贪心) 题面 题面较长,略 分析 这个A题稍微有点思维难度,比赛的时候被孙了一下 贪心的思路是,我们换面值越小的 ...

随机推荐

  1. hdu5437 优先队列 长春网赛

    优先队列做,然后遍历人数. #include <queue> #include <stdio.h> #include <string.h> #define maxn ...

  2. MyBatis的基本用法

    MyBatis MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以使 ...

  3. 利用IDEA构建springboot应用-Controller的使用

    Controller的使用 @Controller 处理http请求   @RestController  Spring4之后新加的注解,原来返回json需要@ResponseBody配合@Contr ...

  4. 捕捉WPF应用程序中XAML代码解析异常

    原文:捕捉WPF应用程序中XAML代码解析异常 由于WPF应用程序中XAML代码在很多时候是运行时加载处理的.比如DynamicResource,但是在编译或者运行的过程中,编写的XAML代码很可能有 ...

  5. 2019-8-31-dotnet-通过-WMI-获取系统信息

    title author date CreateTime categories dotnet 通过 WMI 获取系统信息 lindexi 2019-08-31 16:55:59 +0800 2019- ...

  6. 谷歌BERT预训练源码解析(二):模型构建

    目录前言源码解析模型配置参数BertModelword embeddingembedding_postprocessorTransformerself_attention模型应用前言BERT的模型主要 ...

  7. PPP验证对比

  8. Laravel 服务提供者实例教程 —— 创建 Service Provider 测试实例

    从某种意义上说,服务提供者有点类似HTTP控制器,HTTP控制器用于为相关路由注册提供统一管理,而服务提供者用于为相关服务容器提供统一绑定场所,此外服务提供者还可以做一些初始化启动操作.Laravel ...

  9. [转]来自后端的逆袭 blazor简介 全栈的福音

    背景 什么是SPA 什么是MPA MPA (Multi-page Application) 多页面应用指的就是最传统的 HTML 网页设计,早期的网站都是这样的设计,所之称为「网页设计」.使用 MPA ...

  10. xml 校验

    package sax.parsing; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoun ...