链接:

https://vjudge.net/problem/CodeForces-721D

题意:

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.

思路:

贪心对每一个绝对值最小的值处理,小于0就减,大于等于0就加.等于0注意要当大于0考虑.

代码:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long LL;
  4. const int MAXN = 2e5+10;
  5. struct Node
  6. {
  7. int pos;
  8. LL val;
  9. bool operator < (const Node& that) const
  10. {
  11. return abs(this->val) > abs(that.val);
  12. }
  13. }node[MAXN];
  14. int n;
  15. LL k, x;
  16. void Solve()
  17. {
  18. priority_queue<Node> que;
  19. for (int i = 1;i <= n;i++)
  20. que.push(node[i]);
  21. while (k)
  22. {
  23. Node now = que.top();
  24. que.pop();
  25. if (now.val >= 0)
  26. now.val += x;
  27. else
  28. now.val -= x;
  29. que.push(now);
  30. k--;
  31. }
  32. while (!que.empty())
  33. {
  34. node[que.top().pos] = que.top();
  35. que.pop();
  36. }
  37. for (int i = 1;i <= n;i++)
  38. printf("%lld ", node[i].val);
  39. printf("\n");
  40. }
  41. int main()
  42. {
  43. scanf("%d %d %lld", &n, &k, &x);
  44. int cnt = 0;
  45. for (int i = 1;i <= n;i++)
  46. {
  47. scanf("%lld", &node[i].val);
  48. node[i].pos = i;
  49. if (node[i].val < 0)
  50. cnt++;
  51. }
  52. if (cnt == 0)
  53. {
  54. int mpos = 1;
  55. for (int i = 1;i <= n;i++)
  56. {
  57. if (node[i].val < node[mpos].val)
  58. mpos = i;
  59. }
  60. LL ti = (node[mpos].val+1LL+x-1)/x;
  61. if (ti > k)
  62. node[mpos].val -= k*x;
  63. else
  64. node[mpos].val -= ti*x;
  65. k -= min(ti, k);
  66. }
  67. else if (cnt > 0 && cnt%2 == 0)
  68. {
  69. int mpos = 1;
  70. for (int i = 1;i <= n;i++)
  71. {
  72. if (abs(node[i].val) < abs(node[mpos].val))
  73. mpos = i;
  74. }
  75. if (node[mpos].val >= 0)
  76. {
  77. LL ti = (node[mpos].val+1LL+x-1)/x;
  78. if (ti > k)
  79. node[mpos].val -= k*x;
  80. else
  81. node[mpos].val -= ti*x;
  82. k -= min(ti, k);
  83. }
  84. else
  85. {
  86. LL ti = (abs(node[mpos].val)+1LL+x-1)/x;
  87. if (ti > k)
  88. node[mpos].val += k*x;
  89. else
  90. node[mpos].val += ti*x;
  91. k -= min(ti, k);
  92. }
  93. }
  94. Solve();
  95. return 0;
  96. }

CodeForces-721D-Maxim and Array(优先队列,贪心,分类讨论)的更多相关文章

  1. CodeForces 721D Maxim and Array

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

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

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

  3. Codeforces G. Nick and Array(贪心)

    题目描述: Nick had received an awesome array of integers a=[a1,a2,…,an] as a gift for his 5 birthday fro ...

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

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

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

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

  7. Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array 分类讨论连续递推dp

    题意:给出一个 数列 和一个x 可以对数列一个连续的部分 每个数乘以x  问该序列可以达到的最大连续序列和是多少 思路: 不是所有区间题目都是线段树!!!!!! 这题其实是一个很简单的dp 使用的是分 ...

  8. Codeforces 437C The Child and Toy(贪心)

    题目连接:Codeforces 437C  The Child and Toy 贪心,每条绳子都是须要割断的,那就先割断最大值相应的那部分周围的绳子. #include <iostream> ...

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

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

随机推荐

  1. The window object

    At the core of the BOM is the window object, which represents an instance of the browser. The window ...

  2. 关机报 at-spi-bus-launcher

    查看是否有autostart文件夹,有则不建立 mkdir -v ~/.config/autostart 可直接修改/etc/xdg/autostart/at-spi-dbus-bus.desktop ...

  3. github创建项目,并提交本地文件

    1.如图所示,不要点选"Initialize this repository with README",不然就看不到第二幅图的提示信息了 2.根据下面提示,初始化本地文件,然后上传

  4. C#使用NPOI读写excel

    本帖内容来自网络+自己稍作整理,已找不到原贴,侵删 个人比较习惯用NPOI操作excel,方便易理解.在宇宙第一IDE(笑)——VS2017中插入NPOI就很方便: 首先安装NPOI: 然后在.cs文 ...

  5. 【HANA系列】SAP 【第二篇】EXCEL连接SAP HANA的方法(ODBC)

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP [第二篇]EXCEL连接 ...

  6. CSS未完

    CSS介绍 CSS(Cascading Style Sheet,层叠样式表)定义如何显示HTML元素. 当浏览器读到一个样式表,它就会按照这个样式表来对文档进行格式化(渲染). CSS语法 CSS实例 ...

  7. Docker】如何修改Docker的默认镜像存储位置

    江湖有多大,坑就有多多……我使用的服务器, 系统盘根目录只有20G, 默认Docker 的镜像文件是安装在/var/lib/docker 目录下的, 这样的话我根本装不了太多的镜像,之前遇到一种情况就 ...

  8. 微信小程序 ----- this.getOpenerEventChannel is not a function

    小程序 添加新的功能, 页面跳转后,通过事件的发布订阅,实现 from => to 或者 to=> from 数据传递 1. 跳转到指定页面. 通过 wx.navigateTo() .请参 ...

  9. k8s-kubernetes-configmap存储

    存储 configMap configMap描述信息 ConfigMap功能在Kubernetes1.2版本中引入,许多应用程序会从配置文件.命令行参数或环境变量中读取配置信息. ConfigMap ...

  10. Django框架中使用Echart进行统计的SQL语句

    最近想用Echart做数据统计的图形显示,数据来源是MySQL数据库,自然需要根据不同的搜索条件筛选出表中的数据,用比较多的就是时间的参数吧! 常用的mysql时间的条件进行检索的SQL语句: 数据表 ...