题目描述:

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. Django Model 模型

    参考: https://www.runoob.com/django/django-model.html https://www.cnblogs.com/taosiyu/p/11260000.html ...

  2. [转]10 Best GTK Themes for Ubuntu 18.04

    原文地址:https://omgfoss.com/10-best-gtk-themes-ubuntu-18-04/

  3. [PHP] 浅谈 Laravel Authentication 的 auth:api

    auth:api 在 Laravel 的 Routing , Middleware , API Authentication 主题中都有出现. 一. 在 Routing 部分可以知道 auth:api ...

  4. Linux内核参数详解

    所谓Linux服务器内核参数优化(适合Apache.Nginx.Squid等多种web应用,特殊的业务有可能需要做略微调整),主要是指在Linux系统中针对业务服务应用而进行的系统内核参数调整,优化并 ...

  5. PHP不过过滤防止xss攻击的方法

    PHP不过过滤防止xss攻击的方法<pre> $content=htmlspecialchars($content); $pos=strpos($content,"\u" ...

  6. Spring Boot 项目的 API 接口防刷

    首先是写一个注解类 拦截器中实现 注册到springboot中 在Controller中加入注解 说明:使用了注解的方式进行对接口防刷的功能,非常高大上,本文章仅供参考 一,技术要点:springbo ...

  7. Linux下Ngnix的安装与配置

    由于我的博客项目在8084端口,需要Nginx来转发一下端口,记录一下安装过程和踩过的小坑. 一.下载 wget http://nginx.org/download/nginx-1.12.2.tar. ...

  8. 为何一个@LoadBalanced注解就能让RestTemplate拥有负载均衡的能力?【享学Spring Cloud】

    每篇一句 你应该思考:为什么往往完成比完美更重要? 前言 在Spring Cloud微服务应用体系中,远程调用都应负载均衡.我们在使用RestTemplate作为远程调用客户端的时候,开启负载均衡极其 ...

  9. Python的正则表达式和爬虫

    1.常用元字符 . 匹配除换行符以外的任意字符 \w 匹配字母或数字或下划线 \s 匹配任意的空白符 \d 匹配数字 \b 匹配单词的开始或结束 ^ 匹配字符串的开始 $ 匹配字符串的结束 2.常用限 ...

  10. jenkins+springboot+maven多模块部署

    一.jenkins的安装配置 1.去官网下载war包,这种方式比较简单方便 java -jar jenkins.war --httpPort=49001 2.首次运行有一个key放在服务器上需要你填入 ...