CodeForces-721D-Maxim and Array(优先队列,贪心,分类讨论)
链接:
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(优先队列,贪心,分类讨论)的更多相关文章
- CodeForces 721D Maxim and Array
贪心,优先队列. 先看一下输入的数组乘积是正的还是负的. ①如果是负的,也就是接下来的操作肯定是让正的加大,负的减小.每次寻找一个绝对值最小的数操作就可以了. ②如果是正的,也是考虑绝对值,先操作绝对 ...
- Codeforces F. Maxim and Array(构造贪心)
题目描述: Maxim and Array time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- 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 ...
- 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 线段树+贪心
D. Maxim and Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array 分类讨论连续递推dp
题意:给出一个 数列 和一个x 可以对数列一个连续的部分 每个数乘以x 问该序列可以达到的最大连续序列和是多少 思路: 不是所有区间题目都是线段树!!!!!! 这题其实是一个很简单的dp 使用的是分 ...
- Codeforces 437C The Child and Toy(贪心)
题目连接:Codeforces 437C The Child and Toy 贪心,每条绳子都是须要割断的,那就先割断最大值相应的那部分周围的绳子. #include <iostream> ...
- Codeforces 442C Artem and Array(stack+贪婪)
题目连接:Codeforces 442C Artem and Array 题目大意:给出一个数组,每次删除一个数.删除一个数的得分为两边数的最小值,假设左右有一边不存在则算作0分. 问最大得分是多少. ...
随机推荐
- MySQL 树形结构 根据指定节点 获取其所有叶子节点
背景说明 需求:MySQL树形结构, 根据指定的节点,获取其下属的所有叶子节点. 叶子节点:如果一个节点下不再有子节点,则为叶子节点. 问题分析 1.可以使用类似Java这种面向对象的语言,对节点集合 ...
- 网易云课堂_C++程序设计入门(下)_第9单元:白公曾咏牡丹芳,一种鲜妍独“异常”_第9单元 - 作业5:OJ编程 - 使用异常进行图形类的错误处理
第9单元 - 作业5:OJ编程 - 使用异常进行图形类的错误处理 查看帮助 返回 温馨提示: 1.本次作业属于Online Judge题目,提交后由系统即时判分. 2.学生可以在作业截止时间之前 ...
- oracle数据字典视图
数据字典的概念 还记得我们在手工建库的时候,运行的两个脚本文件.一个是catalog.sql,另一个是catproc.sql.catalog.sql是用来创建数据库的内部字典表的.catp ...
- java:IO流(处理流(缓冲流,转换流,数据流),对象的序列化,Properties)
字节缓冲流:(BufferedInputStream,BufferedOutStream) *按照流的功能来分:节点流和处理流 *节点流可以直接操作数据源: *InputStream *--FileI ...
- 用poi从excel文档导入数据
import org.apache.commons.lang3.StringUtils; import org.apache.poi.hssf.usermodel.HSSFWorkbook; impo ...
- 将dos窗口调白教程
将dos弹出窗口调白教程 第1步:同时按住Win+R键,输入cmd 第2步:右键点击标题栏 第3步:点击默认值,然后再点击颜色 第四步:将窗口颜色设置为白色,字体颜色设置为黑色(效果测试)
- nodejs版本控制:nvm use命令失效
Downloading npm version ... Download failed. Rolling Back. Rollback failed. remove C:\Users\Administ ...
- Python3.7 下安装pyqt5
第一步:首先进入python安装目录下的 [scripts]. 第二步:执行安装pyqt5的命令:python37 -m pip install pyqt5 出现以下安装过程代表安装成功. 第三步:在 ...
- jsp运行环境的安装和配置
1.JDK的安装和配置 1)下载jdk,我下载的是1-jdk-6u26-windows-i586.exe,放在D:\StudySystem\JavaWeb\jdk目录下. 2)安装jdk,直接你下载的 ...
- Java 读取application.properties配置文件中配置
实际开发中若需要读取配置文件application.properties中的配置,代码如下.例:读取配置文件中name属性配置值: 代码如下: import org.springframework.c ...