题目链接:http://codeforces.com/problemset/problem/721/D

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
  1. 5 3 1
  2. 5 4 3 5 2
output
  1. 5 4 3 5 -1
input
  1. 5 3 1
  2. 5 4 3 5 5
output
  1. 5 4 0 5 5
input
  1. 5 3 1
  2. 5 4 4 5 5
output
  1. 5 1 4 5 5
input
  1. 3 2 7
  2. 5 4 2
output
  1. 5 11 -5

题解:

1.首先在输入时,统计负数的个数,目的是知道初始状态的乘积是正数(包括0)还是负数。

2.如果乘积为正数,那么就需要减小某个数的绝对值,使其改变符号, 而且步数越少越好,由此推出:减少绝对值最小的那个数的绝对值,可以最快改变符号,使得乘积变为负数。

3.如果乘积已经为负,那么就需要增加某个数的绝对值, 使得乘积的绝对值尽可能大, 根据基本不等式: a+b>=2*根号(a*b),若要a*b的值最大,则a==b。 可以得出结论:当a与b的差值越小时,a*b越大。或者可以自己手动推算一遍, 也可以得出这个结论。由此推出:增加绝对值最小的那个数的绝对值,使得乘积的绝对值尽可能大。

4.总的来说:就是需要对绝对值最小的数进行操作。当乘积为正或为0时, 减小其绝对值,直到符号改变; 当乘积为负时, 增加其绝对值, 使其乘积的绝对值尽可能大。 用优先队列维护。

学习之处: 

a+b=常数, 当a与b的差值越小时,a*b越大(a*b>=0)。

这里的a*b,可以假设a为绝对值最小的那个数, b为其他数的绝对值的乘积。所以这个结论也适用于多个数的乘积(将多个转为2个)。

代码如下:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cstdlib>
  5. #include <string>
  6. #include <vector>
  7. #include <map>
  8. #include <set>
  9. #include <queue>
  10. #include <stack>
  11. #include <sstream>
  12. #include <algorithm>
  13. using namespace std;
  14. #define ms(a, b) memset((a), (b), sizeof(a))
  15. #define eps 0.0000001
  16. typedef long long LL;
  17. const int INF = 2e9;
  18. const LL LNF = 9e18;
  19. const int mod = 1e9+7;
  20. const int maxn = 2e5+10;
  21.  
  22. struct node
  23. {
  24. LL v, s, pos;
  25. bool operator<(const node&x)const{
  26. return v>x.v;
  27. }
  28. };
  29. priority_queue<node>q;
  30. LL a[maxn], n, k, x, flag;
  31.  
  32. void init()
  33. {
  34. scanf("%lld%lld%lld",&n, &k, &x);
  35. while(!q.empty()) q.pop();
  36. flag = 0;
  37. for(int i = 1; i<=n; i++)
  38. {
  39. node e;
  40. scanf("%lld",&a[i]);
  41. e.v = abs(a[i]);
  42. e.s = (a[i]<0);
  43. e.pos = i;
  44. q.push(e);
  45.  
  46. if(a[i]<0) flag = !flag;
  47. }
  48. }
  49.  
  50. void solve()
  51. {
  52. while(k--)
  53. {
  54. node e = q.top();
  55. q.pop();
  56. if(!flag)
  57. {
  58. e.v -= x;
  59. if(e.v<0)
  60. {
  61. e.v = -e.v;
  62. e.s = !e.s;
  63. flag = !flag;
  64. }
  65. }
  66. else
  67. {
  68. e.v += x;
  69. }
  70. a[e.pos] = e.v;
  71. if(e.s) a[e.pos] = -a[e.pos];
  72. q.push(e);
  73. }
  74. for(int i = 1; i<=n; i++)
  75. printf("%lld ",a[i]);
  76. putchar('\n');
  77. }
  78.  
  79. int main()
  80. {
  81. // int T;
  82. // scanf("%d",&T);
  83. // while(T--)
  84. {
  85. init();
  86. solve();
  87. }
  88. return 0;
  89. }

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

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

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

  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. mac下安装pyQt4

    1.首先安装QT,同时要有gcc 2.然后就是先安装sip,然后安装pyqt4 python configure.py -q /usr/bin/qmake-4.8 -d /Library/Python ...

  2. Javao中使用Jackson反序列时,将LinkedHashMap转成对象的方法(将任何Object类型转成实体)

    可能存在这样一种情况,Jackson已经满足了大部分的序列化和反序列化工作,但是对于复杂的泛型实体估计未必能如愿的正常反序列,而此时对于一些泛型里面的实体对象就会反序列化成LinkedHashMap类 ...

  3. echarts判断点击参数类型,series为有效,markPoint 无效

    https://www.w3cschool.cn/echarts_tutorial/echarts_tutorial-7o3u28yh.html 可以设置如果点击的是markPoint,直接返回

  4. [bug]Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding

    写在前面 在mysql中这个异常是非常常见的,超时分为连接超时和执行超时,而连接超时,大部分原因是网络问题,或客户端到服务端的端口问题造成. bug场景 有的时候,使用MySqlDataReader在 ...

  5. 如何设置tomcat服务器编码为utf-8编码

    原文:http://blog.csdn.net/u014079773/article/details/52637057 在实际开发中我们经常遇到request请求的中文乱码,那么如何解决中文乱码问题? ...

  6. 【转载】Linux下套接字学习

    感觉这个系列还不错,学习一下. 先看的是第三篇: http://blog.csdn.net/gatieme/article/details/46334337 < Linux下套接字详解(三)-- ...

  7. Java中字符串转为16进制表示

    Java中字符串转为16进制表示 String str = "鲸"; char[] chars = "0123456789ABCDEF".toCharArray ...

  8. AngularJS的简单表单验证

    代码下载:https://files.cnblogs.com/files/xiandedanteng/angularjsCheckSimpleForm.rar 代码: <!DOCTYPE HTM ...

  9. Google的分布式计算模型Map Reduce map函数将输入分割成key/value对

    http://www.nowamagic.net/librarys/veda/detail/1768 上一篇 大规模分布式数据处理平台Hadoop的介绍 中提到了Google的分布式计算模型Map R ...

  10. substr使用注意

    substr使用时要判断起点和长度是否超过了串本身的长度,否则会抛异常