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. STL——sort函数的实现原理

    实现原理 sort结合了快速排序.堆排序.直接插入排序三种排序方法. 根据不同的数量级别以及不同情况,能自动选用合适的排序方法.当数据量较大时采用快速排序,分段递归.一旦分段后的数据量小于某个阀值,为 ...

  2. [LeetCode] 154. Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二

      Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i. ...

  3. 【cf比赛记录】Codeforces Round #605 (Div. 3)

    比赛传送门 Div3真的是暴力杯,比div2还暴力吧(这不是明摆的嘛),所以对我这种一根筋的挺麻烦的,比如A题就自己没转过头来浪费了很久,后来才醒悟过来了.然后这次竟然还上分了...... A题:爆搜 ...

  4. java web开发入门五(ssh整合)基于intellig idea

    SSH整合 1.引入jar包 Struts 核心jar Hibernate 核心jar Spring Core  核心功能 Web  对web模块支持 Aop   aop支持 Orm   对hiber ...

  5. portal项目启动问题

    错误信息: Disconnected from the target VM, address: '127.0.0.1:58909', transport: 'socket' Process finis ...

  6. Python学习教程(一)自学资源分享

    Python 可以用来做什么? 在我看来,基本上可以不负责任地认为,Python 可以做任何事情.无论是从入门级选手到专业级选手都在做的爬虫,还是Web 程序开发.桌面程序开发还是科学计算.图像处理, ...

  7. 在Mu-kittenbot中使用Robotbit固件

    首先,先下载安装支持robotbit扩展板的Mu: http://cdn.kittenbot.cn/mu/mu-kittenbot.exe 标准的3针插口,信号,正电,负电,可接市面的arduino模 ...

  8. Git的使用--如何安装和使用 github,让小白不在那么白 (一)(超详解)

    简介 刚开始写了关于如何将本地代码上传到github上,但是有些小伙伴们不清楚如何安装Git,这一篇就给小伙伴们普及一下Git的安装和使用.适合刚开始用git的小白,大神或者大佬请绕道. 实际项目开发 ...

  9. 提高性能,MySQL 读写分离环境搭建

    这是松哥之前一个零散的笔记,整理出来分享给大伙! MySQL 读写分离在互联网项目中应该算是一个非常常见的需求了.受困于 Linux 和 MySQL 版本问题,很多人经常会搭建失败,今天松哥就给大伙举 ...

  10. 关于IDEA中web项目中web.xml配置文件标红的解决办法

    原文链接 https://blog.csdn.net/qq_33451695/article/details/86684127 解决方法前提:web.xml没有实际错误,但依然被web.xml标红 出 ...