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. 这题 ...
随机推荐
- SharePoint服务器端对象模型 之 访问文件和文件夹(Part 1)
本节中所阐述的内容,主要适用于SharePoint文档库中的文件和文件夹,以及列表中的文件夹.系统中的其他文件(如_layouts中的文件.配置文件.程序文件等)不在本章节的讨论范围之内. (一) ...
- Anker—工作学习笔记
1.前言 最近在项目中用nginx做反向代理,需要动态生成nginx的配置.大概流程是用户在页面上新增域名.http或https协议以及端口信息,后台会根据域名自动生成一个nginx的server配置 ...
- Spring 事务机制详解(事务的隔离性和传播性)
原文出处: 陶邦仁 Spring事务机制主要包括声明式事务和编程式事务,此处侧重讲解声明式事务,编程式事务在实际开发中得不到广泛使用,仅供学习参考. Spring声明式事务让我们从复杂的事务处理中得到 ...
- 001-maven下载jar后缀为lastUpdated问题
问题简述 Maven在下载仓库中找不到相应资源时,网络中断等,会生成一个.lastUpdated为后缀的文件.如果这个文件存在,那么即使换一个有资源的仓库后,Maven依然不会去下载新资源. 解决方案 ...
- MFC实现文字随鼠标移动
1 实验介绍 此实验是在刚开始接触MFC时做的.它要求实现的功能如下: 文字跟随鼠标动态移动(跟随移动方式自定) 修改图标为自己喜欢的图标 修改窗口标题 修改文档名称 可以用菜单项选定指定的颜色显示文 ...
- Django Rest Framework(一)
一.什么是RESTful REST与技术无关,代表一种软件架构风格,REST是Representational State Transfer的简称,中文翻译为“表征状态转移”. REST从资源的角度审 ...
- Django——admin组件
Django提供了基于web的管理工具. Django自动管理工具是django.contrib的一部分.你可以在项目的settings.py中的INSTALLED_APPS看到它: # Applic ...
- Android:日常学习笔记(7)———探究UI开发(4)
Android:日常学习笔记(7)———探究UI开发(4) UI概述 View 和 ViewGrou Android 应用中的所有用户界面元素都是使用 View 和 ViewGroup 对象构建而成 ...
- ibatis工作原理
摘要: iBATIS 通过 SQL Map 将 Java 对象映射成 SQL 语句和将结果集再转化成 Java 对象,与其他 ORM 框架相比,既解决了 Java 对象与输入参数和结果集的映射,又能够 ...
- text_field text_tag 用法
= f.text_field :tax_category_id, :value => @invoice.tax_category.name, :class => "form-co ...