贪心
如果有0先变成非0
如果负数的个数 应该变为偶数
之后就是每次将绝对值最小的值加K

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5+5;
typedef long long ll; int N,K,X;
ll A[MAXN];
int tag[MAXN];
struct Node{
ll x; int id;
Node(ll a=0, int b=0):x(a),id(b){}
bool operator <(const Node &T) const {
return x > T.x;
}
};
ll Abs(ll x) {
if(x < 0) x *= -1;
return x;
}
void doo(int id,ll num) {
if(A[id] > 0) A[id] += num;
else A[id] -= num;
}
priority_queue<Node> Q;
int main(){
while(~scanf("%d %d %d",&N,&K,&X)) {
memset(tag,0,sizeof(tag));
while(!Q.empty()) Q.pop(); // int c1 = 0; int c2 = 0;
int c3 = 0; // pos zero neg
for(int i = 1; i <= N; ++i) {
scanf("%lld",&A[i]);
Q.push(Node(Abs(A[i]), i));
}
for(int i = 1; i <= N; ++i) {
if(A[i] < 0) c3 ++;
}
while(K) {
ll x = Q.top().x; int id = Q.top().id;
Q.pop();
if(x == 0) {
if(~c3&1) {
A[id] = -X;
c3 ++;
}else {
A[id] = X;
}
}else if(~c3&1){
ll tt = (x+X)/X;
if(tt > K) {
doo(id, -1ll*K*X);
break;
}else {
doo(id,-1ll*tt*X); K -= tt; K++;
}
c3 ++;
}else {
doo(id,X);
}
// printf("%d %lld\n",id,A[id]);
Q.push(Node(Abs(A[id]),id)); K--;
} for(int i = 1; i <= N; ++i) printf("%lld ",A[i]); printf("\n");
}
return 0;
}

CF374 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. Maxim and Array CodeForces - 721D (贪心)

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

随机推荐

  1. Java程序占用的内存可能会大于Xmx

    很多人认为Xmx和-Xms参数指定的就是Java程序将会占用的内存,但是这实际上只是Java堆对象将会占用的内存.堆只是影响Java程序占用内存数量的一个因素. 除了堆,影响Java程序所占用内存的因 ...

  2. Exchanger

    Exchanger可以在两个线程之间交换数据,只能是2个线程,不支持更多的线程之间互换数据. 当线程A调用Exchange对象的exchange()方法后,他会进入阻塞状态,直到线程B也调用了exch ...

  3. RMI基础

    远程方法调用(Remote Method Invocation,RMI)从JDK1.1就已经实现,它大大增强了Java开发分布式应用的能力.可以实现通过网络完成不同JVM间的通信,不仅可以传递基本的数 ...

  4. JDK8的新特性——Lambda表达式

    JDK8已经发布快4年的时间了,现在来谈它的新特性显得略微的有点“不合时宜”.尽管JDK8已不再“新”,但它的重要特性之一——Lambda表达式依然是不被大部分开发者所熟练运用,甚至不被开发者所熟知. ...

  5. nginx/php-fpm 访问php文件直接下载而不运行

    遇到这种问题,首先确认你web服务器配置中的.PHP是不是被指定给FastCGI server处理: location ~ .php$ { fastcgi_pass ; } 如已配置,那么可能是由于f ...

  6. elk安装配置

    ELK介绍   官网https://www.elastic.co/cn/ 中文指南https://www.gitbook.com/book/chenryn/elk-stack-guide-cn/det ...

  7. maven The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path错误

    对于这个问题的话,请在pom文件中加入 <dependency> <groupId>javax.servlet</groupId> <artifactId&g ...

  8. 手把手教你树莓派实现简易室内监控系统(C)之BOA服务器的搭建

    本篇主要讲利用BOA服务器做室内监控系统的服务器端. 古人云:万事开头靠百度,实在不行就Google.小编也是一步一步的,亲自搭建成功,不能说是万全之策,仅仅是给大家一个参考就满足了. 第一步: 1. ...

  9. python_crawler,批量下载文件

    这个第一个python3网络爬虫,参考书籍是<python网络数据采集>.该爬虫的主要功能是爬取某个网站,并将.rar,.doc,.docx,.zip文件批量下载. 后期将要改进的是,用后 ...

  10. hdu1425 哈希技术

    常用的技巧,把每个数字分别对应数组的下标,如果存在小于零的数字,就统一加一个数使得都能映射到一个下标上去. AC代码: #include<cstdio> #include<cstri ...