ZCMU暑期训练四-H - Nick and Array

探讨如何通过改变数组元素的正负性来最大化整个数组的乘积,提供了一个算法解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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=1 and 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
4
2 2 2 2
Output
-3 -3 -3 -3
Input
1
0
Output
0
Input
3
-3 -3 2
Output
-3 -3 2
题面:找序列题目里面的有几个然后删掉以后可以满足条件
注意判断条件

#include<iostream>
#include<map>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<vector>
using namespace std;
#pragma warning (disable:4786)
const int maxn=1e5+10;
int main()
{
    int n;
    scanf("%d",&n);
    vector<int>a(n);
    for(int i=0;i<n;i++)
    {
        int v;
        scanf("%d",&v);
        if(v==4)a[i]=1;
        if(v==8)a[i]=2;
        if(v==15)a[i]=3;
        if(v==16)a[i]=4;
        if(v==23)a[i]=5;
        if(v==42)a[i]=6;
    }
    vector<int>cur(7);
    cur[0]=n;
    for(int i=0;i<n;i++)
    {
        if(cur[a[i]-1]>0)
        {
            ++cur[a[i]];
            --cur[a[i]-1];
        }
    }
    int ans=n-6*cur[6];
    printf("%d",ans);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值