贪心,优先队列。

先看一下输入的数组乘积是正的还是负的。

①如果是负的,也就是接下来的操作肯定是让正的加大,负的减小。每次寻找一个绝对值最小的数操作就可以了。

②如果是正的,也是考虑绝对值,先操作绝对值最小的那个数,直到那个数字的符号发生变化就停止操作,接下来就是第①步。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c = getchar();
x = ;
while(!isdigit(c)) c = getchar();
while(isdigit(c)) { x = x * + c - ''; c = getchar(); }
} LL ABS(LL a)
{
if(a>) return a;
return -a;
} const int maxn=;
int n,k; LL x;
struct X
{
LL x,y;int p;
bool operator < (const X &a) const {
return y>a.y;
}
}s[maxn]; bool cmp2(X a,X b)
{
return a.y<b.y;
} priority_queue<X>q;
int cnt;
LL ans[maxn]; int main()
{
scanf("%d%d%lld",&n,&k,&x);
for(int i=;i<=n;i++)
{
scanf("%lld",&s[i].x), s[i].p=i;
s[i].y=ABS(s[i].x);
if(s[i].x<) cnt++;
} if(cnt%==)
{
sort(s+,s++n,cmp2); if(s[].x<)
{
while(k&&s[].x<=)
{
s[].x+=x;
k--;
s[].y=ABS(s[].x);
}
}
else
{
while(k&&s[].x>=)
{
s[].x-=x;
k--;
s[].y=ABS(s[].x);
}
} for(int i=;i<=n;i++) q.push(s[i]); while(k)
{
X h=q.top(); q.pop();
if(h.x>=) h.x+=x;
else h.x-=x;
h.y=ABS(h.x);
q.push(h); k--;
} while(!q.empty())
{
ans[q.top().p]=q.top().x;
q.pop();
}
}
else
{
for(int i=;i<=n;i++) q.push(s[i]); while(k)
{
X h=q.top(); q.pop();
if(h.x>=) h.x+=x;
else h.x-=x;
h.y=ABS(h.x);
q.push(h); k--;
} while(!q.empty())
{
ans[q.top().p]=q.top().x;
q.pop();
}
} for(int i=;i<=n;i++) cout<<ans[i]<<" ";
cout<<endl; return ;
}

CodeForces 721D Maxim and Array的更多相关文章

  1. Codeforces F. Maxim and Array(构造贪心)

    题目描述: Maxim and Array time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  2. Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心

    D. Maxim and Array 题目连接: http://codeforces.com/contest/721/problem/D Description Recently Maxim has ...

  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 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 ...

  5. Codeforces 442C Artem and Array(stack+贪婪)

    题目连接:Codeforces 442C Artem and Array 题目大意:给出一个数组,每次删除一个数.删除一个数的得分为两边数的最小值,假设左右有一边不存在则算作0分. 问最大得分是多少. ...

  6. Codeforces Round #504 D. Array Restoration

    Codeforces Round #504 D. Array Restoration 题目描述:有一个长度为\(n\)的序列\(a\),有\(q\)次操作,第\(i\)次选择一个区间,将区间里的数全部 ...

  7. 【24.17%】【codeforces 721D】Maxim and Array

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. CodeForces - 721D Maxim and Array (贪心)

    Recently Maxim has found an array of n integers, needed by no one. He immediately come up with idea ...

  9. Maxim and Array CodeForces - 721D (贪心)

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

随机推荐

  1. 安装SQL Server 2005 - 初学者系列 - 学习者系列文章

    初学者阶段,建议从数据库为基础入手进行学习. 下面介绍微软的SQL Server 2005数据库的安装. 首先,从下列地址获取SQL Server 2005的安装程序. ed2k://|file|cs ...

  2. 通过如何通过js实现复制粘贴功能

    在ie中window.clipboardData(剪切板对象)是可以被获取,所以利用这个方法我们可以实现在IE当中复制粘贴的功能,demo如下! <html> <head> & ...

  3. highchart几个图表马金摘要

    1)  Js的引入顺序不对,导致highchart的图表出不来. 由于Highchart插件中用到了jquery,jquery的js要在引入highchart的js之前引入,否则当会导致当载入high ...

  4. git 简单教程更新

    0.初始化 $ git config --global user.name "xxx" $ git config --global user.email "xxx@gma ...

  5. 【转】GitHub 优秀的 Android 开源项目

    转自:http://blog.csdn.net/shulianghan/article/details/18046021 主要介绍那些不错个性化的View,包括ListView.ActionBar.M ...

  6. DropDownList和GridView用法

    DropDownList和GridView用法   DropDownList控件和GridView控件在Asp.net中相当常用,以下是控件的解释,有些是常用的,有些是偶尔的,查找.使用.记录,仅此而 ...

  7. Bootstrap 网格系统

    自版本 2.3.2 起,Bootstrap 提供了两种类型的网格,默认网格系统和流动网格系统.默认的网格系统是 940px 宽和 12 列.本文主要讲解第一种. 首先让我们看,怎么来应用默认网格系统 ...

  8. 红黑树LLRB

    LLRB——红黑树的现代实现 一.本文内容 以一种简明易懂的方式介绍红黑树背后的逻辑实现2-3-4树,以及红黑树的插入.删除操作,重点在2-3-4树与红黑树的对应关系上,并理清红黑树相关操作的来龙去脉 ...

  9. SSL协议的握手过程

    SSL握手的目的 第一,客户端与服务器需要就一组用于保护数据的算法达成一致. 第二,它们需要确立一组由那些算法所使用的加密密钥. 第三,握手还可以选择对客户端进行认证. SSL 握手概述 SSL 握手 ...

  10. Java并发性和多线程介绍

    java并发性和多线程介绍: 单个程序内运行多个线程,多任务并发运行 多线程优点: 高效运行,多组件并行.读->操作->写: 程序设计的简单性,遇到多问题,多开线程就好: 快速响应,异步式 ...