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 ...
随机推荐
- 配置serv-u access数据库遇到的一些问题
配置好access数据库后,需要建个web页面来供用户修改密码,但修改时,提示:odbc被占用无法打开. serv-u一直在保持打开access数据库.我们需要将数据库设置默认打开方式为共享,不锁定. ...
- PBKDF2WithHmacSHA1算法
主要用于明文密码加密字符串存入数据库.由棱镜门思考.目前大部分企业中都是明文密码.一旦被攻破.危害非常大.现在主流加密技术是MD5加密.不过MD5的存在小概率碰撞(根据密码学的定义,如果内容不同的明文 ...
- Factovisors - PC110704
欢迎访问我的新博客:http://www.milkcu.com/blog/ 原文地址:http://www.milkcu.com/blog/archives/uva10139.html 原创: 作者: ...
- c# in deep 之Lambda表达式
从很多方面,Lambda表达式都可以看作是C# 2的匿名方法的一种演变.匿名方法能做的几乎一切事情都可以用Lambda表达式来完成,而且其更简洁.易读.下面是一个简单例子. class Film ...
- noip模拟赛:部队[技巧?思想?]
王国军总指挥——卡西乌斯准将决定重建情报局,需要从全国各地挑选有能力的士兵,选择的标准为A,B两种能力.对于每个候选士兵,如果存在另一名士兵的两项能力均大于等于他,那么他将被淘汰.(注意:若两名士兵两 ...
- SugarSync网盘之XML解析
iOS的XML解析 刚在应用里支持了SugarSync网盘.其实也是第一次听说这个网盘,不过在国外貌似还蛮有名,这些都不是重点,重点是借此来总结一下iOS的XML解析.Xml想必也不陌生了,但是在iO ...
- ASP.NET交互Rest服务接口(Jquery的Get与Post方式)
ASP.NET交互Rest服务接口(Jquery的Get与Post方式) 本文将通过一个简单的实例,介绍如何创建一个Rest服务接口,以及通过JQUERY去对它进行调用;主要采取两种方式分别为Get跟 ...
- 关于GNU软件的版本号命名规则
这里所说的版本号命名并非指“正式版”.“测试版”这种方式,而是在讨论版本编号的问题,例如Linux内核3.0以后的版本命名规则是3.A.B,A是内核的版本,B是安全补丁.那么对于一般的软件的版本号命名 ...
- MVC test
1,index @{ ViewBag.Title = "Index"; } <!DOCTYPE html> <html> <head> < ...
- 基于easyui的webform扩展(续)
基于easyui的webform扩展(续) 回顾 <前端基于easyui的mvc扩展>.<前端基于easyui的mvc扩展(续)>.<基于easyui的webform扩展 ...