Codeforces Round #374 (Div. 2) D. Maxim and Array —— 贪心
题目链接:http://codeforces.com/problemset/problem/721/D
2 seconds
256 megabytes
standard input
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.
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.
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.
5 3 1
5 4 3 5 2
5 4 3 5 -1
5 3 1
5 4 3 5 5
5 4 0 5 5
5 3 1
5 4 4 5 5
5 1 4 5 5
3 2 7
5 4 2
5 11 -5
题解:
1.首先在输入时,统计负数的个数,目的是知道初始状态的乘积是正数(包括0)还是负数。
2.如果乘积为正数,那么就需要减小某个数的绝对值,使其改变符号, 而且步数越少越好,由此推出:减少绝对值最小的那个数的绝对值,可以最快改变符号,使得乘积变为负数。
3.如果乘积已经为负,那么就需要增加某个数的绝对值, 使得乘积的绝对值尽可能大, 根据基本不等式: a+b>=2*根号(a*b),若要a*b的值最大,则a==b。 可以得出结论:当a与b的差值越小时,a*b越大。或者可以自己手动推算一遍, 也可以得出这个结论。由此推出:增加绝对值最小的那个数的绝对值,使得乘积的绝对值尽可能大。
4.总的来说:就是需要对绝对值最小的数进行操作。当乘积为正或为0时, 减小其绝对值,直到符号改变; 当乘积为负时, 增加其绝对值, 使其乘积的绝对值尽可能大。 用优先队列维护。
学习之处:
a+b=常数, 当a与b的差值越小时,a*b越大(a*b>=0)。
这里的a*b,可以假设a为绝对值最小的那个数, b为其他数的绝对值的乘积。所以这个结论也适用于多个数的乘积(将多个转为2个)。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <sstream>
#include <algorithm>
using namespace std;
#define ms(a, b) memset((a), (b), sizeof(a))
#define eps 0.0000001
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+7;
const int maxn = 2e5+10; struct node
{
LL v, s, pos;
bool operator<(const node&x)const{
return v>x.v;
}
};
priority_queue<node>q;
LL a[maxn], n, k, x, flag; void init()
{
scanf("%lld%lld%lld",&n, &k, &x);
while(!q.empty()) q.pop();
flag = 0;
for(int i = 1; i<=n; i++)
{
node e;
scanf("%lld",&a[i]);
e.v = abs(a[i]);
e.s = (a[i]<0);
e.pos = i;
q.push(e); if(a[i]<0) flag = !flag;
}
} void solve()
{
while(k--)
{
node e = q.top();
q.pop();
if(!flag)
{
e.v -= x;
if(e.v<0)
{
e.v = -e.v;
e.s = !e.s;
flag = !flag;
}
}
else
{
e.v += x;
}
a[e.pos] = e.v;
if(e.s) a[e.pos] = -a[e.pos];
q.push(e);
}
for(int i = 1; i<=n; i++)
printf("%lld ",a[i]);
putchar('\n');
} int main()
{
// int T;
// scanf("%d",&T);
// while(T--)
{
init();
solve();
}
return 0;
}
Codeforces Round #374 (Div. 2) D. Maxim and Array —— 贪心的更多相关文章
- Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心
D. Maxim and Array 题目连接: http://codeforces.com/contest/721/problem/D Description Recently Maxim has ...
- 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 ...
随机推荐
- 仓鼠找sugar(lca)
洛谷——P3398 仓鼠找sugar 题目描述 小仓鼠的和他的基(mei)友(zi)sugar住在地下洞穴中,每个节点的编号为1~n.地下洞穴是一个树形结构.这一天小仓鼠打算从从他的卧室(a)到餐厅( ...
- SRM1154--Topcoder初体验
SRM 711 DIV2 <br > 在frank_c1的帮助下,辣鸡Xiejiadong也开始做Topcoder辣...... <br > 这算是一次Topcoder的初体验 ...
- Java-ArrayList使用技巧---从第一个List中去除所有第二个List中与之重复的元素
需求:从 mAllList 中去除所有 mSubList 中与之重复的元素 测试数据:mAllList 中包含100000个无序无重复字符串,mSubList 中包含50000个无序无重复字符串 方法 ...
- Generate C and C++ Header File
1. 2. 其中bootclasspath 后面的参数就是自己android.jar具体位置 location: ${system_path:javah} working Directoy: ${pr ...
- Win7下Nginx的安装与配置
1. 下载nginx1.9.9版本:(版本随时间而变,下载最新即可) http://nginx.org/download/nginx-1.9.9.zip 2. 解压软件到对应位置,并重命名文件夹为n ...
- Flash如何为文字描边
可以使用墨水瓶工具,但是要先把文字打散(可以打散之后再组合起来)粗细和颜色都可以调,粗细就是笔触,颜色就是前景色(边框颜色)
- CrtmpServr 接收Http流程
最近在研究CrtmpServer http部分,记录一些基本的流程,以备查阅. 首先,打开配置脚本CrtmpServer.lua ,确认脚本中有以下内容,如果没有需要加上. { name=" ...
- Python+Selenium框架设计--- Page Object Model
POM(Page Object Model):页面对象模型,POM是一种最近几年非常流行的自动化测试模型,或者思想,POM不是一个框架,就是一个解决问题的思想.采用POM的目的,是为了解决前端中UI变 ...
- WPF 基础到企业应用系列1——开篇故意
參考资料 提到參考资料,大家第一感觉就是MSDN,当然我也不例外.这个站点基本上是学习微软技术的首选站点,除了这个站点以外,我还參考了非常多其它的社区和站点,基本上都在.NET 技术社区之我见(英文篇 ...
- [笔记] 精通正则表达式/Mastering Regular Expressions
/ 匹配<emphasis>这个tag标注的IP地址的RE:‘<emphasis>([0-9]+(\.[0-9]+){3})</emphasis>' / 锚定--a ...