前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >2019.8.15乘兴打Codeforces Round #569 (Div. 2)小记B. Nick and Array

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

作者头像
glm233
发布2020-09-28 10:43:07
4880
发布2020-09-28 10:43:07
举报
文章被收录于专栏:glm的全栈学习之路

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

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

代码语言:javascript
复制
#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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/08/15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档