题目描述:

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 a**i either with a**i + x or with a**i - 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, ..., a**n () — the elements of the array found by Maxim.

Output

Print n integers b1, b2, ..., b**n 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

Copy

5 3 1
5 4 3 5 2

Output

Copy

5 4 3 5 -1

Input

Copy

5 3 1
5 4 3 5 5

Output

Copy

5 4 0 5 5

Input

Copy

5 3 1
5 4 4 5 5

Output

Copy

5 1 4 5 5

Input

Copy

3 2 7
5 4 2

Output

Copy

5 11 -5

思路:

题目是要求给一个数列,k次操作在某个数上加或减x,让数列乘积最小。因为数有正负,要分情况讨论。

如果现在负数个数是偶数,就是乘积是个正数,应该相办法让它变成负,就让绝对值最小的变,因为它距离零最近。如果要变的是正数,就减x,如果是负数要变,就加x,即使不能让乘积编号也可以让乘积变小。

如果现在负数个数是奇数,乘积是个负数,我们要让乘积的绝对值更大来使乘积更小。就变绝对值最小的,是正数就加x,是负数就减x,因为当一堆数大小越接近,这堆数的乘积越大。

注意每次变化要更新负数的个数,要快速取得绝对值最小的元素,要维护一个结构体的优先级队列,需要在结构体的定义里重载<运算符。

刚开始我沙雕用了两个队列,一个存整数,一个存负数,每次选绝对值小的还要比较,\(if-else\)写了一大堆最后还错了。-_-||详见后面的代码。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
#define max_n 200005
using namespace std;
int n,k,x;
long long a[max_n];
struct node
{
int id;
long long val;
bool operator<(const node& a) const
{
return abs(val-0)>abs(a.val-0);
}
};
int cnt = 0;
priority_queue<node> que;
int main()
{
cin >> n >> k >> x;
for(int i = 0;i<n;i++)
{
cin >> a[i];
node nw;
nw.val = a[i];
nw.id = i;
if(a[i]<0)
{
cnt++;
}
que.push(nw);
}
while(k)
{
node nw = que.top();
int id = nw.id;
if(cnt%2==0)
{
if(a[id]<0)
{
a[id] += x;
if(a[id]>=0)
{
cnt--;
}
}
else
{
a[id] -= x;
if(a[id]<0)
{
cnt++;
}
}
}
else
{
if(a[id]<0)
{
a[id] -= x;
}
else
{
a[id] += x;
}
}
nw.val = a[id];
que.pop();
que.push(nw);
k--;
}
for(int i = 0;i<n;i++)
{
cout << a[i] << " ";
}
cout << endl;
}

不明哪里写错的代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
#define max_n 200005
using namespace std;
long long n,k,x;
long long a[max_n];
struct node
{
int id;
long long val;
bool operator<(const node& a) const
{
return abs(val-0)>abs(a.val-0);
}
};
priority_queue<node> fque;
priority_queue<node> zque;
int main()
{
cin >> n >> k >> x;
for(int i = 0;i<n;i++)
{
cin >> a[i];
node nw;
nw.val = a[i];
nw.id = i;
if(a[i]<0)
{ fque.push(nw);
}
else
{
zque.push(nw);
}
}
//cout << "input " << endl;
while(k)
{
/*for(int i = 0;i<n;i++)
{
cout << a[i] << " ";
}
cout << endl;*/
if(fque.size()%2==0)
{
if(fque.size()==0)
{
int id = zque.top().id;
a[id] -= x;
node nw;
nw.id = id;
nw.val = a[id];
if(a[id]<0)
{
zque.pop();
fque.push(nw);
}
else
{
zque.pop();
zque.push(nw);
}
}
else if(zque.size()==0)
{
int id = fque.top().id;
a[id] += x;
node nw;
nw.id = id;
nw.val = a[id];
if(a[id]>=0)
{
fque.pop();
zque.push(nw);
}
else
{
fque.pop();
fque.push(nw);
}
}
else
{
int gapz = abs(zque.top().val-0);
int idz = zque.top().id;
int gapf = abs(fque.top().val-0);
int idf = fque.top().id;
if(gapz<gapf)
{
a[idz] -= x;
node nw;
nw.id = idz;
nw.val = a[idz];
if(a[idz]<0)
{
zque.pop();
fque.push(nw);
}
else
{
zque.pop();
zque.push(nw);
}
}
else
{
a[idf] += x;
node nw;
nw.id = idf;
nw.val = a[idf];
if(a[idf]>=0)
{
fque.pop();
zque.push(nw);
}
else
{
fque.pop();
fque.push(nw);
}
}
}
}
else
{
if(zque.size()==0)
{
int id = fque.top().id;
a[id] -= x;
node nw;
nw.id = id;
nw.val = a[id];
fque.pop();
fque.push(nw);
}
else
{
int gapz = abs(zque.top().val-0);
int idz = zque.top().id;
int gapf = abs(fque.top().val-0);
int idf = fque.top().id;
if(gapz<gapf)
{
a[idz] += x;
zque.pop();
node nw;
nw.id = idz;
nw.val = a[idz];
zque.push(nw);
}
else
{
a[idf] -= x;
fque.pop();
node nw;
nw.id = idf;
nw.val = a[idf];
fque.push(nw);
}
}
}
k--;
}
for(int i = 0;i<n;i++)
{
cout << a[i] << " ";
}
cout << endl;
return 0; }

参考文章:

木流牛马,D. Maxim and Array,https://www.cnblogs.com/thunder-110/p/9340279.html

Codeforces F. Maxim and Array(构造贪心)的更多相关文章

  1. CodeForces 721D Maxim and Array

    贪心,优先队列. 先看一下输入的数组乘积是正的还是负的. ①如果是负的,也就是接下来的操作肯定是让正的加大,负的减小.每次寻找一个绝对值最小的数操作就可以了. ②如果是正的,也是考虑绝对值,先操作绝对 ...

  2. Codeforces G. Nick and Array(贪心)

    题目描述: Nick had received an awesome array of integers a=[a1,a2,…,an] as a gift for his 5 birthday fro ...

  3. Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心

    D. Maxim and Array 题目连接: http://codeforces.com/contest/721/problem/D Description Recently Maxim has ...

  4. 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 ...

  5. 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 ...

  6. Educational Codeforces Round 56 (Rated for Div. 2) F - Vasya and Array dp好题

    F - Vasya and Array dp[ i ][ j ] 表示用了前 i 个数字并且最后一个数字是 j 的方案数. dp[ i ][ j ] = sumdp [i - 1 ][ j ], 这样 ...

  7. Codeforces 437C The Child and Toy(贪心)

    题目连接:Codeforces 437C  The Child and Toy 贪心,每条绳子都是须要割断的,那就先割断最大值相应的那部分周围的绳子. #include <iostream> ...

  8. Codeforces 442C Artem and Array(stack+贪婪)

    题目连接:Codeforces 442C Artem and Array 题目大意:给出一个数组,每次删除一个数.删除一个数的得分为两边数的最小值,假设左右有一边不存在则算作0分. 问最大得分是多少. ...

  9. Codeforces Round #546 (Div. 2) D 贪心 + 思维

    https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...

随机推荐

  1. 1.Python爬虫入门_urllib

    #2019-11-22 import urllib.request #Pthon自带的网络连接库 import gzip #解压缩库 #程序入口 if __name__=='__main__': #u ...

  2. Oracle存储过程(包:PACK_KPI_KERNEL For YS三度评价体系)

    CREATE OR REPLACE PACKAGE PACK_KPI_KERNEL IS --定义多级数组 字符串 TYPE TSTRARRY ) INDEX BY BINARY_INTEGER; T ...

  3. python3.5.3rc1学习十一:字典与模块

    #os模块import oscurDir = os.getcwd()print(curDir) os.mkdir("新建") import timetime.sleep(2)os. ...

  4. 【CSP-S 2019】D2T1 Emiya 家今天的饭

    Description 传送门 Solution 算法1 32pts 爆搜,复杂度\(O((m+1)^n)\) 算法2 84pts 裸的dp,复杂度\(O(n^3m)\) 首先有一个显然的性质要知道: ...

  5. 第十 构建Web内容的技术

    第十章 构建Web内容的技术 一.HTML HTML(HyperText Markup Language,超文本标记语言)是为了发送Web 上的超文本(Hypertext)而开发的标记语言.超文本是一 ...

  6. bzoj2093 Frog

    题目链接 思路 非常有趣的一道题. 先考虑如何找出第K远的位置. 因为给出的序列是单调的,所以对于位置\(i\)的前\(K\)远位置肯定是一个包含位置\(i\)的长度为\(k+1\)的区间.我们用\( ...

  7. ubuntu 查看版本

    cat /etc/proc 可以查看是16.04还是18.04

  8. 在windows上搭建git服务器教程

    1.首先,需要确保windows系统上安装并配置了Java运行环境,JDK>=1.7. 2.下载Gitblit,下载地址:http://www.gitblit.com/ 3.解压缩下载的压缩包即 ...

  9. Shell基本运算符之字符串运算符

    Shell基本运算符 1.字符串运算符 常用的字符串运算符 运算符 说明 例子 = 检测两字符串是否相等,相等返回true [ $a = $b ] != 检测两个字符串是否部相等,不相等返回true ...

  10. Vue devtool插件安装后无法使用,提示“vue.js not detected”的解决方法

    vue devtool下载 极简插件  github vue devtool安装 点击谷歌浏览器箭头所指图标-更多工具-扩展程序   ①:直接将后缀为crx的安装包拖进下图区域即可自动安装     ② ...