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;
}
- Java中使用线程时,请不要忘记Spring TaskExecutor组件
- 常见.NET功能代码汇总
- 如何通过追踪代码自动发现网站之间的“关联”
- 教你如何和使用这款强大的渗透平台-Xerosploit
- No.014 Longest Common Prefix
- Java实现解析IP地址的方法,给出一串数字,生成正确的IP地址
- 图解“管道过滤器模式”应用实例:SOD框架的命令执行管道
- No.013 Roman to Integer
- OData – the best way to REST–实例讲解ASP.NET WebAPI OData (V4) Service & Client
- Java实现的手工做乘法方法,给出二个字符串数字,返回相乘结果
- No.012 Integer to Roman
- 黑帽SEO剖析之手法篇
- IOS学习1——IOS应用程序的生命周期及基本架构
- 浅议“全局变量”、“多线程”和“编译器陷阱”
- 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 数组属性和方法
- Puppeteer 入门与实战
- 【DB笔试面试854】在Oracle中,删除数据库的方式有哪几种?
- 方差齐性检验
- Windows WSL2 htop打开黑屏的问题解决
- 【DB笔试面试855】在Oracle中,简单说说PSU升级的过程
- HashMap中add()方法的源码学习
- IOC容器实现
- Docker安装mysql
- 【LeeCode 面试题】二叉树的前序遍历,中序遍历,后序遍历递归和迭代的两种实现方式
- MySQL笔记汇总
- 【DB笔试面试856】在Oracle中,如何判定实例是否运行?
- 【DB笔试面试857】在Oracle中,若一个主机上有多个Oracle实例,则如何确定哪些共享内存段属于想要清掉的实例的内存段?
- vue + flask实现邮件密码找回功能
- PicGo + Gitee 构建免费云图床
- python scipy.stats计算单样本假设检验(1 sample test)