CodeForces 721D Maxim and Array
贪心,优先队列。
先看一下输入的数组乘积是正的还是负的。
①如果是负的,也就是接下来的操作肯定是让正的加大,负的减小。每次寻找一个绝对值最小的数操作就可以了。
②如果是正的,也是考虑绝对值,先操作绝对值最小的那个数,直到那个数字的符号发生变化就停止操作,接下来就是第①步。
#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的更多相关文章
- Codeforces F. Maxim and Array(构造贪心)
题目描述: Maxim and Array time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- 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 ...
- 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 442C Artem and Array(stack+贪婪)
题目连接:Codeforces 442C Artem and Array 题目大意:给出一个数组,每次删除一个数.删除一个数的得分为两边数的最小值,假设左右有一边不存在则算作0分. 问最大得分是多少. ...
- Codeforces Round #504 D. Array Restoration
Codeforces Round #504 D. Array Restoration 题目描述:有一个长度为\(n\)的序列\(a\),有\(q\)次操作,第\(i\)次选择一个区间,将区间里的数全部 ...
- 【24.17%】【codeforces 721D】Maxim and Array
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- CodeForces - 721D Maxim and Array (贪心)
Recently Maxim has found an array of n integers, needed by no one. He immediately come up with idea ...
- Maxim and Array CodeForces - 721D (贪心)
大意: 给定序列, 每次操作选择一个数+x或-x, 最多k次操作, 求操作后所有元素积的最小值 贪心先选出绝对值最小的调整为负数, 再不断选出绝对值最小的增大它的绝对值 #include <io ...
随机推荐
- iis7 下配置MVC问题
HTTP 错误 404.0 - Not Found 您要找的资源已被删除.已更名或暂时不可用. 详细错误信息 模块 IIS Web Core 通知 MapRequestHandler 处理程序 Sta ...
- ACE模板之Jqgrid
Asp.Net MVC中使用ACE模板之Jqgrid 第一次看到ACE模板,有种感动,有种相见恨晚的感觉,于是迅速来研究.它本身是基于bootstrap和jqueryui,但更nice,整合之后为 ...
- [转载]Nginx 反向代理、负载均衡、页面缓存、URL重写及读写分离详解
大纲 一.前言 二.环境准备 三.安装与配置Nginx 四.Nginx之反向代理 五.Nginx之负载均衡 六.Nginx之页面缓存 七.Nginx之URL重写 八.Nginx之读写分离 注,操作系统 ...
- Bootstrap 模态框
Bootstrap 模态框(也可以说的弹出层) 最近在尝试使用bootstrap的模态框 使用模态框主要要引入一下几个js和css: bootstrap.css jquery.1.9.1.js(这个可 ...
- 7.25 RPN转换
思想: 目的:将中缀表达式(即标准形式的表达式)转换为后缀式. 例子:a+b*c+(d*e+f)*g转换成abc*+de*f+g*+ 转换原则: 1.当读到一个操作数时,立即将它放到输出中.操作符则不 ...
- linux网卡驱动安装及锐捷使用
原创博文,转载请注明出处 先吐槽一下,以前装了个Centos win7双系统, 然后手贱一不小心把启动文件给删了,接下来就用grub恢复启动文件,整了一天也没搞出来还把win7的Boot Manage ...
- C++中文件的操作
#include <iostream> #include <fstream> using namespace std; int main() { char s[27],m[27 ...
- CODEFORCES#274 DIV2
A[傻逼题] 大意:给你a,b,c三个数,你可以在其中加上括号,加号,乘号,使得到的值最大 就是问你 a+b+c,a*(b+c),(a+b)*c,a*b*c,(a+c)*b 哪个最大! 我去...这不 ...
- 算法打基础——HashⅡ: 全域哈希与完美哈希
这一节涉及数学超级多,各种数论知识,各种不明觉厉! 看了几遍,才勉强看懂一些,所以这 篇稍微简单的介绍着两种hash table, 免得瞎说说错了. 这一讲的主要知识点是:1. 全域哈希及构造 ...
- WinForm酒店管理软件--框架
WinForm酒店管理软件--框架 搞软件开发4年多了,现在自认为还是菜鸟,从一开始走上工作岗位各种技术对我都很新奇没解决一个问题都觉得很伟大到后来开始对出路的迷茫,到现在我坚信学什么技术不重要,做什 ...