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;
}
Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心的更多相关文章
- Codeforces Round #374 (Div. 2) D. Maxim and Array —— 贪心
题目链接:http://codeforces.com/problemset/problem/721/D D. Maxim and Array time limit per test 2 seconds ...
- 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 ...
- Codeforces Round #374 (Div. 2) D. Maxim and Array
传送门 分析:其实没什么好分析的.统计一下负数个数.如果负数个数是偶数的话,就要尽量增加负数或者减少负数.是奇数的话就努力增大每个数的绝对值.用一个优先队列搞一下就行了. 我感觉这道题的细节极为多,非 ...
- Codeforces Round #297 (Div. 2)C. Ilya and Sticks 贪心
Codeforces Round #297 (Div. 2)C. Ilya and Sticks Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xxx ...
- Codeforces Round #374 (div.2)遗憾题合集
C.Journey 读错题目了...不是无向图,结果建错图了(喵第4样例是变成无向就会有环的那种图) 并且这题因为要求路径点尽可能多 其实可以规约为限定路径长的拓扑排序,不一定要用最短路做 #prag ...
- Codeforces Round #374 (Div. 2) A B C D 水 模拟 dp+dfs 优先队列
A. One-dimensional Japanese Crossword time limit per test 1 second memory limit per test 256 megabyt ...
- 拓扑序+dp Codeforces Round #374 (Div. 2) C
http://codeforces.com/contest/721/problem/C 题目大意:给你有向路,每条路都有一个权值t,你从1走到n,最多花费不能超过T,问在T时间内最多能访问多少城市? ...
- Codeforces Round #374 (Div. 2) C. Journey DP
C. Journey 题目连接: http://codeforces.com/contest/721/problem/C Description Recently Irina arrived to o ...
- Codeforces Round #374 (Div. 2) B. Passwords 贪心
B. Passwords 题目连接: http://codeforces.com/contest/721/problem/B Description Vanya is managed to enter ...
随机推荐
- hdu 5755 Gambler Bo (高斯消元法解同余方程组)
http://acm.hdu.edu.cn/showproblem.php?pid=5755 题意: n*m矩阵,每个格有数字0/1/2 每选择一个格子,这个格子+2,4方向相邻格子+1 如何选择格子 ...
- 【整理】HTML5游戏开发学习笔记(2)- 弹跳球
1.预备知识(1)在画布上绘制外部图片资源(2)梯度(gradient):HTML5中的对象类型,包括线性梯度和径向梯度.如createLinearGradient,绘制梯度需要颜色组http://w ...
- Vue模板语法V-bind
一.插值 1.文本 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...
- 2017/05/21 java 基础 随笔
工具类:所有的方法都是静态的,如果一个类中所有的方法都是静态的,需要再多做一步,私有构造方法,不让其他类创建本类对象. 生成文档: java.lang 包不用导入 常见代码块的应用 * a:局部 ...
- pytorch之LSTM
from:http://pytorch-cn.readthedocs.io/zh/latest/package_references/torch-nn/#recurrent-layers class ...
- 关于overflow: hidden;的一个诡异问题
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- C#获取特定进程CPU和内存使用率
首先是获取特定进程对象,可以使用Process.GetProcesses()方法来获取系统中运行的所有进程,或者使用Process.GetCurrentProcess()方法来获取当前程序所对应的进程 ...
- docker:一个支持django的dockerfile
其中,包括了主要的生产环境模块, 从alpine作起,镜像不大.保存用. FROM alpine:3.7 COPY . /target-dir WORKDIR /target-dir RUN sed ...
- pig cookbook学习
pig cookbook学习 Overview 近期需要用pig做一些统计,由于没有系统学习,问题出现一些问题,且不容易调试,执行效率也不高.所以打算看一些官方文档,在此做些笔记. pig性能提升 指 ...
- 003 RequestMapping——Ant路径
一: 1.介绍 Ant风格资源地址支持3中配配符 ?:匹配文件名中的一个字符 * :匹配文件名中的任意字符 **:匹配多层路径 2.RequestMapping支持的Ant风格的路径 二:程序说明 ...