其他分享
首页 > 其他分享> > Codeforces Round #569 (Div. 2) B. Nick and Array

Codeforces Round #569 (Div. 2) B. Nick and Array

作者:互联网

Codeforces Round #569 (Div. 2)

 

B. Nick and Array

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

 

题意:题目意思是给你n个数,你可以对任何一个数进行操作:num=-num-1,让你输出经过操作之后,n个数乘积最大的一个序列

思路:显然如果是正数或0,经过操作后变负数,绝对值加一,既然是绝对值增加,那么我们应该把这些数都变成负数,

那么乘积的绝对值才是最大的,但是显然乘积不能为负,那么我们就要对n判断一下,如果n是偶数,乘积为正值,就ok了;

如果n是奇数,那就要在n个数里面找一个负数绝对值最大的,操作后变正,则乘积变为正数最大值......

 

 

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<cmath>
 5 #include<algorithm>
 6 #include<map>
 7 #include<set>
 8 #include<vector>
 9 #include<queue>
10 using namespace std;
11 #define ll long long 
12 const int mod=1e9+7;
13 const int inf=1e9+7;
14 
15 const int maxn=1e5+5;
16 
17 int num[maxn];
18 
19 int main()
20 {
21     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
22     
23     int n;
24     
25     while(cin>>n)
26     {
27         
28         for(int i=0;i<n;i++)
29         {
30             cin>>num[i];
31             if(num[i]>=0)
32                 num[i]=-num[i]-1;
33         }
34         
35         if(n&1)
36         {
37             int minn=-1;
38             int mark_i=0;
39             
40             for(int i=0;i<n;i++)
41             {
42                 if(num[i]<minn)
43                 {
44                     minn=num[i];
45                     mark_i=i;
46                 }
47             }
48             
49             num[mark_i]=-num[mark_i]-1;
50             
51         }
52         
53         for(int i=0;i<n-1;i++)
54             cout<<num[i]<<" ";
55         cout<<num[n-1]<<endl;
56         
57     }
58     
59     return 0;
60 }

 

标签:569,elements,operation,int,Codeforces,num,array,include,Round
来源: https://www.cnblogs.com/xwl3109377858/p/11070253.html