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 ...
随机推荐
- linux服务创建及jq配置服务列表查看
1.应用背景 随着业务需求,后台处理服务不断增多,对于这些服务或后台程序的查看.更新操作越来越凌乱,所以我们首先需要一个服务列表查看工具,方便查看各 服务的端口.运行状态.jar包路径等等. 2.创建 ...
- Birdge(桥接)模式
1.出现原因 1.同一个类型,有两个变化的维度(两个维度的抽象:一个抽象部分的抽象,一个实现部分的抽象) 2.如何应对这种“多维度的变化”?如何利用面向对象技术来使得同一类型可以轻松地沿着两个方向变化 ...
- 1关于script标签属性,注意点,浏览器文档模式,各种数据类型的转化
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- SZU:A12 Jumping up and down
Judge Info Memory Limit: 32768KB Case Time Limit: 10000MS Time Limit: 10000MS Judger: Number Only Ju ...
- Dynamics CRM 提示“操作无效”
今天打开一个许久没用的CRM学习环境发现出现下面的错误.顿时感觉到莫名其妙.(貌似CRM的所有错误都让人感觉到莫名其妙) 于是查看系统日志,错误如下: Current key (KeyType : C ...
- [Usaco2007 Dec]穿越泥地[bfs][水]
Description 清早6:00,Farmer John就离开了他的屋子,开始了他的例行工作:为贝茜挤奶.前一天晚上,整个农场刚经受过一场瓢泼大雨的洗礼,于是不难想见,FJ 现在面对的是一大片泥泞 ...
- Grub禁用UUID
这个属于一个个人喜好问题,我每次看到 df -h 的结果都很郁闷,根目录那一行设备是用uuid表示的,那一串字符真是够长的,看起来非常别扭,所以就自己修改了一下/etc/default/grub文件. ...
- mac下安装eclipse以及python
因为前几天刚重装了我的mac osx 系统,从昨天开始我就在安装各种软件,当我安装好破解版的myeclipse后,在我安装pydev插件的时候,虽然现实成功但是在preference中死活找不到pyd ...
- XML中的五个保留字符及实体引用
字符名称 字符 实体引用 和 & & 大于号 > > 小于号 < < 单引号 ‘ ' 双引号 “ " 在XML文档中,构成元素内 ...
- CFileDialog类与16进制格式的dat文件
CFileDialog类与16进制格式的dat文件 要将数据保存为.dat文件,而且是16进制的数字,怎么保存? 要读取.dat文件,文件是16进制的,怎么读取? 用CFileDialog类可以得到[ ...