您的位置:首页 > 其它

CF 721D贪心

2016-10-30 21:58 302 查看
——————题目链接——————

题目大意

n个数 k次操作 每次可以将n个数中任意一个数加或减x 使得k次操作后这n个数乘积最小 求每个数

思路

贪心,每次取出绝对值最小的数,如果当前负数个数为奇数个,那就将这个数背离0前进,如果负数个数为偶数个,那就将这个数朝0前进

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn=2e5+10;
struct node
{
ll num;
int id;
node(ll _num,int _id)
{
this->num=_num;
this->id=_id;
}
bool operator < (const node b) const
{
return abs(num)>abs(b.num);
}
};
priority_queue<node>q;
int n,k,x;
ll a[maxn];
int main()
{
scanf("%d%d%d",&n,&k,&x);
int flag=1;
for(int i = 0; i < n; ++i) {
scanf("%I64d",&a[i]);
if(a[i]<0) {
flag=-flag;
}
q.push(node(a[i],i));
}
while(k--) {
node temp=q.top();
q.pop();
if(a[temp.id]<0) {
if(flag==-1) a[temp.id]-=x;
else a[temp.id]+=x;
if(a[temp.id]>=0) {
flag=-flag;
}
}
else {
if(flag==-1) a[temp.id]+=x;
else a[temp.id]-=x;
if(a[temp.id]<0) {
flag=-flag;
}
}
q.push(node(a[temp.id],temp.id));
}
for(int i = 0; i < n; ++i) {
if(i) printf(" ");
printf("%I64d",a[i]);
}
printf("\n");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codeforces