链接:

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. C++ lower_bound

    代码 #include<iostream> #include<algorithm> using namespace std; int main(void) { ] = { 0. ...

  2. java:Maven(Maven_ssm)

    1.maven_ssm: DOS命令向maven仓库导入jar包: mvn install:install-file -Dfile=F:\jars\json-lib-2.4-jdk15.jar -Dg ...

  3. html+css实现奥运五环(环环相扣)

    <!DOCTYPE html> <html> <head> <title>奥运五环</title> <style type=" ...

  4. jenkins自动化部署工具

    jenkins自动化测试 & 持续集成 知识点: 1.下载地址:jenkins.io download:

  5. C#动态调用带有SoapHeader验证的WebServices

    http://blog.csdn.net/u012995964/article/details/54573143 本文记录C#中通过反射动态的调用带有SoapHeader验证的WebServices服 ...

  6. Docker&Java&Mysql&Python3&Supervisor&Elasticsearch安装

    目录 docker 安装java 安装mysql 安装Mysql8 安装python3 安装supervisor 安装ElasticSearch 打包images docker yum install ...

  7. CentOS Linux修改默认Bash shell为Zsh shell

    Shell是在程序员与服务器间建立一个桥梁,它对外提供一系列命令,让我们得以控制服务器.常用的Bash就是Shell的一种,也是Linux下默认Shell程序.这里介绍一种更强大的.更人性化的Shel ...

  8. PTA(Basic Level)1011.A+B和C

    给定区间 [−231,231] 内的 3 个整数 A.B 和 C,请判断 A+B 是否大于 C. 输入格式: 输入第 1 行给出正整数 T (≤10),是测试用例的个数.随后给出 T 组测试用例,每组 ...

  9. Python的入门(day1)

    一:Python的起源 Python的创始人为Guido van Rossum.1989年圣诞节期间,在阿姆斯特丹,Guido为了打发圣诞节的无趣,决心开发一个新的脚本解释程序,做为ABC 语言的一种 ...

  10. oracle 数据库备份与恢复

    oracle 数据库备份与恢复 包含四个部分: 1.数据泵备份与恢复 2.rman备份与恢复 3.CSV增量备份恢复 4.截库操作 1.数据泵备份与恢复 expdp/ / impdp 时的 CONTE ...