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

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

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
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;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head const int N = 1e6+10;
int n, k, x;
struct _ {
ll w;
int id;
bool operator < (const _ &rhs) const {
return abs(w) > abs(rhs.w);
}
} e[N];
ll ans[N];
int sgn(ll x) {return x<0?-1:1;}
int main() {
scanf("%d%d%d", &n, &k, &x);
int f = 0;
REP(i,1,n) scanf("%lld", &e[i].w), e[i].id=i, f^=e[i].w<0;
if (!f) {
_ *r = max_element(e+1,e+1+n);
if (!r->w) --k,r->w-=x;
else {
int ff = sgn(r->w);
while (k) {
r->w -= sgn(r->w)*x, --k;
if (sgn(r->w)!=ff) break;
}
}
}
priority_queue<_> q;
REP(i,1,n) q.push(e[i]);
while (k) {
_ t = q.top(); q.pop();
t.w += sgn(t.w)*x;
q.push(t), --k;
}
while (q.size()) ans[q.top().id]=q.top().w,q.pop();
REP(i,1,n) printf("%lld ", ans[i]);hr;
}

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. php的__autoload和php的__call

    首先, __call是php语言自身就具有的一种语言features...,不是thinkphp的语言特征. 关于__call参考: http://love-love-l.blog.163.com/b ...

  2. LIS|计蒜客2019蓝桥杯省赛 B 组模拟赛(一)

    #include <bits/stdc++.h> using namespace std; const int N = 1e5 + 9; int f[N], a[N]; int n; // ...

  3. 论文笔记之:Heterogeneous Face Attribute Estimation: A Deep Multi-Task Learning Approach

    Heterogeneous Face Attribute Estimation: A Deep Multi-Task Learning Approach  2017.11.28 Introductio ...

  4. Linux 压缩、解压命令使用

    tar在Linux上是常用的打包.压缩.加压缩工具,他的参数很多,这里仅仅列举常用的压缩与解压缩参数 参数: -c :create 建立压缩档案的参数:(个人理解也就是打包) -x : 解压缩压缩档案 ...

  5. numa.h:No such file or directory 解决方法

    参考: numa.h:No such file or directory numa.h:No such file or directory 解决方法 Ubuntu: $ apt-get install ...

  6. 51nod 1069 Nim游戏 + BZOJ 1022: [SHOI2008]小约翰的游戏John(Nim游戏和Anti-Nim游戏)

    首先,51nod的那道题就是最简单的尼姆博弈问题. 尼姆博弈主要就是判断奇异局势,现在我们就假设有三个石子堆,最简单的(0,n,n)就是一个奇异局势,因为无论先手怎么拿,后手总是可以在另一堆里拿走相同 ...

  7. Select2 添加默认值

    折腾很久才解决问题 $.ajax({ url: '@Url.Action("GetSystemSzzdItem", "CangpinGushi")', type ...

  8. bean标签常用属性

    scope属性: singleton:只有一个 prototpye:每次创建都是新的 对象初始化方法: init-method 对象销毁方法: destroy-method

  9. 33 Python 详解命令解析 - argparse--更加详细--转载

    https://blog.csdn.net/lis_12/article/details/54618868 Python 详解命令行解析 - argparse Python 详解命令行解析 - arg ...

  10. _itemmod_stat

    制作几种基础模板,用模板快速生成装备 `comment` 备注 `entry`目标装备 entry `src_entry` 模板装备entry `stat_muilt` 属性倍率 `mindmg_mu ...