题目描述:

Nick had received an awesome array of integers a=[a1,a2,…,an] as a gift for his 5 birthday from his mother. He was already going to explore its various properties but after unpacking he was disappointed a lot because the product a1⋅a2⋅…an of its elements seemed to him not large enough.

He was ready to throw out the array, but his mother reassured him. She told him, that array would not be spoiled after the following operation: choose any index i (1≤i≤n) and do ai:=−ai−1.

For example, he can change array [3,−1,−4,1] to an array [−4,−1,3,1] after applying this operation to elements with indices i=1and i=3.

Kolya had immediately understood that sometimes it's possible to increase the product of integers of the array a lot. Now he has decided that he wants to get an array with the maximal possible product of integers using only this operation with its elements (possibly zero, one or more times, as many as he wants), it is not forbidden to do this operation several times for the same index.

Help Kolya and print the array with the maximal possible product of elements a1⋅a2⋅…an which can be received using only this operation in some order.

If there are multiple answers, print any of them.

Input

The first line contains integer n (1≤n≤105) — number of integers in the array.

The second line contains n integers a1,a2,…,an (−106≤ai≤106) — elements of the array

Output

Print n numbers — elements of the array with the maximal possible product of elements which can be received using only this operation in some order from the given array.

If there are multiple answers, print any of them.

Examples

input

4

2 2 2 2

output

-3 -3 -3 -3

input

1

0

output

0

input

3

-3 -3 2

output

-3 -3 2

思路:

题目是要通过给定的变换,让最后得到的数列的积最大。可以看到正数做了一次变换后绝对值就变大了,负数相反,而且做两次同样的变换相当于没做变换。把所有数都变味负数,如果这个时候元素的个数为偶数,把所有整数变到负数就ok了;如果是奇数个,就想一想该把哪一个负数变成正数乘积最大。

哪一个呢?首先-1肯定不能乱变,变了就是0。

刚开始以为是最大的那个数(绝对值最小除了-1外)变成正数,我就先排了个序,还用了什么二分lower_bound,upper_bound,又加个<type>()变成相反地意思,后来发现输出要按顺序,就改写成结构体形式,自己写了一个二分搞得很复杂。

最后,去的应该是是绝对值最大的元素。-_-||就不用费尽心思把-1挑出去了。

为什么呢?证明一下:假设0<a1<a2<...<an,a1*a2*...*an的积(忽略符号,a1,a2,...,看成是对应元素的绝对值),现在忽略负号的影响,也就是经过变换现在假设他们只要将一个数做变换成正,最后乘积就是正数的情况下,变a1为a1-1,和变an为an-1哪个大。

(a1-1)*a2*...*an-a1*a2*...*(an-1)=(a1-an)*(a2*...*a(n-1))<0,也就是变绝对值大的为正数就好了。

知识点:

 template <class ForwardIterator, class T, class Compare>
ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last,
const T& val, Compare comp);//自定义比较函数

lower_bound( begin,end,num,greater<type>() ):

从数组的begin位置到end-1位置二分查找第一个小于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。

对应的upper_bound同理(具体见参考文章)

代码:

 #include <iostream>
#include <algorithm>
#define max_n 100005
using namespace std;
struct node
{
int val;
int ord;
};
node a[max_n];
int n;
int cmp1(node a,node b)
{
return a.val<b.val;
}
int cmp2(node a,node b)
{
return a.ord<b.ord;
}
int bisearch(int num,int l,int r)
{
int mid = ;
while(l<r)
{
mid = (l+r)>>;
//cout << l << " " << r << endl;
if(a[mid].val<num)
{
l = mid+;
}
else //if(a[mid].val>num)
{
r = mid;
}
}
return mid;
}
int main()
{
cin >> n;
for(int i = ;i<n;i++)
{
cin >> a[i].val;
a[i].ord = i;
if(a[i].val>=)
{
a[i].val = -a[i].val-;
}
}
/*for(int i = 0;i<n;i++)
{
cout << a[i].val << " ";
}
cout << endl;*/
if(n%!=)
{
sort(a,a+n,cmp1);
a[].val = -a[].val-;
sort(a,a+n,cmp2);
for(int i = ;i<n;i++)
{
cout << a[i].val << " ";
}
cout << endl;
}
else
{ for(int i = ;i<n;i++)
{
cout << a[i].val << " ";
}
cout << endl;
}
return ;
}

参考文章:

讲解lower_bound(虽然这题好像用不上)

brandong,关于lower_bound( )和upper_bound( )的常见用法,https://blog.csdn.net/qq_40160605/article/details/80150252

Andywu_0010,lower_bound()函数和upper_bound()函数,以及二分查找,https://i.cnblogs.com/EditPosts.aspx?postid=11249693&update=1

Codeforces G. Nick and Array(贪心)的更多相关文章

  1. Codeforces Round #569 (Div. 2) 题解A - Alex and a Rhombus+B - Nick and Array+C - Valeriy and Dequ+D - Tolik and His Uncle

    A. Alex and a Rhombus time limit per test1 second memory limit per test256 megabytes inputstandard i ...

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

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

  3. Codeforces Round #504 D. Array Restoration

    Codeforces Round #504 D. Array Restoration 题目描述:有一个长度为\(n\)的序列\(a\),有\(q\)次操作,第\(i\)次选择一个区间,将区间里的数全部 ...

  4. codeforces Gym 100338E Numbers (贪心,实现)

    题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...

  5. ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (思维,贪心)

    ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (思维,贪心) Trace 问答问题反馈 只看题面 35.78% 1000ms 262144K There's a beach in t ...

  6. [Codeforces 1214A]Optimal Currency Exchange(贪心)

    [Codeforces 1214A]Optimal Currency Exchange(贪心) 题面 题面较长,略 分析 这个A题稍微有点思维难度,比赛的时候被孙了一下 贪心的思路是,我们换面值越小的 ...

  7. Codeforces 754A Lesha and array splitting(简单贪心)

    A. Lesha and array splitting time limit per test:2 seconds memory limit per test:256 megabytes input ...

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

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

  9. Codeforces Round #258 (Div. 2) . Sort the Array 贪心

    B. Sort the Array 题目连接: http://codeforces.com/contest/451/problem/B Description Being a programmer, ...

随机推荐

  1. Jsp编写的页面如何适应手机浏览器页面

    经常遇到JSP网页需要适配手机设备的尺寸问题 解决: 在JSP加入<meta name="viewport" content="width=device-width ...

  2. nginx的rtmp搭建流媒体服务器实现直播流

    最近自己搞了一个用nginx的rtmp来搭建流媒体服务器,从而实现直播的过程,参考了网上很多资料,有些资料对于初学者来说比较难以理解,在此将我搭建的过程记录下来,分享给大家. 1.下载nginx-rt ...

  3. xunit 单元测试

    代码:GitHub 参考地址:https://github.com/Acumatica/xunit.autofac xunit +autofac进行单元测试 ①创建一个类库 引用nuget: xuni ...

  4. ReflectionUtils.invokeMethod的作用

    Object invokeMethod(Method method, Object target, Object... args)在指定对象(target)上,使用指定参数(args),执行方法(me ...

  5. AtCoder-arc058(题解)

    A - こだわり者いろはちゃん / Iroha's Obsession(暴力) 题目链接 题目大意: 给你 \(k\) 个个位数字和一个数字 \(n\) ,要求找到一个大于等于n的数字,使得不出现 \ ...

  6. Flask源码之:配置加载

    加载配置文件的思路: 1. 读取配置文件中的所有键值对,并将键值对全都放到Config对象.(Config是一个字典,因为它继承了Dict) 2. 把包含所有配置文件的Config对象,赋值给 app ...

  7. OracleVM桥接网卡无法获取本地连接网卡

    问题现象 VM虚拟机采用桥接网卡时,界面名称为"未指定",无法获取本地连接对应网卡信息: 处理方式: 进入本地连接,选择本地连接右键进入属性设置窗口; 选择安装,单击服务选项后点击 ...

  8. Kafka理解

    1. 引言 最近使用Kafka做消息队列时,完成了基本的消息发送与接收,已上线运行.一方面防止出现Bug时自己不能及时定位问题,一方面网上的配置可能还可以更加优化,决定去了解下Kafka. 2. 配置 ...

  9. oracle查询包含在子表中的主表数据

    Oracle数据库,查询某表中包含在子表中的数据,子表中数据按特定条件来源于该父表,SQL命令如 ) a_table父表,b_table子表,a和b表都有commandId列,a表的commandId ...

  10. 倾斜动画(SkewTransform)

    Silverlight中的倾斜变化动画(SkewTransform)能够实现对象元素的水平.垂直方向的倾斜变化动画效果.我们现实生活中的倾斜变化效果是非常常见的,比如翻书的纸张效果,关门开门的时候门缝 ...