cf 569 div2 B题解

题目原文
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.

题意
现在你有一个数组,你可以对数组中的每个元素进行如下操作(不限次数)

a[i]=-a[i]-1;

请问,操作后的使数组的乘积达到最大值的新数组是什么

题解
首先一看,立刻就想到了可以计算正负数的个数,然后特判之类的,写了半天wa了,然后又对0特判半天,终于发现了不对劲
我们可以这样想,要想使数组的乘积最大,那么数组的元素要变大吧?
那么如何可以让元素变大呢?
操作。

a[i]=-a[i]-1;

我们如果对于两个正数都这样操作了,那么不仅变大,而且负负得正也不影响最后结果,那么也就意味着,我们可以对所有的正数都进行操作,然后对其正负进行处理就可以了。
我们可以想,如果元素的个数是偶数,那么我们可以把所有大于等于0的数都操作一下,这样可以最大化值而且负负得正;
如果元素个数是奇数
我们以 -6 -7 3 4 5 为例
要想其最大,那么最大应该是
-6 6 -4 -5 -6 即所有正数均操作一下,再把绝对值最大的数操作一下,因为可以想到,如果只是加减一的话,那么越大的数减一所影响的程度就越少,而小的数则反之,所以我们将最大的数操作,减少最大的数的值,实际上保证了乘积的最大化。
注意:如果有一对相反数是绝对值最大的话,我们只需要操作那个正数就可以了
因为我们这个操作的本质是维持正负不改变,而用最大绝对值数操作可以最大化的减少损失,但相反数的话,只操作那个正的数反而可以增大绝对值。
代码如下

#include <bits/stdc++.h>
using namespace std;
int a[100005];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,i,j,k,t;
    cin>>n;
    for(i=0;i<n;i++)
        cin>>a[i];
    if(n%2==0){//偶数的话全部处理,因为处理可以变大,而且偶数不会影响正负
            for(i=0;i<n;i++)
                if(a[i]>=0) a[i]=-a[i]-1;
            for(i=0;i<n;i++)
                cout<<a[i]<<" ";
        }
    else{
            int maxx=-1;
            for(i=0;i<n;i++){
                if(abs(a[i])>maxx){//找出绝对值最大的数及其位置,为了最后的特殊处理
                    maxx=abs(a[i]);
                    j=i;
                }
            }
            for(i=0;i<n;i++){
                if(a[i]==maxx){//如果有一个正数满足最大的话,直接单独操作那个正数就可以了
                    j=i;
                    break;
                }
            }
            for(i=0;i<n;i++){
                if(a[i]>=0) a[i]=-a[i]-1;
            }
            a[j]=-a[j]-1;//单独处理来维持正负
            for(i=0;i<n;i++)
                cout<<a[i]<<" ";
        }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值