2019.8.15乘兴打Codeforces Round #569 (Div. 2)小记B. Nick and Array

时间:2022-07-25
本文章向大家介绍2019.8.15乘兴打Codeforces Round #569 (Div. 2)小记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

思路:比赛的时候想的太麻烦,最后绝对想复杂了,就是一个贪心,先全部转成负数,因为负数绝对值更大嘛,然后如果序列长度是偶数,就可以,否则挑绝对值最大的数变成正数

#include<bits/stdc++.h>
#define rg register long long
#define inf 21474899993647
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
#define ll long long
#define maxn 100005
#define endl "n"
#define   N 6000
const double eps=1e-8;
using namespace std;
inline ll read()
{
    char ch=getchar();
    ll s=0,w=1;
    while(ch<48||ch>57)
    {
        if(ch=='-')
            w=-1;
        ch=getchar();
    }
    while(ch>=48&&ch<=57)
    {
        s=(s<<1)+(s<<3)+(ch^48);
        ch=getchar();
    }
    return s*w;
}
inline void write(ll x)
{
    if(x<0)
        putchar('-'),x=-x;
    if(x>9)
        write(x/10);
    putchar(x%10+48);
}
ll n=read(),a[maxn];
int main()
{
    ll zhengmax=-inf,fumin=inf,zcnt=0,fcnt=0;
    for(rg i=1;i<=n;i++)
    {
        a[i]=read();
        if(a[i]>=0)a[i]=-a[i]-1;
    }
    if(n%2)
    {
        ll temp=0;
          for(rg i=1;i<=n;i++)
    {
        if(fumin>a[i])
    {
        fumin=min(a[i],fumin);
    temp=i;
    }
    }
    a[temp]=-a[temp]-1;
 
    }
    for(rg i=1;i<=n;i++)
    {
        cout<<a[i]<<" ";
    }
 
    return 0;
}