CodeForces - 721D Maxim and Array (贪心)
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 koperations 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
5 3 1
5 4 3 5 2
5 4 3 5 -1
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime> #define fuck(x) cerr<<#x<<" = "<<x<<endl;
#define debug(a, x) cerr<<#a<<"["<<x<<"] = "<<a[x]<<endl;
#define ls (t<<1)
#define rs ((t<<1)|1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int loveisblue = ;
const int maxn = ;
const int maxm = ;
const int inf = 0x3f3f3f3f;
const ll Inf = ;
const int mod = ;
const double eps = 1e-;
const double pi = acos(-); int n,k;
struct node{
ll num,absnum;
int id;
bool operator<(const node &p)const{
return p.absnum<absnum;
}
}a[maxn];
priority_queue<node>q;
ll ans[maxn];
int main() {
// ios::sync_with_stdio(false);
// freopen("in.txt", "r", stdin); int n,k;
ll x;
scanf("%d%d%lld",&n,&k,&x);
for(int i=;i<=n;i++){
ll num;
scanf("%lld",&num);
a[i]=node{num,abs(num),i};
}
sort(a+,a++n);
int fu = ;
for(int i=;i<=n;i++){
if(a[i].num<){
fu++;
}
}
if(fu%==){
if(x*k>=a[n].absnum){
int p = min(1ll*k,a[n].absnum/x+);
k-=p;
if(a[n].num<){
a[n].num+=p*x;
a[n].absnum = abs(a[n].num);
}else{
a[n].num-=p*x;
a[n].absnum = abs(a[n].num);
}
}
}
fu = ;
for(int i=;i<=n;i++){
if(a[i].num<){
fu++;
}
}
for(int i=;i<=n;i++){
q.push(a[i]);
}
while (k--){
node exa = q.top();
q.pop();
if(fu&){
if(exa.num<){
exa.num-=x;
exa.absnum+=x;
}else{
exa.num+=x;
exa.absnum+=x;
}
}else{
if(exa.num<){
exa.num+=x;
exa.absnum-=x;
}else{
exa.num-=x;
exa.absnum-=x;
}
}
q.push(exa);
}
while (!q.empty()){
node exa = q.top();
q.pop();
ans[exa.id]=exa.num;
}
for(int i=;i<=n;i++){
printf("%lld ",ans[i]);
}
return ;
}
CodeForces - 721D Maxim and Array (贪心)的更多相关文章
- CodeForces 721D Maxim and Array
贪心,优先队列. 先看一下输入的数组乘积是正的还是负的. ①如果是负的,也就是接下来的操作肯定是让正的加大,负的减小.每次寻找一个绝对值最小的数操作就可以了. ②如果是正的,也是考虑绝对值,先操作绝对 ...
- Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心
D. Maxim and Array 题目连接: http://codeforces.com/contest/721/problem/D Description Recently Maxim has ...
- 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 ...
- Codeforces F. Maxim and Array(构造贪心)
题目描述: Maxim and Array time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- 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 ...
- Codeforces 442C Artem and Array(stack+贪婪)
题目连接:Codeforces 442C Artem and Array 题目大意:给出一个数组,每次删除一个数.删除一个数的得分为两边数的最小值,假设左右有一边不存在则算作0分. 问最大得分是多少. ...
- Codeforces Round #504 D. Array Restoration
Codeforces Round #504 D. Array Restoration 题目描述:有一个长度为\(n\)的序列\(a\),有\(q\)次操作,第\(i\)次选择一个区间,将区间里的数全部 ...
- codeforces Gym 100338E Numbers (贪心,实现)
题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...
- [Codeforces 1214A]Optimal Currency Exchange(贪心)
[Codeforces 1214A]Optimal Currency Exchange(贪心) 题面 题面较长,略 分析 这个A题稍微有点思维难度,比赛的时候被孙了一下 贪心的思路是,我们换面值越小的 ...
随机推荐
- PHPCMS快速建站系列之需要掌握的函数
路径:phpcms\libs\classes\model.class.php /** * 执行sql查询 * @param $where 查询条件[例`name`='$name'] * ...
- 微服务开源生态报告 No.5
「微服务开源生态报告」,汇集各个开源项目近期的社区动态,帮助开发者们更高效的了解到各开源项目的最新进展. 社区动态包括,但不限于:版本发布.人员动态.项目动态和规划.培训和活动. 非常欢迎国内其他微服 ...
- js函数易犯的错误
1.定义一个js函数时不能写在另一个函数里面. 2.定义一个打开网页自动执行的函数,一定要注意加载的顺序.如果是不是自动执行的,而是等页面加载完后事件触发的,那写在任何地方都没问题. 错误的:
- nodeJs学习-14 mysql数据库学习、Navicat管理工具
数据库: MySQL 免费.性能非常不错 缺点:集群.容灾稍微弱一点 Oracle 收费.大型应用.金融级.性能非常不错.集群.容灾非常强 缺点:贵 mySQL安装教程--nodeJsz智能社视频 ...
- dva与create-react-app的结合使用
dva与我们的create-react-app创建的两款脚手架是我们写react项目的两款优秀框架,之前一种使用create-react-app这款脚手架进行开发.然后这个框架美中不足的是redux方 ...
- dnspython
dnspython 一个Python实现的一个DNS工具包,利用其查询功能来实现dns的服务监控及解析结果的校验. 安装 pip install dnspython 解析域名为IP from dns ...
- iOS iOS8注册通知
http://blog.csdn.net/apple_app/article/details/39228221 极光推送 action设置 http://docs.jpush.cn/display/d ...
- mysql 中 DATE_ADD函数和 DATE_SUB函数用法
mysql 中 DATE_ADD(date,INTERVAL expr type) 和 DATE_SUB(date,INTERVAL expr type) 这些函数执行日期运算. date 是一个 D ...
- oracle避免在索引列上使用计算
WHERE子句中,如果索引列是函数的一部分.优化器将不使用索引而使用全表扫描. 举例: 低效: SELECT … FROM DEPT WHERE SAL * 12 > 25000; 高效: SE ...
- 割点(tarjan)
对于根来说,如果它有超过1棵子树,那么它是一个割点 对于非叶结点来说,如果它的某一个儿子没有回边能到达高于它的点,那么它是一个割点 叶节点不是割点 //洛谷3388 #include<algor ...