大意: 给定序列, 每次操作选择一个数+x或-x, 最多k次操作, 求操作后所有元素积的最小值

贪心先选出绝对值最小的调整为负数, 再不断选出绝对值最小的增大它的绝对值

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstdio>
  4. #include <math.h>
  5. #include <set>
  6. #include <map>
  7. #include <queue>
  8. #include <string>
  9. #include <string.h>
  10. #include <bitset>
  11. #define REP(i,a,n) for(int i=a;i<=n;++i)
  12. #define PER(i,a,n) for(int i=n;i>=a;--i)
  13. #define hr putchar(10)
  14. #define pb push_back
  15. #define lc (o<<1)
  16. #define rc (lc|1)
  17. #define mid ((l+r)>>1)
  18. #define ls lc,l,mid
  19. #define rs rc,mid+1,r
  20. #define x first
  21. #define y second
  22. #define io std::ios::sync_with_stdio(false)
  23. #define endl '\n'
  24. using namespace std;
  25. typedef long long ll;
  26. typedef pair<int,int> pii;
  27. const int P = 1e9+7, INF = 0x3f3f3f3f;
  28. ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
  29. ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
  30. ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
  31. //head
  32.  
  33. const int N = 1e6+10;
  34. int n, k, x;
  35. struct _ {
  36. ll w;
  37. int id;
  38. bool operator < (const _ &rhs) const {
  39. return abs(w) > abs(rhs.w);
  40. }
  41. } e[N];
  42. ll ans[N];
  43. int sgn(ll x) {return x<0?-1:1;}
  44. int main() {
  45. scanf("%d%d%d", &n, &k, &x);
  46. int f = 0;
  47. REP(i,1,n) scanf("%lld", &e[i].w), e[i].id=i, f^=e[i].w<0;
  48. if (!f) {
  49. _ *r = max_element(e+1,e+1+n);
  50. if (!r->w) --k,r->w-=x;
  51. else {
  52. int ff = sgn(r->w);
  53. while (k) {
  54. r->w -= sgn(r->w)*x, --k;
  55. if (sgn(r->w)!=ff) break;
  56. }
  57. }
  58. }
  59. priority_queue<_> q;
  60. REP(i,1,n) q.push(e[i]);
  61. while (k) {
  62. _ t = q.top(); q.pop();
  63. t.w += sgn(t.w)*x;
  64. q.push(t), --k;
  65. }
  66. while (q.size()) ans[q.top().id]=q.top().w,q.pop();
  67. REP(i,1,n) printf("%lld ", ans[i]);hr;
  68. }

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

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

  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 721D [贪心]

    /* 不要低头,不要放弃,不要气馁,不要慌张. 题意: 给一列数a,可以进行k次操作,每次操作可以选取任意一个数加x或者减x,x是固定的数.求如何才能使得这个数列所有数乘积最小. 思路: 贪心...讨 ...

  4. Artem and Array CodeForces - 442C (贪心)

    大意: 给定序列$a$, 每次任选$a_i$删除, 得分$min(a_{i-1},a_{i+1})$(无前驱后继时不得分), 求最大得分. 若一个数$x$的两边都比$x$大直接将$x$删除, 最后剩余 ...

  5. CodeForces - 721D 贪心+优先队列(整理一下优先队列排序情况)

    题意: 给你一个长度为n的数组,你可以对其中某个元素加上x或者减去x,这种操作你最多只能使用k次,让你输出操作后的数组,且保证这个数组所有元素的乘积尽可能小 题解: 在这之前我们要知道a*b>a ...

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

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

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

  8. Greg and Array CodeForces 296C 差分数组

    Greg and Array CodeForces 296C 差分数组 题意 是说有n个数,m种操作,这m种操作就是让一段区间内的数增加或则减少,然后有k种控制,这k种控制是说让m种操作中的一段区域内 ...

  9. CodeForces - 721D Maxim and Array (贪心)

    Recently Maxim has found an array of n integers, needed by no one. He immediately come up with idea ...

随机推荐

  1. win10中命令操作Zookeeper

    目录 zk客户端命令: 连接: 命令: 四字命令: 常用命令: 返回参数说明: 参考: zk客户端命令: 连接: C:\Users\qhong\Desktop $ zkCli.cmd -server ...

  2. 枚举+排序|神奇算式|2014年蓝桥杯A组题解析第三题-fishers

    标题:神奇算式 由4个不同的数字,组成的一个乘法算式,它们的乘积仍然由这4个数字组成. 比如: 210 x 6 = 1260 8 x 473 = 3784 27 x 81 = 2187 都符合要求. ...

  3. 160CrackMe练手 001

    peid判断无壳,打开输入伪码注册,根据报错od查找字符串 接下来定位到字符串周边代码 0042FA15 |. 8D55 F0 lea edx,[local.4] 0042FA18 |. 8B83 D ...

  4. js 二叉树删除最大值和最小值

    //删除最小值function delMinNode (root){ if(!root) { return false; } var current = root; if (current.left ...

  5. P4720 【模板】扩展卢卡斯

    思路 扩展Lucas和Lucas定理其实没什么关系 我们要求的是这样的一个问题 \[ \left(\begin{matrix}n\\m\end{matrix}\right) mod\ P \] p不一 ...

  6. P2633 Count on a tree

    思路 运用树上差分的思想,转化成一个普通的主席树模型即可求解 代码 #include <cstdio> #include <algorithm> #include <cs ...

  7. 论文笔记之 SST: Single-Stream Temporal Action Proposals

    SST: Single-Stream Temporal Action Proposals 2017-06-11 14:28:00 本文提出一种 时间维度上的 proposal 方法,进行行为的识别.本 ...

  8. Java中sort实现降序排序

    利用Collections的reverseOrder方法: import java.util.Arrays; import java.util.Collections; public class Ma ...

  9. (转载)Windows下小狼毫输入法(Rime)的安装与配置(含导入搜狗词库)

    div id="cnblogs_post_body" class="blogpost-body"> 最近彻底烦透了搜狗拼音输入法的各种流氓行为,自动升级不 ...

  10. Java基础【冒泡、选择排序、二分查找】

    冒泡排序的思路就是前一个和后一个进行比较,如果大的就交换位置   大的数字后浮 如   12      8    5     31 第一轮   8   5   12   31 第二轮   5   8 ...