大意: 给定序列, 每次操作选择一个数+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. 比较好的一些 ConcurrentHashMap讲解博客

    jdk8 https://blog.csdn.net/jianghuxiaojin/article/details/52006118#commentBox jdk7.8 https://crossov ...

  2. Cannot retrieve metalink for repository: epel 错误解决办法

    centos下安装完EPEL源, 然后更新一下yum缓存, 如果发现这样的错误:Error: Cannot retrieve metalink for repository: epel. Please ...

  3. HIHOcoder 1403 后缀数组一·重复旋律

    思路 后缀数组的板子题,注意后缀数组的rank[]数组是通过位置找到对应排名的,sa[]是通过排名找到位置的,height[i]记录的是sa[i]和sa[i+1]之间的lcp 不要写错了就行了 代码 ...

  4. (转载)C#:Enum、Int和String的互相转换,枚举转换

    Enum为枚举提供基类,其基础类型可以是除 Char 外的任何整型.如果没有显式声明基础类型,则使用 Int32.编程语言通常提供语法来声明由一组已命名的常数和它们的值组成的枚举.注意:枚举类型的基类 ...

  5. java中Properties类及读取properties中属性值

    本文为博主原创,未经允许不得转载: 在项目的应用中,经常将一些配置放入properties文件中,在代码应用中读取properties文件,就需要专门的类Properties类,通过这个类可以进行读取 ...

  6. 正则解析json数据

    http://tool.chinaz.com/regex http://tool.oschina.net/regex/

  7. ERR! registry error parsing json

    报错日志: ERR! registry error parsing json ERR! registry error parsing json 解决过程: 从github上克隆一个项目,在npm i的 ...

  8. Selenium 页面自动化测试 面试 问题汇总

    1.   专业技术 在学习完Selenium的大部分接口或者方法之后,你可能会去面试自动化测试,主要是Selenium的自动化测试.下面这些问题总结,可能会对你有所帮助. 什么是Selenium? S ...

  9. eclipse中启动 Eclipse 弹出“Failed to load the JNI shared library jvm.dll”错误

    原因1:给定目录下jvm.dll不存在. 对策:(1)重新安装jre或者jdk并配置好环境变量.(2)copy一个jvm.dll放在该目录下. 原因2:eclipse的版本与jre或者jdk版本不一致 ...

  10. 将矩阵数据转换为栅格图 filled.contour()

    require(grDevices) # for colours filled.contour(volcano, color = terrain.colors, asp = 1) # simple x ...