D. Maxim and Array(贪心+优先队列模板)
2017-03-30 16:18
302 查看
D. Maxim and Array
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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 k operations 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
input
output
input
output
input
output
input
output
题目大意:给你n个数,然后给你K次改变机会,可以让n个数中的任意一个数增加或者减少x,求k次改变后的数使之乘积最小
解题思路:要让乘积最小,肯定是负数最小,所以判断负数的个数,然后负数个数奇数可以乘积最小,然后再去使绝对值最小的数字不断增加x,这道题用了优先队列,之前没有用过,所以当作模板去处理
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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 k operations 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
input
5 3 1 5 4 3 5 2
output
5 4 3 5 -1
input
5 3 1 5 4 3 5 5
output
5 4 0 5 5
input
5 3 1 5 4 4 5 5
output
5 1 4 5 5
input
3 2 7 5 4 2
output
5 11 -5
题目大意:给你n个数,然后给你K次改变机会,可以让n个数中的任意一个数增加或者减少x,求k次改变后的数使之乘积最小
解题思路:要让乘积最小,肯定是负数最小,所以判断负数的个数,然后负数个数奇数可以乘积最小,然后再去使绝对值最小的数字不断增加x,这道题用了优先队列,之前没有用过,所以当作模板去处理
#include<iostream> #include<cstdio> #include<stdio.h> #include<cstring> #include<cstdio> #include<climits> #include<cmath> #include<vector> #include <bitset> #include<algorithm> #include <queue> #include<map> using namespace std; struct point { friend bool operator< (point n1, point n2) { return n1.sum> n2.sum; } long long int sum; long long int i; int flag; }poin[200005], pp; priority_queue<point> qua; bool cmp(point x, point y) { return x.sum < y.sum; } bool cmp1(point x, point y) { return x.i < y.i; } long long int n, k, x, i, sum, ss; long long int a[200005]; int main() { cin >> n >> k >> x; sum = 0; for (i = 1; i <= n; i++) { cin >> ss; if (ss < 0) { poin[i].sum = -ss; poin[i].flag = -1; sum++; } else { poin[i].sum = ss; poin[i].flag = 1; } poin[i].i = i; qua.push(poin[i]); } if (sum % 2 == 0) { pp = qua.top(); qua.pop(); while (pp.sum>=0) { pp.sum -= x; k--; if (k == 0) { break; } } if (pp.sum < 0) { pp.flag = -pp.flag; pp.sum = -pp.sum; } qua.push(pp); } while (k--) { pp = qua.top(); qua.pop(); pp.sum += x; qua.push(pp); } while (!qua.empty()) { pp = qua.top(); qua.pop(); if (pp.flag == 1) { a[pp.i] = pp.sum; } else { a[pp.i] = -pp.sum; } } for (i = 1; i < n; i++) { cout << a[i] << " "; } cout << a[i] << endl; }
相关文章推荐
- Codeforces712 D. Maxim and Array (贪心)
- 【codeforces 721D】【贪心 堆】D. Maxim and Array 【给出n个数,k次机会,每次使得任意一个数字减少或者加上x,使得最后的乘积最小,最后的n个数大小】
- 【Codeforces Round 374 (Div 2)D】【贪心】 Maxim and Array n个数做K次±X使得乘积尽可能小
- codeforces 721D. Maxim and Array(贪心)
- codeforces 721D D. Maxim and Array (STL||优先队列)
- Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心
- CodeForces 721D. 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 #331 (Div. 2) B. Wilbur and Array (贪心)
- Codeforces Round #390 (Div. 2) D. Fedor and coupons 贪心+优先队列
- 【Codeforces Round 331 (Div 2)B】【水题 贪心】Wilbur and Array 修改对p位置后的所有位置生效 最少修改次数使得数列变成b
- CodeForces 596 B. Wilbur and Array(贪心)
- Codeforces 361C - Levko and Array Recovery(贪心)
- Codeforces #253 (Div. 1)C. Artem and Array ( 贪心
- 水题(Maxim and Array,cf 721D)