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

    D. Maxim and Array
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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.

    Examples
    input
    5 3 1
    5 4 3 5 2
    output
    5 4 3 5 -1 
    input
    5 3 1
    5 4 3 5 5
    output
    5 4 0 5 5 
    input
    5 3 1
    5 4 4 5 5
    output
    5 1 4 5 5 
    input
    3 2 7
    5 4 2
    output
    5 11 -5 

     题意:n个数,可以修改k次,每次可以+x或者-x,使得成绩最小;

    思路:每次寻找绝对值最小的那个数,判断负数的个数,进行+x或者-x;

       ps:优先队列也可做;

    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define pi (4*atan(1.0))
    const int N=2e5+10,M=4e6+10,inf=1e9+10,mod=1e9+7;
    const ll INF=1e18+10;
    struct is
    {
        ll num;
        int pos;
    }tree[N<<2];
    ll ans[N];
    void pushup(int pos)
    {
        tree[pos].num=min(tree[pos<<1].num,tree[pos<<1|1].num);
    }
    void buildtree(int l,int r,int pos)
    {
        if(l==r)
        {
            tree[pos].num=abs(ans[l]);
            tree[pos].pos=l;
            return;
        }
        int mid=(l+r)>>1;
        buildtree(l,mid,pos<<1);
        buildtree(mid+1,r,pos<<1|1);
        pushup(pos);
    }
    void update(int p,ll c,int l,int r,int pos)
    {
        if(p==r&&p==l)
        {
            tree[pos].num=abs(c);
            return;
        }
        int mid=(l+r)>>1;
        if(p<=mid)
        update(p,c,l,mid,pos<<1);
        else
        update(p,c,mid+1,r,pos<<1|1);
        pushup(pos);
    }
    int query(ll x,int l,int r,int pos)
    {
        if(l==r&&tree[pos].num==x)
            return tree[pos].pos;
        int mid=(l+r)>>1;
        if(tree[pos<<1].num==x)
        return query(x,l,mid,pos<<1);
        else
        return query(x,mid+1,r,pos<<1|1);
    }
    int main()
    {
        int n,m,k;
        int flag=0;
        scanf("%d%d%d",&n,&m,&k);
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&ans[i]);
            if(ans[i]<0)flag++;
        }
        buildtree(1,n,1);
        while(m--)
        {
            ll x=tree[1].num;
            int pos=query(x,1,n,1);
            if(flag&1)
            {
                if(ans[pos]>=0)
                ans[pos]+=k;
                else
                ans[pos]-=k;
                update(pos,ans[pos],1,n,1);
            }
            else
            {
                if(ans[pos]>=0)
                {
                    ans[pos]=ans[pos]-k;
                    if(ans[pos]<0)
                    flag++;
                }
                else
                {
                    ans[pos]=ans[pos]+k;
                    if(ans[pos]>=0)
                    flag--;
                }
                update(pos,ans[pos],1,n,1);
            }
        }
        for(int i=1;i<=n;i++)
        printf("%lld ",ans[i]);
        return 0;
    }
  • 相关阅读:
    [置顶] Spring的自动装配
    BZOJ2831(小强的金字塔系列问题--区域整点数求法)
    国际跆拳道联盟
    Linux如何查找某个时间点后生成的空文件
    ORACLE中关于外键缺少索引的探讨和总结
    ORA-12514, TNS:listener does not currently know of service requested in connect descriptor案例2
    SQL Server 2005 sp_send_dbmail出现Internal error at FormatRowset (Reason: Not enough storage is available to complete this operation)
    SQL Server数据库邮件发送异常案例
    MySQL二进制日志总结
    OSWatcher使用过程中小问题解决方法
  • 原文地址:https://www.cnblogs.com/jhz033/p/5925792.html
Copyright © 2011-2022 走看看