zoukankan      html  css  js  c++  java
  • Codeforces Round #569 (Div. 2) B. Nick and Array

    链接:

    https://codeforces.com/contest/1180/problem/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.

    思路:

    先将所有正值变成负数,偶数不处理,奇数吧最小的变成正数。

    代码:

    #include<bits/stdc++.h>
    using namespace std;
     
    const int MAXN = 1e5+10;
    int a[MAXN];
     
    int main()
    {
        int n, m, x;
        cin >> n;
        for (int i = 1;i <= n;i++)
        {
            cin >> a[i];
            if (a[i] >= 0)
                a[i] = -a[i]-1;
            if (a[i] < m)
            {
                m = a[i];
                x = i;
            }
        }
        if (n%2 == 1)
            a[x] = -a[x]-1;
        for (int i = 1;i <= n;i++)
            cout << a[i] << ' ' ;
        cout << endl;
     
        return 0;
    }
    
  • 相关阅读:
    cb快捷键
    N的阶乘的长度 V2(斯特林近似)
    最大子序列和(Max Sum ,Super Jumping! Jumping! Jumping! )
    关于莫比乌斯和莫比乌斯反演
    最少拦截系统
    set用法详解
    几种数学公式(环排列 母函数 唯一分解定理 卡特兰数 默慈金数 贝尔数 那罗延数)
    最小堆算法
    并查集算法
    dijkstra算法演示
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11155464.html
Copyright © 2011-2022 走看看