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。


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
const int MAXN=200000+100;
const int INF=0x3f3f3f3f;
struct fun
{
	int id;
	ll num;
}a[MAXN];
ll ab(ll x)
{
	return x>=0?x:-x;
}
bool cmp(fun x,fun y)
{
	return ab(x.num)<ab(y.num);
}
bool cmp1(fun x,fun y)
{
	return x.id<y.id;
}
struct cmp2
{
	 bool operator()(fun x,fun y)
     {
	    return ab(x.num)>ab(y.num);
     }
};
int main()
{
	int n,k;
	ll x;
	scanf("%d%d%I64d",&n,&k,&x);
	int cnt=0;
	for(int i=1;i<=n;i++)
	{
		scanf("%I64d",&a[i].num);
		a[i].id=i;
		if(a[i].num<0) cnt++;
	}
	if(cnt%2==0)
	{
		sort(a+1,a+n+1,cmp);
		if(a[1].num<0)
		{
			while(k&&a[1].num<0)
			{
				a[1].num+=x;
				k--;
			}
		}
		else 
		{
			while(k&&a[1].num>=0)
			{
				a[1].num-=x;
				k--;
			}
		}
		sort(a+1,a+n+1,cmp1);
	} 
	priority_queue<fun,vector<fun>,cmp2> q;
	for(int i=1;i<=n;i++) q.push(a[i]);
	while(k)
	{
		fun cur=q.top();
		q.pop();
		if(cur.num>=0) cur.num+=x,a[cur.id].num+=x;
		else cur.num-=x,a[cur.id].num-=x;
		q.push(cur);
		k--;
	}
	sort(a+1,a+n+1,cmp1);
	for(int i=1;i<=n;i++)
	printf("%I64d%c",a[i].num,i==n?'\n':' ');
	return 0;
}



cf上看到的一个不错的代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

struct Node
{
    ll id,val;
    Node(ll a,ll b):id(a),val(b){}
    bool operator < (const Node& rhs) const
    {
        return abs(val)>abs(rhs.val);
    }
};
ll n,k,x;
vector<Node>vec;
bool cmp(Node& a1,Node& a2)
{
    return a1.id<a2.id;
}

int main()
{
    priority_queue<Node>q;
    scanf("%lld %lld %lld",&n,&k,&x);
    ll temp;
    ll fu=0;
    for(ll i=1;i<=n;i++)
    {
        scanf("%lld",&temp);
        if(temp<0) fu++;
        q.push(Node(i,temp));
    }
    if((fu&1)==0&&k>0)
    {
        Node now=q.top();
        q.pop();
        if(now.val>=0)
        {
            while(k>0&&abs(now.val)<=abs(q.top().val))
            {
                k--;
                now.val-=x;
            }
        }
        else
        {
            while(k>0&&abs(now.val)<=abs(q.top().val))
            {
                k--;
                now.val+=x;
            }
        }
        q.push(now);
    }
    Node now=q.top();
    while(k>0)
    {
        now=q.top();
        q.pop();
        while(k>0&&abs(now.val)<=abs(q.top().val))
        {
            if(now.val>=0) now.val+=x;
            else now.val-=x;
            k--;
        }
        q.push(now);
    }
    while(!q.empty())
    {
        vec.push_back(q.top());
        q.pop();
    }
    sort(vec.begin(),vec.end(),cmp);
    for(unsigned int i=0;i<vec.size();i++)
        printf("%lld ",vec[i].val);
    puts("");
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值