贪心
如果有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. 夏令营讲课内容整理Day 1.

    主要内容是栈和队列. 1.  栈 运算受到限制的线性表.只允许从一端进行插入和删除等操作.这一端便是栈顶,另一端便是栈底. 其实可以把栈想象层任何有底无盖的柱状的容器...毕竟栈满足后进先出的特性.计 ...

  2. 2048小游戏代码解析 C语言版

    2048小游戏,也算是风靡一时的益智游戏.其背后实现的逻辑比较简单,代码量不算多,而且趣味性强,适合作为有语言基础的童鞋来加强编程训练.本篇分析2048小游戏的C语言实现代码. 前言 游戏截图:  游 ...

  3. 关于WebAPI

    1. 现在越来越多的企业以及网站 以及互联网使用WebApi  .那么WebApi 和 普通的WebServices  和WCF 最大的区别是什么了.那就是Web API很多人都会想到Web服务,但是 ...

  4. 汇编之EBP的认识。

    说到EBP就不能忽略了ESP.ESP是一个指针,始终执行堆栈的栈顶.而EBP就是那个所谓的堆栈了. 先看几个例子吧. push ebp ; 把ebp,堆栈的0地址压入堆栈 mov ebp,esp ; ...

  5. 2018/1/27 Zookeeper实现分布式锁

    public class DistributedClient { // 超时时间 private static final int SESSION_TIMEOUT = 5000; // zookeep ...

  6. 【模板小程序】求小于等于N范围内的质数

    //筛法求N以内的素数(普通法+优化),N>=2 #include <iostream> #include <cmath> #include <vector> ...

  7. 有关static静态方法知识的收集

    1.何时使用静态方法: 如果某些操作不依赖具体实例,那它就是静态的,反之如果某些操作是依赖具体实例的(例如访问一个特定会员的名称),那它就应该是实例化的. 2.静态方法和实例方法的区别主要体现在两个方 ...

  8. mysql必知必会

    春节放假没事,找了本电子书mysql必知必会敲了下.用的工具是有道笔记的markdown文档类型. 下面是根据大纲已经敲完的章节,可复制到有道笔记的查看,更美观. # 第一章 了解SQL## 什么是S ...

  9. OpenVPN的那些坑

    遇到的情形 最近遇到一种情况,当需要同时使用到多个VPN连接时,默认的openVPN连接是不支持的,但是可以通过手动配置虚拟网络适配器进行相关的设置. 具体解决方法 基本思路是:在本地的网络连接中添加 ...

  10. NSData 与 NSString 的转化

    1 NSString * str = @"hello, world!"; 2 NSData * data = [str dataUsingEncoding:NSUTF8String ...