2019.8.15乘兴打Codeforces Round #569 (Div. 2)小记B. Nick and Array
B. Nick and Array time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output Nick had received an awesome array of integers a=[a1,a2,…,an] as a gift for his 5 birthday from his mother. He was already going to explore its various properties but after unpacking he was disappointed a lot because the product a1⋅a2⋅…an of its elements seemed to him not large enough.
He was ready to throw out the array, but his mother reassured him. She told him, that array would not be spoiled after the following operation: choose any index i (1≤i≤n) and do ai:=−ai−1.
For example, he can change array [3,−1,−4,1] to an array [−4,−1,3,1] after applying this operation to elements with indices i=1 and i=3.
Kolya had immediately understood that sometimes it’s possible to increase the product of integers of the array a lot. Now he has decided that he wants to get an array with the maximal possible product of integers using only this operation with its elements (possibly zero, one or more times, as many as he wants), it is not forbidden to do this operation several times for the same index.
Help Kolya and print the array with the maximal possible product of elements a1⋅a2⋅…an which can be received using only this operation in some order.
If there are multiple answers, print any of them.
Input The first line contains integer n (1≤n≤105) — number of integers in the array.
The second line contains n integers a1,a2,…,an (−106≤ai≤106) — elements of the array
Output Print n numbers — elements of the array with the maximal possible product of elements which can be received using only this operation in some order from the given array.
If there are multiple answers, print any of them.
Examples inputCopy 4 2 2 2 2 outputCopy -3 -3 -3 -3 inputCopy 1 0 outputCopy 0 inputCopy 3 -3 -3 2 outputCopy -3 -3 2B. Nick and Array time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output Nick had received an awesome array of integers a=[a1,a2,…,an] as a gift for his 5 birthday from his mother. He was already going to explore its various properties but after unpacking he was disappointed a lot because the product a1⋅a2⋅…an of its elements seemed to him not large enough.
He was ready to throw out the array, but his mother reassured him. She told him, that array would not be spoiled after the following operation: choose any index i (1≤i≤n) and do ai:=−ai−1.
For example, he can change array [3,−1,−4,1] to an array [−4,−1,3,1] after applying this operation to elements with indices i=1 and i=3.
Kolya had immediately understood that sometimes it’s possible to increase the product of integers of the array a lot. Now he has decided that he wants to get an array with the maximal possible product of integers using only this operation with its elements (possibly zero, one or more times, as many as he wants), it is not forbidden to do this operation several times for the same index.
Help Kolya and print the array with the maximal possible product of elements a1⋅a2⋅…an which can be received using only this operation in some order.
If there are multiple answers, print any of them.
Input The first line contains integer n (1≤n≤105) — number of integers in the array.
The second line contains n integers a1,a2,…,an (−106≤ai≤106) — elements of the array
Output Print n numbers — elements of the array with the maximal possible product of elements which can be received using only this operation in some order from the given array.
If there are multiple answers, print any of them.
Examples inputCopy 4 2 2 2 2 outputCopy -3 -3 -3 -3 inputCopy 1 0 outputCopy 0 inputCopy 3 -3 -3 2 outputCopy -3 -3 2
思路:比赛的时候想的太麻烦,最后绝对想复杂了,就是一个贪心,先全部转成负数,因为负数绝对值更大嘛,然后如果序列长度是偶数,就可以,否则挑绝对值最大的数变成正数
#include<bits/stdc++.h>
#define rg register long long
#define inf 21474899993647
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
#define ll long long
#define maxn 100005
#define endl "n"
#define N 6000
const double eps=1e-8;
using namespace std;
inline ll read()
{
char ch=getchar();
ll s=0,w=1;
while(ch<48||ch>57)
{
if(ch=='-')
w=-1;
ch=getchar();
}
while(ch>=48&&ch<=57)
{
s=(s<<1)+(s<<3)+(ch^48);
ch=getchar();
}
return s*w;
}
inline void write(ll x)
{
if(x<0)
putchar('-'),x=-x;
if(x>9)
write(x/10);
putchar(x%10+48);
}
ll n=read(),a[maxn];
int main()
{
ll zhengmax=-inf,fumin=inf,zcnt=0,fcnt=0;
for(rg i=1;i<=n;i++)
{
a[i]=read();
if(a[i]>=0)a[i]=-a[i]-1;
}
if(n%2)
{
ll temp=0;
for(rg i=1;i<=n;i++)
{
if(fumin>a[i])
{
fumin=min(a[i],fumin);
temp=i;
}
}
a[temp]=-a[temp]-1;
}
for(rg i=1;i<=n;i++)
{
cout<<a[i]<<" ";
}
return 0;
}
- ActiveMQ笔记(4):搭建Broker集群(cluster)
- OOAD-设计模式(二)之GRASP模式与GOF设计模式概述
- 围棋之幸?围棋之悲?史上最年轻“五冠王”柯洁宣布再战AI
- spring 在Thread中注入@Resource失败,总为null的解决方案
- ActiveMQ笔记(3):基于Networks of Brokers的HA方案
- HTML5 Video Player概览
- 统计01:概述
- ActiveMQ笔记(2):基于ZooKeeper的HA方案
- ActiveMQ笔记(2):基于ZooKeeper的HA方案
- CSS几个竖直与水平居中盒子模型
- ZooKeeper 笔记(6) 分布式锁
- Hadoop(十五)MapReduce程序实例
- dubbox 的各种管理和监管
- bash魔法堂:History用法详解
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法