链接:

https://vjudge.net/problem/CodeForces-721D

题意:

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.

思路:

贪心对每一个绝对值最小的值处理,小于0就减,大于等于0就加.等于0注意要当大于0考虑.

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL; const int MAXN = 2e5+10; struct Node
{
int pos;
LL val;
bool operator < (const Node& that) const
{
return abs(this->val) > abs(that.val);
}
}node[MAXN];
int n;
LL k, x; void Solve()
{
priority_queue<Node> que;
for (int i = 1;i <= n;i++)
que.push(node[i]);
while (k)
{
Node now = que.top();
que.pop();
if (now.val >= 0)
now.val += x;
else
now.val -= x;
que.push(now);
k--;
}
while (!que.empty())
{
node[que.top().pos] = que.top();
que.pop();
}
for (int i = 1;i <= n;i++)
printf("%lld ", node[i].val);
printf("\n");
} int main()
{
scanf("%d %d %lld", &n, &k, &x);
int cnt = 0;
for (int i = 1;i <= n;i++)
{
scanf("%lld", &node[i].val);
node[i].pos = i;
if (node[i].val < 0)
cnt++;
}
if (cnt == 0)
{
int mpos = 1;
for (int i = 1;i <= n;i++)
{
if (node[i].val < node[mpos].val)
mpos = i;
}
LL ti = (node[mpos].val+1LL+x-1)/x;
if (ti > k)
node[mpos].val -= k*x;
else
node[mpos].val -= ti*x;
k -= min(ti, k);
}
else if (cnt > 0 && cnt%2 == 0)
{
int mpos = 1;
for (int i = 1;i <= n;i++)
{
if (abs(node[i].val) < abs(node[mpos].val))
mpos = i;
}
if (node[mpos].val >= 0)
{
LL ti = (node[mpos].val+1LL+x-1)/x;
if (ti > k)
node[mpos].val -= k*x;
else
node[mpos].val -= ti*x;
k -= min(ti, k);
}
else
{
LL ti = (abs(node[mpos].val)+1LL+x-1)/x;
if (ti > k)
node[mpos].val += k*x;
else
node[mpos].val += ti*x;
k -= min(ti, k);
}
}
Solve(); return 0;
}

CodeForces-721D-Maxim and Array(优先队列,贪心,分类讨论)的更多相关文章

  1. CodeForces 721D Maxim and Array

    贪心,优先队列. 先看一下输入的数组乘积是正的还是负的. ①如果是负的,也就是接下来的操作肯定是让正的加大,负的减小.每次寻找一个绝对值最小的数操作就可以了. ②如果是正的,也是考虑绝对值,先操作绝对 ...

  2. Codeforces F. Maxim and Array(构造贪心)

    题目描述: Maxim and Array time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  3. Codeforces G. Nick and Array(贪心)

    题目描述: Nick had received an awesome array of integers a=[a1,a2,…,an] as a gift for his 5 birthday fro ...

  4. Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心

    D. Maxim and Array 题目连接: http://codeforces.com/contest/721/problem/D Description Recently Maxim has ...

  5. 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 ...

  6. 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 ...

  7. Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array 分类讨论连续递推dp

    题意:给出一个 数列 和一个x 可以对数列一个连续的部分 每个数乘以x  问该序列可以达到的最大连续序列和是多少 思路: 不是所有区间题目都是线段树!!!!!! 这题其实是一个很简单的dp 使用的是分 ...

  8. Codeforces 437C The Child and Toy(贪心)

    题目连接:Codeforces 437C  The Child and Toy 贪心,每条绳子都是须要割断的,那就先割断最大值相应的那部分周围的绳子. #include <iostream> ...

  9. Codeforces 442C Artem and Array(stack+贪婪)

    题目连接:Codeforces 442C Artem and Array 题目大意:给出一个数组,每次删除一个数.删除一个数的得分为两边数的最小值,假设左右有一边不存在则算作0分. 问最大得分是多少. ...

随机推荐

  1. VMWare中Centos Minimal最小安装包安装后网络,ftp配置

    1.官网下载centos Minimal安装包,安装. 2.使用ip addr命令查看后没有ip地址显示. 3.点击WMWare的编辑->虚拟网络编辑->选择vmnet0(Bridged) ...

  2. java:CSS(定位,组合选择符,边距,Float,Table的样式,显示和隐藏,换行,盒子模型,iframe和frameset框架)

    1.绝对定位,相对定位,fixed定位(指浏览器窗口定位): <!DOCTYPE html> <html> <head> <meta charset=&quo ...

  3. java驼峰法和下划线法字符串的相互转换

    java驼峰法和下划线法字符串的相互转换 1 import java.util.regex.Matcher; import java.util.regex.Pattern; public class ...

  4. Laravel中一些要记住 的写法

    模型篇: 1.根据数据库部分URL返回完整的URL public function getImageUrlAttribute() { // 如果 image 字段本身就已经是完整的 url 就直接返回 ...

  5. WorkStation Linux 客户端 虚拟机的使用过程

    1. 安装Workstation 版本选择 12.5 以上的版本 2. 安装完成之后 选择 打开虚拟机->选择 ovf文件 选择 ovf 文件 选择读入即可 3. 设置虚拟机的选项: 3.1  ...

  6. [转帖].NET Core 项目指定SDK版本

    .NET Core 项目指定SDK版本 https://www.cnblogs.com/stulzq/p/9503121.html 一. 版本里的坑 自从 .NET Core 2.1.0版本发布以后, ...

  7. 手把手教你用Python搭建自己的量化回测框架【均值回归策略】

    手把手教你用Python搭建自己的量化回测框架[均值回归策略] 引言 大部分量化策略都可以归类为均值回归与动量策略.事实上,只有当股票价格是均值回归或趋势的,交易策略才能盈利.否则,价格是随机游走的, ...

  8. 续AspectJ篇

    这篇将介绍AspectJ的第二种开发方法:基于注解的声明式-AspectJ. 与基于代理类的AOP实现相比,基于XML的声明式AspectJ要便捷的多,但是它也存在一些缺点,那就是在Spring文件中 ...

  9. VeryNginx详细配置说明

    自定义行为 ( Custom Action ) 匹配器 (Matcher) 匹配器 VeryNginx 会收到各种各样的Http请求,当我们定义一条规则(Action)的时候,我们可能会需要限定,这条 ...

  10. vue-cli3.0本地代理cookie跨域请求Nginx配置

    由于后端需要通过请求取前端中的cookie信息,在本地开发模式中,直接请求接口,后端无法拿到前端cookie数据, 经测试需在 vue-cli 中使用代理,如果使用Nginx做反向代理需同时修改Ngi ...