Codeforces Round #569 (Div. 2)ios
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 }