美文网首页
Codeforces Round #569 (Div. 2) B

Codeforces Round #569 (Div. 2) B

作者: xiaohejun | 来源:发表于2019-06-22 16:40 被阅读0次

标签(空格分隔): 题解(codeforces)

ps:关于本题,昨晚熬夜到1:05打cf想涨分结果脑袋抽了,
一直B题wa9.mmp.没有想到0可以变成-1.没有想到修改绝对值最大的那个负数
rating掉到1340.mmp

首先正的变成负的。在绝对值上会增加1.所以先把正的变成负的
是奇数的情况下.全部是负的.将谁修改成正的.修改成正的绝对值要减1.
看下面的情况:
-2, -3, -4
修改-2
1 * -3 * -4 = 12
修改-3
-2 * -3 * 3 = 18
表面上看是修改最小的那个,也就是绝对值最大的那个为正的
假设数字按照绝对值递增排列,按照大小从大到小排序.a_i < 0
a_1, a_2, a_3, a_4, a_5 ...a_i...a_n
证明修改an要优于修改a_i(i \neq n). a_n <= a_i. |a_n| >= |a_i|
记除去a_ia_n的数字之间相乘的结果是C. C < 0
修改a_i.
a_i = -a_i-1. ans1 = C*(-a_i-1)*a_n = -C*a_n*a_i -C*a_n
修改an.
a_n = -a_n-1. ans2 = C*(-an-1)*ai = -C*an*ai -C*ai
容易知道
-C*a_n < 0, -C*a_i < 0, -C*a_n*a_i > 0.
并且
-C*a_n <= -C*a_i
所以ans1 <= ans2.所以修改a_n最优

#pragma GCC optimize("O2") 
#include <bits/stdc++.h>
using namespace std;

#define dbg(x) cerr << #x"=" << x << endl;
typedef long long LL;
const int MAX_N = 1e5+100;
int a[MAX_N];

int main(){
//  freopen("in.txt","r",stdin);    
    ios::sync_with_stdio(0); cin.tie(0);
    int n;
    cin >> n;
    for(int i = 1; i <= n; ++i){
        cin >> a[i];
        if(a[i] >= 0) a[i] = -a[i]-1;
    }
    if(n&1){
        int mn = -1;
        int id = 0;
        for(int i = 1; i <= n; ++i){
            if(mn >= a[i]){
                mn = a[i];
                id = i;
            }
        } 
        a[id] = -a[id]-1;
    }
    for(int i = 1; i <= n; ++i){
        cout << a[i] << " ";
    }
    cout << endl;
    return 0;
}

相关文章

网友评论

      本文标题:Codeforces Round #569 (Div. 2) B

      本文链接:https://www.haomeiwen.com/subject/hgrvqctx.html