贪心
如果有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. 使用jvisualvm来远程观察Java程序及jvisualvm插件[转]

    jvisualvm是JDK自带的监控JVM运行状况的工具,利用jvisualvm可以查看JVM的运行.GC情况:线程的运行情况:内存中类及实例的情况等. 使用jvisualvm来远程观察Java程序, ...

  2. Codeforces Round #402 (Div. 2)

    Codeforces Round #402 (Div. 2) A. 日常沙比提 #include<iostream> #include<cstdio> #include< ...

  3. Python中Template使用的一个小技巧

    Python中Template是string中的一个类,可以将字符串的格式固定下来,重复利用. from string import Template s = Template("there ...

  4. Selenium+PhantomJS实现简易有道翻译爬虫

    Selenium一款自动化测试工具,当然用来写爬虫也是没有问题的.它支持Chrome.Safari.Firefox等主流界面式浏览器,另外它也支持多种语言开发,比如 Java,C,Ruby,Pytho ...

  5. ECMAScript 6 笔记(二)

    ES6中的基本扩展 一.字符串的扩展 1. 字符的Unicode表示法 用两个双字节的形式表达字符时,如果直接在\u后面跟上超过0xFFFF的数值(比如\u20BB7),JavaScript会理解成\ ...

  6. checkbox/input文本框与文字对齐

    3种方法都能实现checkbox文本框或radio文本框与文字对齐: <meta charset="utf-8"> <input style="vert ...

  7. POJ 1021 2D-Nim

    Description The 2D-Nim board game is played on a grid, with pieces on the grid points. On each move, ...

  8. JavaScript面向对象学习笔记

    JavaScript 常被描述为一种基于原型的语言 (prototype-based language)--每个对象拥有一个原型对象,对象以其原型为模板.从原型继承方法和属性.原型对象也可能拥有原型, ...

  9. Java集合中的AbstractMap抽象类

    jdk1.8.0_144 AbstractMap抽象类实现了一些简单且通用的方法,本身并不难.但在这个方法中有两个方法非常值得关注,keySet和values方法源码的实现可以说是教科书式的典范. 抽 ...

  10. Eclipse中如何忽略报错的js文件

    https://jingyan.baidu.com/article/4f7d5712d3701a1a20192786.html