【24.17%】【codeforces 721D】Maxim and Array
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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
【题目链接】:http://codeforces.com/contest/721/problem/D
【题解】
如果负数的个数为偶数个.
则需要把某一个数的符号变一下.让负数的个数变成奇数个.
(负数肯定比正数大!);
显然。我们让那个绝对值最小的数变号是最好的.
之后如果还有剩余的操作次数
就尽量让所有的数都”平均一点”;
这样最后连乘的结果最大.(是负数所以结果反而更小);
方法就是让绝对值最小的数的绝对值变大.
(每次都操作绝对值最小的数,让它的绝对值变大);
(绝对值最小的数可以用一个set维护出来);
那个重载<不包括==,所以如果想等就要随便想一个东西写上去.
(如果想到就直接返回true这样写最好)
不然会有东西莫名其妙地消失。
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int MAXN = 2e5+10;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
LL a[MAXN];
struct node
{
int x,sign;
friend bool operator < (node aa,node bb)
{
if (abs(a[aa.x]) != abs(a[bb.x]))
return abs(a[aa.x]) < abs(a[bb.x]);
else
return true;
}
};
int n,k,fu=0;
LL x;
set <node> dic;
int main()
{
//freopen("F:\\rush.txt","r",stdin);
rei(n);rei(k);rel(x);
rep1(i,1,n)
{
rel(a[i]);
node temp;
if (a[i] < 0)
temp.sign = -1,fu++;
else
temp.sign = 1;
temp.x = i;
dic.insert(temp);
}
if (!(fu&1))
{
while (k)
{
k--;
int po = dic.begin()->x;
int s = dic.begin()->sign;
dic.erase(dic.begin());
node temp1;
LL temp = abs(a[po]);
if (temp < x)
{
s*=-1;
temp-=x;
a[po] = abs(temp)*s;
temp1.sign = s;temp1.x = po;
dic.insert(temp1);
break;
}
else
{
temp-=x;
a[po] = temp*s;
temp1.sign = s;temp1.x = po;
dic.insert(temp1);
}
}
}
while (k)
{
k--;
int po = dic.begin()->x;
int s = dic.begin()->sign;
dic.erase(dic.begin());
node temp1;
LL temp = abs(a[po]);
temp += x;
temp*=s;
a[po] = temp;
temp1.sign = s;
temp1.x = po;
dic.insert(temp1);
}
rep1(i,1,n)
{
printf("%I64d",a[i]);
if (i==n)
puts("");
else
putchar(' ');
}
return 0;
}
【24.17%】【codeforces 721D】Maxim and Array的更多相关文章
- CodeForces - 721D Maxim and Array (贪心)
Recently Maxim has found an array of n integers, needed by no one. He immediately come up with idea ...
- JAVA 基础编程练习题17 【程序 17 猴子吃桃问题】
17 [程序 17 猴子吃桃问题] 题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个 第二天早上又 将剩下的桃子吃掉一半,又多吃了一个.以后每天早上都吃了前一天剩下的一 ...
- 【 BowWow and the Timetable CodeForces - 1204A 】【思维】
题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...
- 【24.67%】【codeforces 551C】 GukiZ hates Boxes
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【24.34%】【codeforces 560D】Equivalent Strings
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【40.17%】【codeforces 569B】Inventory
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【17.07%】【codeforces 583D】Once Again...
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces】【比赛题解】#855 Codefest 17
神秘比赛,以<哈利波特>为主题……有点难. C题我熬夜切终于是写出来了,可惜比赛结束了,气啊. 比赛链接:点我. [A]汤姆·里德尔的日记 题意: 哈利波特正在摧毁神秘人的分灵体(魂器). ...
- 【77.78%】【codeforces 625C】K-special Tables
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
随机推荐
- 免费超大量邮件发送服务Amazon SES和Mailgun提供SMTP和API支持
一般来说网站注册.论坛消息.新闻推送.广告宣传等都会有发送邮件服务,大量的邮件发送服务如果用PHP来发送,一是会消耗主机资源,二是容易被各大邮箱判定为垃圾邮件而被拒收.用第三方的邮局服务发送邮件,可以 ...
- Android-Volley网络通信框架(二次封装数据请求和图片请求(包含处理请求队列和图片缓存))
1.回想 上篇 使用 Volley 的 JsonObjectRequest 和 ImageLoader 写了 电影列表的样例 2.重点 (1)封装Volley 内部 请求 类(请求队列,数据请求,图片 ...
- HDU 1043 八数码(A*搜索)
在学习八数码A*搜索问题的时候须要知道下面几个点: Hash:利用康托展开进行hash 康托展开主要就是依据一个序列求这个序列是第几大的序列. A*搜索:这里的启示函数就用两点之间的曼哈顿距离进行计算 ...
- 经验之谈—让你看明确block
事实上我感觉不常常使用block的朋友.对block应该是比較陌生的,那么如今我们来扒开block的真面目,看看block的本质 普通的局部变量.block内部仅仅会引用它初始的值(block定义那一 ...
- PHP JSON的BUG
将下面的数组进行 JSON 编码时出错,编码中丢掉了最后一维数组中的下标. Array ( [1] => Array ( [0] => Array ( [0] => Array ( ...
- Eclipse中自动添加注释
方法一:Eclipse中设置在创建新类时自动生成注释 windows-->preference Java-->Code Style-->Code Templates code- ...
- 读阮一峰《ECMAScript 6 入门》小结
读阮一峰<ECMAScript 6 入门>小结,http://es6.ruanyifeng.com/ 1. ES6简介 Babel 是一个广泛使用的 ES6 转码器,可以将 ES6 代码转 ...
- BZOJ3160: 万径人踪灭(FFT,回文自动机)
BZOJ传送门: 解题思路: FFT在处理卷积时可以将自己与自己卷,在某一种字母上标1其他标0,做字符集次就好了. (回文就是直接对称可以联系偶函数定义理解,根据这个性质就可以将字符串反向实现字符串匹 ...
- Event Serializers官网剖析(博主推荐)
不多说,直接上干货! Flume Sources官网剖析(博主推荐) Flume Channels官网剖析(博主推荐) Flume Channel Selectors官网剖析(博主推荐) Flume ...
- java解压多目录Zip文件(解决中文乱码问题)--转载
原文地址:http://zhangyongbo.iteye.com/blog/1749439 import java.io.BufferedOutputStream; import java.io.F ...