Codeforces Round #374 (Div. 2) D. Maxim and Array 线段树+贪心
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
题意:n个数,可以修改k次,每次可以+x或者-x,使得成绩最小;
思路:每次寻找绝对值最小的那个数,判断负数的个数,进行+x或者-x;
ps:优先队列也可做;
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
const int N=2e5+,M=4e6+,inf=1e9+,mod=1e9+;
const ll INF=1e18+;
struct is
{
ll num;
int pos;
}tree[N<<];
ll ans[N];
void pushup(int pos)
{
tree[pos].num=min(tree[pos<<].num,tree[pos<<|].num);
}
void buildtree(int l,int r,int pos)
{
if(l==r)
{
tree[pos].num=abs(ans[l]);
tree[pos].pos=l;
return;
}
int mid=(l+r)>>;
buildtree(l,mid,pos<<);
buildtree(mid+,r,pos<<|);
pushup(pos);
}
void update(int p,ll c,int l,int r,int pos)
{
if(p==r&&p==l)
{
tree[pos].num=abs(c);
return;
}
int mid=(l+r)>>;
if(p<=mid)
update(p,c,l,mid,pos<<);
else
update(p,c,mid+,r,pos<<|);
pushup(pos);
}
int query(ll x,int l,int r,int pos)
{
if(l==r&&tree[pos].num==x)
return tree[pos].pos;
int mid=(l+r)>>;
if(tree[pos<<].num==x)
return query(x,l,mid,pos<<);
else
return query(x,mid+,r,pos<<|);
}
int main()
{
int n,m,k;
int flag=;
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=n;i++)
{
scanf("%lld",&ans[i]);
if(ans[i]<)flag++;
}
buildtree(,n,);
while(m--)
{
ll x=tree[].num;
int pos=query(x,,n,);
if(flag&)
{
if(ans[pos]>=)
ans[pos]+=k;
else
ans[pos]-=k;
update(pos,ans[pos],,n,);
}
else
{
if(ans[pos]>=)
{
ans[pos]=ans[pos]-k;
if(ans[pos]<)
flag++;
}
else
{
ans[pos]=ans[pos]+k;
if(ans[pos]>=)
flag--;
}
update(pos,ans[pos],,n,);
}
}
for(int i=;i<=n;i++)
printf("%lld ",ans[i]);
return ;
}
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 —— 贪心
题目链接: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
传送门 分析:其实没什么好分析的.统计一下负数个数.如果负数个数是偶数的话,就要尽量增加负数或者减少负数.是奇数的话就努力增大每个数的绝对值.用一个优先队列搞一下就行了. 我感觉这道题的细节极为多,非 ...
- Codeforces Round #373 (Div. 2) E. Sasha and Array 线段树维护矩阵
E. Sasha and Array 题目连接: http://codeforces.com/contest/719/problem/E Description Sasha has an array ...
- Codeforces Round #292 (Div. 1) C. Drazil and Park 线段树
C. Drazil and Park 题目连接: http://codeforces.com/contest/516/problem/C Description Drazil is a monkey. ...
- Codeforces Round #254 (Div. 1) C. DZY Loves Colors 线段树
题目链接: http://codeforces.com/problemset/problem/444/C J. DZY Loves Colors time limit per test:2 secon ...
- Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树扫描线
D. Vika and Segments 题目连接: http://www.codeforces.com/contest/610/problem/D Description Vika has an i ...
- Codeforces Round #337 (Div. 2) D. Vika and Segments (线段树+扫描线+离散化)
题目链接:http://codeforces.com/contest/610/problem/D 就是给你宽度为1的n个线段,然你求总共有多少单位的长度. 相当于用线段树求面积并,只不过宽为1,注意y ...
- Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)
题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题 ...
随机推荐
- 爬虫入门【8】Python连接MongoDB的用法简介
MongoDB的连接和数据存取 MongoDB是一种跨平台,面向文档的NoSQL数据库,提供高性能,高可用性并且易于扩展. 包含数据库,集合,文档等几个重要概念. 我们在这里不介绍MongoDB的特点 ...
- mongodb基础操作
查询选择器>db.customers.find({age:{$lt:102}})查询age小于102的数据$lte表示小于或等于$gt表示大于$gte表示大于或等于>db.customer ...
- 时间格式化输出strtime
The format argument consists of one or more codes; as in printf, the formatting codes are preceded b ...
- javascript之冒泡排序
Array.prototype.bubble = function () { var temp; //第1轮比较求第一,第二轮求第二,以此类推(i<=数组成员的个数); for (var i = ...
- [BZOJ3551]Peaks
[BZOJ3551]Peaks BZOJ luogu 建Kruskal重构树,点权为边权 按dfn序建出主席树 倍增找到能跳到的最浅的祖先 主席树查询一下 #include<bits/stdc+ ...
- 找出n的阶乘末尾有几个零
原理:因为10由2*5组成,而构成2的因数比5多 所以最终转换成求5的个数 int getNumber(int n) { int count = 0; while(n) { n = n/5; coun ...
- NumPy基础知识:数组和矢量计算
NumPy 的ndarray:一种多维数组对象 该对象是一个快速且灵活的大数据容器,可以利用这种数组对整个数据进行科学计算,语法跟标量元素之间的计算一样. 创建ndarray的方法: array函数: ...
- LSTM梳理,理解,和keras实现 (一)
注:本文主要是在http://colah.github.io/posts/2015-08-Understanding-LSTMs/ 这篇文章的基础上理解写成,姑且也可以称作 The understan ...
- Way to Git
最近在学习Git,我先后在CentOS6.4, Ubuntu12.04, Windows7上安装Git,遇到的问题比较多的是在CentOS上的安装,Ubuntu和Windows7上的安装相对比较简单, ...
- python之路 内置函数,装饰器
一.内置函数 #绝对值 abs() #所有值都为真才为真 all() #只要有一个值为真就为真 any() #10进制转成二进制 bin() #10进制转成八进制 oct() #10进制转成十六进制 ...