D. Maxim and Array
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的更多相关文章
- 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 线段树+贪心
D. Maxim and Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 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 F. Maxim and Array(构造贪心)
题目描述: Maxim and Array time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- 【24.17%】【codeforces 721D】Maxim and Array
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- CodeForces - 721D Maxim and Array (贪心)
Recently Maxim has found an array of n integers, needed by no one. He immediately come up with idea ...
- Codeforces Round #374 (Div. 2) D. Maxim and Array
传送门 分析:其实没什么好分析的.统计一下负数个数.如果负数个数是偶数的话,就要尽量增加负数或者减少负数.是奇数的话就努力增大每个数的绝对值.用一个优先队列搞一下就行了. 我感觉这道题的细节极为多,非 ...
- CodeForces 721D Maxim and Array
贪心,优先队列. 先看一下输入的数组乘积是正的还是负的. ①如果是负的,也就是接下来的操作肯定是让正的加大,负的减小.每次寻找一个绝对值最小的数操作就可以了. ②如果是正的,也是考虑绝对值,先操作绝对 ...
- CF374 Maxim and Array
贪心 如果有0先变成非0 如果负数的个数 应该变为偶数 之后就是每次将绝对值最小的值加K #include<bits/stdc++.h> using namespace std; cons ...
- Maxim and Array CodeForces - 721D (贪心)
大意: 给定序列, 每次操作选择一个数+x或-x, 最多k次操作, 求操作后所有元素积的最小值 贪心先选出绝对值最小的调整为负数, 再不断选出绝对值最小的增大它的绝对值 #include <io ...
随机推荐
- WIMBuilder2软件包及精简方案,请把补丁包放到指定位置
WIMBuilder2软件包及精简方案请把补丁包放到指定位置WimBuilder2-20190901\Projects\WIN10XPE\目录下面精简方案测试适用于LTSB2019.17763.316 ...
- 【贪心】umi的函数
原题传送门 思路 这道题乍一看很难,然而实际在草纸上一模拟,结果就出来了. 分析:这道题实际上要么无解,要么有无数个解,因而题目只要求输出了一个解(明白这点题目就做出来一半了). 题中,规定所求z满足 ...
- CentOS6.9安装MySQL5.6
1 检查系统是否自带MySQL 检查系统是否自带MySQL yum list installed | grep mysql 下面结果说明系统自带MySQL 卸载系统自带MySQL yum -y rem ...
- 解决 ora-01795 的问题
''' <summary> ''' 在 oracle 里 , where in 语句有可能造成问题 : ORA-01795:列表中的最大表达式数为1000 ''' 如果我们在拼接where ...
- k8s之系统组件架构-02
k8s系统架构图 网络组件:calico+kube-proxy(IPVS) 网络暴露:traefik+ingress,分别对HTTP与TCP的服务暴露 存储:glusterfs(heketi管理) 日 ...
- Shell脚本——显示系统上的登录用户数
写一个脚本showlogged.sh,其用法格式为: showlogged.sh -v -c -h|--help 其中,-h选项只能单独使用,用于显示帮助信息:-c选项时,显示当前系统上登录的所有用户 ...
- linux录制终端信息并回放
我们通常会录制桌面环境视频来作为教程使用,但是视频需要大量的存储空间,而终端脚本文件仅仅是一个文本文件,其文件大小不过是KB级别 1, 开始录制终端会话 [root@VM_0_15_centos ~] ...
- vue需求表单有单位(时分秒千克等等)
需求如下: 问题分析: 因为用elementui组件 el-input 相当于块级元素,后面的单位<span>分</span>会被挤下去,无法在同一水平. 解决方法: 不用它的 ...
- 如何解决aws解绑银行卡问题?
首先先来说明一下我自己的情况? 一年的免费使用 前提:没有开启任何的实例服务 先贴一条官方的解释 关于我小白一个.学校课程要求使用aws,注册之后在网络上看到一堆人踩坑,aws的扣费就是个坑! 预授权 ...
- [转帖]Linux date命令的用法(转)
Linux date命令的用法(转) https://www.cnblogs.com/asxe/p/9317811.html 1.命令:date 2.命令功能:date 可以用来显示或设定系统的日期与 ...