zoukankan      html  css  js  c++  java
  • Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心

    D. Maxim and Array

    题目连接:

    http://codeforces.com/contest/721/problem/D

    Description

    Recently Maxim has found an array of n integers, needed by no one. He immediately come up with idea of changing it: he invented positive integer x and decided to add or subtract it from arbitrary array elements. Formally, by applying single operation Maxim chooses integer i (1 ≤ i ≤ n) and replaces the i-th element of array ai either with ai + x or with ai - x. Please note that the operation may be applied more than once to the same position.

    Maxim is a curious minimalis, thus he wants to know what is the minimum value that the product of all array elements (i.e. ) can reach, if Maxim would apply no more than k operations to it. Please help him in that.

    Input

    The first line of the input contains three integers n, k and x (1 ≤ n, k ≤ 200 000, 1 ≤ x ≤ 109) — the number of elements in the array, the maximum number of operations and the number invented by Maxim, respectively.

    The second line contains n integers a1, a2, ..., an () — the elements of the array found by Maxim.

    Output

    Print n integers b1, b2, ..., bn in the only line — the array elements after applying no more than k operations to the array. In particular, should stay true for every 1 ≤ i ≤ n, but the product of all array elements should be minimum possible.

    If there are multiple answers, print any of them.

    Sample Input

    5 3 1
    5 4 3 5 2

    Sample Output

    5 4 3 5 -1

    Hint

    题意

    给你n个数,你可以操作k次,每次使得一个数增加x或者减小x

    你要使得最后所有数的乘积最小,问你最后这个序列长什么样子。

    题解:

    贪心,根据符号的不同,每次贪心的使得一个绝对值最小的数减去x或者加上x就好了

    这个贪心比较显然。

    假设当前乘积为ANS,那么你改变a[i]的大小的话,那么对答案的影响为ANS/A[i]/*X

    然后找到影响最大的就好了。

    代码

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 2e5+7;
    
    long long a[maxn],b[maxn];
    int n,k,x;
    set<pair<long long,long long> >S;
    int main()
    {
        scanf("%d%d%d",&n,&k,&x);
        int sig = 0;
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&a[i]);
            if(a[i]<0)sig^=1;
            S.insert(make_pair(abs(a[i]),i));
        }
        for(int i=1;i<=k;i++)
        {
            int pos = S.begin()->second;
            S.erase(S.begin());
            if(a[pos]<0)sig^=1;
            if(sig)a[pos]+=x;
            else a[pos]-=x;
            if(a[pos]<0)sig^=1;
            S.insert(make_pair(abs(a[pos]),pos));
        }
        for(int i=1;i<=n;i++)
            cout<<a[i]<<" ";
        cout<<endl;
    
    }
  • 相关阅读:
    H3C防火墙/路由器通过Track实现双线接入
    为了安装runlike 升级python2至python3
    URL:windows_exporter-0.13.0-amd64
    ES_Start
    luogu4323 独特的树叶
    luogu5043
    java操作Jacoco合并dump文件
    【Azure 云服务】Azure Cloud Service 为 Web Role(IIS Host)增加自定义字段 (把HTTP Request Header中的User-Agent字段增加到IIS输出日志中)
    【Azure 应用服务】App Service下部署的应用报错 Out of Memory
    【Azure Developer】使用Key Vault的过程中遇见的AAD 认证错误
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5925893.html
Copyright © 2011-2022 走看看