题目描述:

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. AspNetCore 限流中间件IpRateLimitMiddleware 介绍

    IpRateLimitMiddleware(Github: AspNetCoreRateLimit) 是ASPNETCore的一个限流的中间件,用于控制客户端调用API的频次, 如果客户端频繁访问服务 ...

  2. 【C/C++开发】C++11:左值引用VS右值引用

    左值引用VS右值引用 左值引用对于一般的C++程序员再熟悉不过,但对于右值引用(C++0X新特性),就稍微有点不知所云 左值VS右值 在定义变量的时候,经常会用到左值和右值,比如:int a = 1; ...

  3. centos7安装配置MariaDB10

    1:添加 MariaDB yum 仓库 vi /etc/yum.repos.d/MariaDB.repo在该文件中添加以下内容保存: [mariadb] name = MariaDB baseurl ...

  4. 1 linux性能优化之平均负载uptime

    不知道onenote的笔记复制出来就是图片了...

  5. Redis Sentinel分布式集群

    helm部署Redis哨兵分布式集群 Redis Sentinel集群 介绍 Redis Sentinel集群是由若干Sentinel节点组成的分布式集群,可以实现故障发现.故障自动转移.配置中心和客 ...

  6. MAC——Homebrew介绍和使用

    一.Homebrew是什么 Homebrew是一款Mac OS平台下的软件包管理工具,拥有安装.卸载.更新.查看.搜索等很多实用的功能.简单的一条指令,就可以实现包管理,而不用你关心各种依赖和文件路径 ...

  7. Linux字符设备驱动基本结构

    1.Linux字符设备驱动的基本结构 Linux系统下具有三种设备,分别是字符设备.块设备和网络设备,Linux下的字符设备是指只能一个字节一个字节读写的设备,不能随机读取设备内存中某一数据,读取数据 ...

  8. 【LEETCODE】73、根据身高重建队列 第406题

    说实话,这道题我没想出来,但是看解题报告题解比较让人觉得眼前一亮,这里记录下来 package y2019.Algorithm.greedy.medium; import java.util.Arra ...

  9. 线程池及Executor框架

    1.为什么要使用线程池? ​ 诸如 Web 服务器.数据库服务器.文件服务器或邮件服务器之类的许多服务器应用程序都面向处理来自某些远程来源的大量短小的任务.请求以某种方式到达服务器,这种方式可能是通过 ...

  10. 2019三六零 java面试笔试题 (含面试题解析)

    本人3年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.360等公司offer,岗位是Java后端开发,最终选择去了三六零. 面试了很多家公司,感觉大部分公司考察的点都差 ...