https://www.cnblogs.com/qscqesze/p/5925893.html  原博客

http://codeforces.com/group/1EzrFFyOc0/contest/721/problem/D 题目

题意

给你n个数,你可以操作k次,每次使得一个数增加x或者减小x

你要使得最后所有数的乘积最小,问你最后这个序列长什么样子。

题解:

贪心,根据符号的不同,每次贪心的使得一个绝对值最小的数减去x或者加上x就好了

这个贪心比较显然。

假设当前乘积为ANS,那么你改变a[i]的大小的话,那么对答案的影响为ANS/A[i]/*X

然后找到影响最大的就好了。

 #include<iostream>
#include<cstdio>
#include <cctype>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include<cmath>
#include<set>
#include<vector>
#include<stack>
#include<queue>
#include<map>
using namespace std;
#define ll long long
#define mem(a,x) memset(a,x,sizeof(a))
#define se second
#define fi first
const int INF= 0x3f3f3f3f;
const int N=2e5+; ll n,k,x;
ll a[N]; set< pair<ll,ll> > s; //自动排序 int main()
{
cin>>n>>k>>x;
bool flag=; //负数的数目奇偶情况
for(ll i=;i<=n;i++){
scanf("%lld",&a[i]);
if(a[i]<) flag^=;//这技巧要记住
s.insert( make_pair (abs(a[i]),i) );//这技巧要记住
}
for(int i=;i<=k;i++)
{
int j=s.begin()->second; //这技巧要记住
s.erase(s.begin()); if(a[j]<) flag^=; //如果当前这个数是负数,负数数量减少1,讨论这个a[j]
if(!flag){ //除a[i]以外的 负数个数为偶数
a[j]-=x;
}
else{ //除a[i]以外的 负数个数为奇数
a[j]+=x;
}
if(a[j]<) flag^=; //再对改变后的a[j]讨论,如果是负的,负数数量+1;
s.insert( make_pair( abs(a[j]),j) );
}
for(int i=;i<=n;i++)
{
cout<<a[i]<<' ';
}
}

D. Maxim and Array的更多相关文章

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

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

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

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

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

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

  5. 【24.17%】【codeforces 721D】Maxim and Array

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  6. CodeForces - 721D Maxim and Array (贪心)

    Recently Maxim has found an array of n integers, needed by no one. He immediately come up with idea ...

  7. Codeforces Round #374 (Div. 2) D. Maxim and Array

    传送门 分析:其实没什么好分析的.统计一下负数个数.如果负数个数是偶数的话,就要尽量增加负数或者减少负数.是奇数的话就努力增大每个数的绝对值.用一个优先队列搞一下就行了. 我感觉这道题的细节极为多,非 ...

  8. CodeForces 721D Maxim and Array

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

  9. CF374 Maxim and Array

    贪心 如果有0先变成非0 如果负数的个数 应该变为偶数 之后就是每次将绝对值最小的值加K #include<bits/stdc++.h> using namespace std; cons ...

  10. Maxim and Array CodeForces - 721D (贪心)

    大意: 给定序列, 每次操作选择一个数+x或-x, 最多k次操作, 求操作后所有元素积的最小值 贪心先选出绝对值最小的调整为负数, 再不断选出绝对值最小的增大它的绝对值 #include <io ...

随机推荐

  1. <Dynamic Programming> 120 279

    120. Triangle 从倒数第二行找,然后逐个遍历这个DP数组,对于每个数字,和它之后的元素比较选择较小的再加上面一行相邻位置的元素做为新的元素,然后一层一层的向上扫描 class Soluti ...

  2. 学习vue_01

    目录 vue 框架: 框架介绍 基础格式 插值表达式: 文本指令: 事件指令: 属性指令: 小结: js 补充(面向对象): vue 框架: --构建虚拟的DOM结构,(内存内改变对象)- 操作数据的 ...

  3. [LeetCode] 673. Number of Longest Increasing Subsequence 最长递增序列的个数

    Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...

  4. [LeetCode] 99. Recover Binary Search Tree 复原二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  5. 使用GitHub的仓库以及介绍

    一.创建仓库 假如Responsitory name是Hello-World Description一栏中可以设置仓库的说明. Public, Private  在这一栏可以选择Public还是Pri ...

  6. cv2.putText,cv2.rectangle方法

    常用方法:cv2.putText(img,xy,(x1,y1), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), thickness=2)img:要作用的图片xy:显示 ...

  7. mysql truncate 引起的 system lock,导致其他进程等待

    1.现状:上线新项目,导致api服务延迟,cpu正常,内存正常,连接数正常,sql性能正常,sql进程正常(初步分析) 最后再次分析sql进程才发现 由于该 truncate table name ; ...

  8. elasticsearch6设置默认分片数和副本数

    elasticsearch6设置索引的默认分片数和副本数已经不是在elasticsearch.yml文件中了,而是使用了一个索引模板的东西 curl -XPUT 'http://10.27.12.16 ...

  9. java8时间处理实例

    实例: package com.javaBase.time; import java.time.Clock; import java.time.LocalDate; import java.time. ...

  10. 【JVM】jmap命令详解----查看JVM内存使用详情

    linux获取java进程PID: https://www.cnblogs.com/sxdcgaq8080/p/10734752.html 如果命令使用过程中报错,可能解决你问题的方案: https: ...