Codeforces Round #374 (Div. 2) D - Maxim and Array
来源:互联网 发布:预测算法有哪些 编辑:程序博客网 时间:2024/05/21 15:01
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 integerx and decided to add or subtract it from arbitrary array elements. Formally, by applying single operation Maxim chooses integeri (1 ≤ i ≤ n) and replaces thei-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 thank operations to it. Please help him in that.
The first line of the input contains three integers n, k andx (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.
Print n integers b1, b2, ..., bn in the only line — the array elements after applying no more thank operations to the array. In particular, should stay true for every 1 ≤ i ≤ n, but the product of all array elements should beminimum possible.
If there are multiple answers, print any of them.
5 3 15 4 3 5 2
5 4 3 5 -1
5 3 15 4 3 5 5
5 4 0 5 5
5 3 15 4 4 5 5
5 1 4 5 5
3 2 75 4 2
5 11 -5
题意:
有n个数,有k次操作,每次操作能选一个数+或者-x;
要求n个数的乘积最小;
思路:
贪心;
当负数为偶数个数的时候:
绝对值最小的值为正数: -x
绝对值最小的值为负数: +x
当负数为奇数个数的时候:
绝对值最小的值为正数: +x
绝对值最小的值为负数: -x
代码:
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <queue>using namespace std;#define LL __int64struct node { int id; LL num; bool operator < (const node &a)const{ return abs(num)>abs(a.num); }};priority_queue <node>Q;const int maxn =200005;LL data[maxn];int main(){ int n,k,x; scanf("%d%d%d",&n,&k,&x); int i,j; int flag=1; for(i=1;i<=n;i++){ scanf("%I64d",&data[i]); if(data[i]<0) flag=-flag; Q.push( (node){i,data[i]} ); } for(i=1;i<=k;i++){ node nflag=Q.top(); Q.pop(); if(nflag.num<0){ if(flag<0){ nflag.num-=x; data[nflag.id]-=x; } else{ nflag.num+=x; data[nflag.id]+=x; if(nflag.num>=0) flag*=-1; } } else{ if(flag<0){ nflag.num+=x; data[nflag.id]+=x; } else{ nflag.num-=x; data[nflag.id]-=x; if(nflag.num<0) flag*=-1; } } Q.push(nflag); } for(i=1;i<=n;i++) printf("%I64d ",data[i]); printf("\n"); return 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(贪心)
- 721D Maxim and Array codeforces(模拟)
- 【24.17%】【codeforces 721D】Maxim and Array
- Codeforces Round #136 (Div. 2) D. Little Elephant and Array
- Codeforces Round #136 (Div. 2) D. Little Elephant and Array
- codeforces 721D D. Maxim and Array (STL||优先队列)
- codeforces 721D. Maxim and Array(贪心)
- python queue和多线程的爬虫 与 JoinableQueue和多进程的爬虫
- NFC模块
- YARN与MRv1对比
- PHP特性整合(PHP5.X到PHP7.1.x)
- Windows内存管理的方式
- Codeforces Round #374 (Div. 2) D - Maxim and Array
- RecyclerView 之notifyDataSetChanged的暴躁
- SQLite文件格式初步分析之记录格式
- windows下安装redis
- Codeforces Round #374 (Div. 2) D. Maxim and Array
- 前端学习 按钮制作
- Describing People: A Poselet-Based Approach to Attribute Classification
- 27. 安全 HTTP (2)
- 异常笔试题