Maxim and Array CodeForces - 721D (贪心)
大意: 给定序列, 每次操作选择一个数+x或-x, 最多k次操作, 求操作后所有元素积的最小值
贪心先选出绝对值最小的调整为负数, 再不断选出绝对值最小的增大它的绝对值
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head const int N = 1e6+10;
int n, k, x;
struct _ {
ll w;
int id;
bool operator < (const _ &rhs) const {
return abs(w) > abs(rhs.w);
}
} e[N];
ll ans[N];
int sgn(ll x) {return x<0?-1:1;}
int main() {
scanf("%d%d%d", &n, &k, &x);
int f = 0;
REP(i,1,n) scanf("%lld", &e[i].w), e[i].id=i, f^=e[i].w<0;
if (!f) {
_ *r = max_element(e+1,e+1+n);
if (!r->w) --k,r->w-=x;
else {
int ff = sgn(r->w);
while (k) {
r->w -= sgn(r->w)*x, --k;
if (sgn(r->w)!=ff) break;
}
}
}
priority_queue<_> q;
REP(i,1,n) q.push(e[i]);
while (k) {
_ t = q.top(); q.pop();
t.w += sgn(t.w)*x;
q.push(t), --k;
}
while (q.size()) ans[q.top().id]=q.top().w,q.pop();
REP(i,1,n) printf("%lld ", ans[i]);hr;
}
Maxim and Array CodeForces - 721D (贪心)的更多相关文章
- 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 F. Maxim and Array(构造贪心)
题目描述: Maxim and Array time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- Codeforces 721D [贪心]
/* 不要低头,不要放弃,不要气馁,不要慌张. 题意: 给一列数a,可以进行k次操作,每次操作可以选取任意一个数加x或者减x,x是固定的数.求如何才能使得这个数列所有数乘积最小. 思路: 贪心...讨 ...
- Artem and Array CodeForces - 442C (贪心)
大意: 给定序列$a$, 每次任选$a_i$删除, 得分$min(a_{i-1},a_{i+1})$(无前驱后继时不得分), 求最大得分. 若一个数$x$的两边都比$x$大直接将$x$删除, 最后剩余 ...
- CodeForces - 721D 贪心+优先队列(整理一下优先队列排序情况)
题意: 给你一个长度为n的数组,你可以对其中某个元素加上x或者减去x,这种操作你最多只能使用k次,让你输出操作后的数组,且保证这个数组所有元素的乘积尽可能小 题解: 在这之前我们要知道a*b>a ...
- 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 —— 贪心
题目链接:http://codeforces.com/problemset/problem/721/D D. Maxim and Array time limit per test 2 seconds ...
- Greg and Array CodeForces 296C 差分数组
Greg and Array CodeForces 296C 差分数组 题意 是说有n个数,m种操作,这m种操作就是让一段区间内的数增加或则减少,然后有k种控制,这k种控制是说让m种操作中的一段区域内 ...
- CodeForces - 721D Maxim and Array (贪心)
Recently Maxim has found an array of n integers, needed by no one. He immediately come up with idea ...
随机推荐
- P4313 文理分科
思路 遇到这种利益冲突的最终利益最大化问题 考虑转化为最小割,使得损失的价值最小 相当于文科是S,理科是T,选出最小割就是确定损失代价最小的方案 然后就把S向每个点连一条cap=art[i][j]的边 ...
- HDU 4576 Robot
思路 概率dp 既然是求概率,顺推 显然有转移\(dp[i][j]=dp[i-1][j-w]/2+dp[i-1][w]/2\) 然后是环,注意特判一下 环上不要用取模处理循环的情况,会被卡常 代码 # ...
- 论文笔记之:SeqGAN: Sequence generative adversarial nets with policy gradient
SeqGAN: Sequence generative adversarial nets with policy gradient AAAI-2017 Introduction : 产生序列模拟数 ...
- System.ServiceProcess与System.Configuration.Install命名空间的介绍
System.ServiceProcess 命名空间提供用于实现.安装和控制 Windows 服务应用程序的类.服务是长期运行的可执行文件,其运行没有用户界面 System.ServiceProces ...
- js作用域题目
window.number = 4var obj = { 'number': 4, 'tbl': (function(){ this.number *= 2; console.log(this.num ...
- 2、ansilbe常用模块详解及inventory介绍
Ansible ansible格式: ansible <host-pattern> [-f forks] [-m module_name] [-a args] args: 用法 key=v ...
- 20. --erg--=--org--=--urg-- 做,工作 (词20、21)
词汇速记21
- C++图形开发相关
1. WxWidgets 2. GTK+ 3. U++ Framework 4. QT
- win10常用命令和设置总结
1.常用命令 exit:退出cmd面板; cls:清除cmd面板; 2.常用设置 2.1 services.msc 禁用:以后怎样都不会运行;手动:是打开某些用到它的程序要用到该服务时才会运行; 自动 ...
- Springboot:没有src/main/resources目录(引入图片时(或静态资源时)发现没有该目录)
今天想在Springboot项目想引进静态资源时,发现自己的项目里没有教程里面的 src/main/resources这个目录,目录如下 我的项目目录结构是这样的,但是还不是很清楚下面的那个src目录 ...