B. Nick and Array 贪心

B. Nick and Array
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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=1 and 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
inputCopy
4
2 2 2 2
outputCopy
-3 -3 -3 -3
inputCopy
1
0
outputCopy
0
inputCopy
3
-3 -3 2
outputCopy
-3 -3 2B. Nick and Array
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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=1 and 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
inputCopy
4
2 2 2 2
outputCopy
-3 -3 -3 -3
inputCopy
1
0
outputCopy
0
inputCopy
3
-3 -3 2
outputCopy
-3 -3 2

题意:
给你一个数组,每一个数都可以通过给定的公式(a[i] = -a[i]-1)变成另外一个值,让你求所有可能的数组中,数组的所有值乘积最大的那个数组
思路:
每个数都可以变成负数和非负数,并且负数的绝对值比非负数的绝对值大,因此我们可以把所有的负数转换成非负数,然后从小到大排序(一定要从小到大排序,可以试一下这组样例 3 2 2 1 0),然后把尽可能多的偶数个非负数转换成负数,如果最后剩下一个,直接输出就行,因为上边已经全部转换成非负数了。

#include<iostream>
#include<stdio.h>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxx = 1e5+10;
struct node
{
    int x;
    int d;
} a[maxx];

int cmp1(struct node a,struct node b)
{
    return a.x<b.x;
}

int cmp2(struct node a,struct node b)
{
    return a.d<b.d;
}


int main()
{
    int n;
    while(cin>>n)
    {
        int i;
        for(i=0; i<n; i++)
        {
            scanf("%d",&a[i].x);
            if(a[i].x<0)
                a[i].x = -a[i].x-1;   //转换成非负数
            a[i].d = i;       // 存下每个的编号,因为输出的时候顺序是不变的
        }
        sort(a,a+n,cmp1);    //从小到大排序
        if(n&1)
        {
            for(i=0; i<n-1; i++)
            {
                a[i].x = -a[i].x-1;
            }
            sort(a,a+n,cmp2);      //按编号从小到大排序
            for(i=0; i<n-1; i++)
            {
                printf("%d ",a[i].x);
            }
            printf("%d",a[i].x);
            printf("\n");
        }
        else
        {
            for(i=0; i<n; i++)
            {
                a[i].x = -a[i].x-1;
            }
            sort(a,a+n,cmp2);
            for(i=0; i<n; i++)
            {
                if(i==0)
                    printf("%d",a[i].x);
                else
                    printf(" %d",a[i].x);
            }
            printf("\n");
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44694282/article/details/93408599