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 ...
随机推荐
- Codeforces.1029D.Isolation(DP 分块)
题目链接 \(Description\) 给定长为\(n\)的序列\(A_i\)和一个整数\(K\).把它划分成若干段,满足每段中恰好出现过一次的数的个数\(\leq K\).求方案数. \(K\le ...
- 洛谷p2613【模板】有理数取余
题目 \(c\)等于一个分数,求他的余数,分数是不能直接模的,除以一个数等于乘上这个数的逆元. 所以此题就是求一个逆元,费马小定理求逆元是很方便的,一个快速幂就解决了. 还要注意因为\(a,b\)的值 ...
- [LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- golang web 方案
概要 开发 web 框架 数据库 认证 日志 配置 静态文件服务 上传/下载 发布 docker 打包 部署中遇到的问题 时区问题 概要 轻量的基于 golang 的 web 开发实践. golang ...
- $scope里的$watch方法
一.$watch的作用 监听model,如果model发生变化,则触发某些事情. 二.$watch的格式 $scope. $watch(watchFn,watchAction,deepWatch); ...
- Java ReentrantLock中tryLock与lock的区别(非公平锁与公平锁)
设置同步状态,利用CAS操作. // CAS操作:如果当前状态值等于期望值,则自动将同步状态设置为给定的更新值 protected final boolean compareAndSetState(i ...
- git add无效,git status(modified content, untracked content)
问题一:git status 时文件目录后提示(modified content, untracked content) git add后也添加不上,文件不能提交上去 例如下图: 原因: 该文 ...
- Spring Security 入门—内存用户验证
简介 作为 Spring 全家桶组件之一,Spring Security 是一个提供安全机制的组件,它主要解决两个问题: 认证:验证用户名和密码: 授权:对于不同的 URL 权限不一样,只有当认证的用 ...
- Blend 触发器
原文:Blend 触发器 介绍用定义触发器来控制视频 的 开始 暂停 继续 停止 触发器设置 效果
- System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false
多线程程序中,新创建的线程不能访问UI线程创建的窗口控件,这时如果想要访问窗口的控件,发现无法对其控制. 这时可将窗口构造函数中的CheckForIllegalCrossThreadCalls设置为f ...