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

Codeforces Round #569 (Div. 2)ios

 

B. Nick and Arrayapp

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

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

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

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

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

If there are multiple answers, print any of them.ip

Inputci

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

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 }
相关文章
相关标签/搜索