链接:https://codeforces.ml/contest/1180/problem/B
Nick had received an awesome array of integers a=[a1,a2,…,an]a=[a1,a2,…,an] as a gift for his 55 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⋅…ana1⋅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 ii (1≤i≤n1≤i≤n) and do ai:=−ai−1ai:=−ai−1.
For example, he can change array [3,−1,−4,1][3,−1,−4,1] to an array [−4,−1,3,1][−4,−1,3,1] after applying this operation to elements with indices i=1i=1and i=3i=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⋅…ana1⋅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 nn (1≤n≤1051≤n≤105) — number of integers in the array.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (−106≤ai≤106−106≤ai≤106) — elements of the array
Output
Print nn 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
Copy
4 2 2 2 2
output
Copy
-3 -3 -3 -3
input
Copy
1 0
output
Copy
0
input
Copy
3 -3 -3 2
output
Copy
-3 -3 2
代码:
#include<bits/stdc++.h>
using namespace std;
long long n,m,t,k,l,ans,flag,a1,b1,c1,s;
struct node{
int x;
int id;
}a[10000001];
bool cmp(node p,node q)
{
return p.x<q.x;
}
bool bmp(node p,node q)
{
return p.id<q.id;
}
int main()
{
cin>>n;
s=0;
for(int i=1;i<=n;i++)
{
cin>>a[i].x;
a[i].id=i;
if(a[i].x<0)
a[i].x=-a[i].x-1;
}
sort(a+1,a+1+n,cmp);
if(n%2==1)
k=n-1;
else
k=n;
for(int i=1;i<=k;i++)
{
a[i].x=-a[i].x-1;
}
sort(a+1,a+1+n,bmp);
for(int i=1;i<=n;i++)
cout<<a[i].x<<" ";
}