CF374 Maxim and Array
贪心
如果有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的更多相关文章
- 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 线段树+贪心
D. Maxim and Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 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 F. Maxim and Array(构造贪心)
题目描述: Maxim and Array time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- 【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 ...
- Codeforces Round #374 (Div. 2) D. Maxim and Array
传送门 分析:其实没什么好分析的.统计一下负数个数.如果负数个数是偶数的话,就要尽量增加负数或者减少负数.是奇数的话就努力增大每个数的绝对值.用一个优先队列搞一下就行了. 我感觉这道题的细节极为多,非 ...
- CodeForces 721D Maxim and Array
贪心,优先队列. 先看一下输入的数组乘积是正的还是负的. ①如果是负的,也就是接下来的操作肯定是让正的加大,负的减小.每次寻找一个绝对值最小的数操作就可以了. ②如果是正的,也是考虑绝对值,先操作绝对 ...
- Maxim and Array CodeForces - 721D (贪心)
大意: 给定序列, 每次操作选择一个数+x或-x, 最多k次操作, 求操作后所有元素积的最小值 贪心先选出绝对值最小的调整为负数, 再不断选出绝对值最小的增大它的绝对值 #include <io ...
随机推荐
- Newtonsoft.Json 操作 JSON 字符串
Newtonsoft.Json介绍 在做开发的时候,很多数据交换都是以json格式传输的.而使用Json的时候,我们很多时候会涉及到几个序列化对象的使用:DataContractJsonSeriali ...
- EasyUI实现更换主题能过样式添加id实现
EasyUI实现更换主题能过样式添加id实现,将原来的样式值添加到cookie中保存,这样下次浏览器访问时,就是我们原来选择的样式! 首先将easyui的样式文件加入一个ID,这里命名为easyuiT ...
- Alex: 2018年对混合现实MR的展望
原文作者:Alex Kipman, 微软操作系统工程院技术院士 Hello 大家好! 难以置信我们已经走过了2018年的头两个月了. 每年一月份我都会去巴西省亲,和我的家人欢聚一堂,度过一个美好的假日 ...
- 【Tools】ubuntu16.04安装搜狗输入法
Ubuntu16,04 安装搜狗输入法 1.下载搜狗输入法的安装包 下载地址为:http://pinyin.sogou.com/linux/ 2.按键Ctr+Alt+T打开终端,输入以下命令切换到下载 ...
- 携程Apollo(阿波罗)配置中心用户管理和部门管理
Apollo是配置管理系统,会提供权限管理(Authorization),理论上是不负责用户登录认证功能的实现(Authentication).所以Apollo定义了一些SPI用来解耦,Apollo接 ...
- 配置python虚拟环境Virtualenv及pyenv
pyenv pyenv 可以让机器安装各种不同版本的python pyenv install --list 查看可以安装的python版本 pyenv versions 查看已安装的python版本 ...
- linux下双网卡的绑定
如果服务器上有两快网卡就可以把它绑定为一块虚拟的网卡,如果一块网卡坏了另一块还可以继续工作,增加了冗余度和负载,具体做法如下: 新建一个虚拟的网卡,命令如下: iv /etc/sysconfig/ne ...
- map,vector 等容器内容的循环删除问题(C++)
map,vector 等容器内容的循环删除问题(C++) map,vector等容器的循环删除不能用普通的方法删除: for(auto p=list.begin();p!=list.end();p++ ...
- Python标准异常总结
Python标准异常总结 AssertionError 断言语句(assert)失败 AttributeError 尝试访问未知的对象属性 EOFError 用户输入文件末尾标志EOF(Ctrl+d ...
- c++ 回调函数使用
普通回调 #include<stdio.h> void printWelcome(int len) { printf("welcome -- %d\n", len); ...