Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心+ 最小堆
来源:互联网 发布:滚动数组是什么 编辑:程序博客网 时间:2024/06/14 17:52
D. Maxim and Arraytime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputRecently 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.
InputThe 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.
OutputPrint 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.
Examplesinput5 3 15 4 3 5 2output5 4 3 5 -1input5 3 15 4 3 5 5output5 4 0 5 5input5 3 15 4 4 5 5output5 1 4 5 5input3 2 75 4 2output5 11 -5贪心,一开始的思路错了,一开始想先找出负数的个数,如果为偶数个则找到绝对值最小的那个将它变成异号,否则就不变。然后再找到绝对值最小的那个,将剩余的k全部加或者减在上面,就是这个地方有了问题,就比如3 2 3-2 4 6如果照着我的想法,就是 -8 4 6但是 -5 7 6明显更好,所以我想到了应该k的变化应该一次次进行,每次找到绝对值最小的那个进行变化。#include<iostream>#include<cstring>#include<cstdio>#include<vector>#include<queue>#include<algorithm>#include<cmath>#include<stack>const int inf=0x3f3f3f3f;typedef long long LL;using namespace std;const int MAXN=2e5+10;LL a[MAXN];LL b[MAXN];struct Node{ int id; LL value; Node(int i,LL v) { id=i; value=v; }};struct cmp{ bool operator()(Node a,Node b){ return a.value>b.value; }};int main(){ int n,k,x; while(cin>>n>>k>>x) { int sum=0; for(int i=1;i<=n;i++) { scanf("%I64d",&a[i]); b[i]=a[i]; if(a[i]<0) { sum++; b[i]*=-1LL; } } int h=0; if(sum%2==0) { LL MIN=b[1]; int index=1; for(int i=2;i<=n;i++) { if(MIN>b[i]) { MIN=b[i]; index=i; } } int t=k; if(MIN/x+1<=k) { b[index]-=LL(MIN/x+1)*LL(x); k-=(MIN/x+1); } else { b[index]-=LL(k*x); k=0; } if(a[index]<0) { a[index]+=LL(t-k)*LL(x); } else { a[index]-=LL(t-k)*LL(x); } } if(k) { for(int i=1;i<=n;i++) { b[i]=a[i]; if(b[i]<0) { b[i]*=(-1LL); } } priority_queue<Node,vector<Node>,cmp> Q; for(int i=1;i<=n;i++) { Q.push(Node(i,b[i])); } while(k) { Node u=Q.top(); Q.pop(); b[u.id]+=LL(x); Q.push(Node(u.id,b[u.id])); k--; } for(int i=1;i<=n;i++) { if(a[i]<0) { a[i]=b[i]*-1LL; } else { a[i]=b[i]; } } } printf("%I64d",a[1]); for(int i=2;i<=n;i++) { printf(" %I64d",a[i]); } cout<<endl; }}
0 0
- Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心+ 最小堆
- Codeforces Round #374 (Div. 2)D. Maxim and Array(贪心)
- Codeforces Round 374 (Div 2)D Maxim and Array 【贪心】
- Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心
- Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心
- Codeforces Round #374 (Div. 2) D - Maxim and Array
- Codeforces Round #374 (Div. 2) D. Maxim and Array
- Codeforces Round #374 (Div. 2) D. Maxim and Array
- Codeforces Round #374 (Div. 2) D. Maxim and Array —— 贪心
- 【Codeforces Round 374 (Div 2)D】【贪心】 Maxim and Array n个数做K次±X使得乘积尽可能小
- Codeforces 721D Maxim and Array[贪心]
- codeforces 721D Maxim and Array(贪心)
- codeforces 721D. Maxim and Array(贪心)
- CodeForces 721D. Maxim and Array(贪心,思维题)
- Codeforces712 D. Maxim and Array (贪心)
- 【codeforces 721D】【贪心 堆】D. Maxim and Array 【给出n个数,k次机会,每次使得任意一个数字减少或者加上x,使得最后的乘积最小,最后的n个数大小】
- 721D Maxim and Array codeforces(模拟)
- 【24.17%】【codeforces 721D】Maxim and Array
- 关于cookie——setPath/setDomain
- [webGL学习]基于three.js构建WebGL实例讲解大纲
- golang 本人学习
- android:DDMS查看Threads
- OpenDaylight的Beryllium (铍)版本安装与配置
- Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心+ 最小堆
- Iaas Paas Saas
- ssh三大框架整合(2)
- 栈操作和应用—括号匹配
- angularjs 表单验证 文本框失焦点验证
- luogu解题报告:P3391文艺平衡树
- e恩win
- DSP28335—CMD文件中的各个段解释
- 项目实战:QuickHit
原创粉丝点击热门IT博客热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 皮质的包包脏了怎么办 包真皮座椅后悔怎么办 煮鸡蛋不好剥皮怎么办 脸上经常爆皮怎么办 要塞物资满了怎么办 小阴唇磨裤子怎么办 包皮的失败了怎么办 老感觉身边有鬼怎么办 婆婆精神病我该怎么办 咽炎老想咳嗽怎么办 硝呋胶囊塞不进怎么办 怀孕了得了霉菌怎么办 狗长针眼了怎么办 艾滋病cd4是0怎么办 妇科有轻度炎症怎么办 有脚气和灰指甲怎么办 想要换车牌号码怎么办 车牌被套牌违章怎么办 车牌被套牌了怎么办 如果汽车被套牌怎么办 异地车牌被偷怎么办 包皮龟头炎红点怎么办 乙肝肝纤维化该怎么办 大三阳肝纤维化怎么办 皮肤经常长癣怎么办 得了水泡型手癣怎么办 寻常型皮肤癣怎么办 腿上长了癣发痒怎么办 大腿内侧长癣怎么办 大腿内侧黑黑的怎么办 大腿根部黑黑的怎么办 痈肿起脓包了怎么办 腹股沟皮肤淹了怎么办 脸颊两侧很红怎么办 脸长期发红发痒怎么办 脸突然发红发痒怎么办 脸反复发红发痒怎么办 脸脱皮干燥发痒怎么办 脸过敏干燥脱皮怎么办 鼻子根部红红的怎么办 大腿内侧坏了怎么办程序博客网,程序员的互联网技术博客家园。csdn论坛精品 msdn技术资料都在这里