因为在解决过程中没有看到比较简洁易懂的代码,所以在解决后发个博客给大家康康

利用公式ai = -ai -1处理数组,使数组积最大,我的思路是在输入时全部转成负值,同时找最小值,如果个数为奇数,转换最小值为正值,如果个数为偶数则不进行更多处理,最后输出

原链接:Codeforces - Problem Bhttps://codeforces.ml/contest/1180/problem/B

Nick had received an awesome array of integers a=[a1,a2,…,an]a=[a1,a2,…,an] as a gift for his 55 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⋅…ana1⋅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 ii (1≤i≤n1≤i≤n) and do ai:=−ai−1ai:=−ai−1.

For example, he can change array [3,−1,−4,1][3,−1,−4,1] to an array [−4,−1,3,1][−4,−1,3,1] after applying this operation to elements with indices i=1i=1 and i=3i=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⋅…ana1⋅a2⋅…an which can be received using only this operation in some order.If there are multiple answers, print any of them.

The first line contains integer nn (1≤n≤1051≤n≤105) — number of integers in the array.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (−106≤ai≤106−106≤ai≤106) — elements of the array

Output

Print nn 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.

样例:

  • in    4 2 2 2 2
  • out  -3 -3 -3 -3
  • in    1 0
  • out  0
  • in    3 -3 -3 2
  • out -3 -3 2
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <algorithm>
using namespace std;int num[1000000];int main()
{int i;int N, nmin;scanf("%d", &N);nmin = 0;for (i = 0; i < N; i++){scanf("%d", &num[i]);if (num[i] >= 0) num[i] = num[i] * (-1) - 1; // 全部转换为负数后 存到数组里if (num[nmin] > num[i]) nmin = i; // 找到最小数}if (N%2) num[nmin] = num[nmin] * (-1) - 1; // 如果个数是奇数,转换最小数为正数for (i = 0; i < N; i++)printf("%d ", num[i]);return 0;
}

B. Nick and Array 简单简洁相关推荐

  1. Android 6.0 动态权限申请简单简洁优雅的处理方式

    Android M 或以上的设备,在使用涉及隐私的安全权限时,需要动态申请,虽然不难,但写下来代码工作量也不少,而且几乎每个应用都需要用到.所以一直在探索一个要足够简单简洁优雅的处理方式,强迫症的精神 ...

  2. Codeforces G. Nick and Array(贪心)

    题目描述: Nick had received an awesome array of integers a=[a1,a2,-,an] as a gift for his 5 birthday fro ...

  3. html5网页代码模板简单,简洁蓝色风格HTML5网页模板

    简洁蓝色风格HTML5网页模板是一款适合健康医疗类网站模板下载 资源下载此资源下载价格为4D币,请先登录 资源文件列表 codedown123-0818-12/apage.html , 7582 co ...

  4. unity伤害数字显示,最简单简洁的方法

    想做一个伤害数字显示,结果发现搜索结果都太繁琐,我觉得这么简单的功能不应该这么麻烦啊! 所以做一个教程 在怪物身上创建一个画布,设置渲染模式为世界空间,然后重置,修改大小,高度高一点,因为text只有 ...

  5. 博客园美化(10)博客园 SimpleMemory 博客皮肤设置(简单简洁)

    之前调整了两个样式的,好看是好看,用着不方便,把不需要的全都删掉了 样式效果就是现在的 使用方法 一.开通博客园的JS权限 管理>设置>勾选JS权限,等待审核通过. 二.选择模板 JS权限 ...

  6. 一个简单简洁的社交媒体共享菜单

    效果预览 效果图如上, 也可以看在线的效果 live demo. 实现过程 1.  创建菜单像的html <div id="social_nav_horizontal"> ...

  7. B. Nick and Array(数学+贪心)Codeforces Round #569 (Div. 2)

    原题链接:https://codeforces.com/contest/1180/problem/B 题意:给定一个整数序列,你可以对任意元素进行nums[i]=-nums[i]-1的操作,但不能改变 ...

  8. 分享一个超级简单简洁的单页导航网站源码,不到1m,带valine评论设置

    影剧导航 示例网页:https://guide.qitablog.com/index.html 底部呢加个api就行 详细的可以看onemanager评论功能实现 onemanager美化详细图文教程 ...

  9. Python文件操作数据格式,简单简洁

    ✅作者简介:大家好,我是Philosophy7?让我们一起共同进步吧!

最新文章

  1. Codeforces 1009D:Relatively Prime Graph
  2. 百度首页html简单代码_百度站长平台为什么要验证网站及验证方法
  3. 【原】[webkit移动开发笔记]之如何去除android上a标签产生的边框
  4. excel实战应用案例100讲(六)-社会判断理论:模型及应用
  5. 记一次CurrentDirectory导致的问题
  6. 【OpenCV】OpenCV函数精讲之 -- 鼠标操作
  7. LeetCode-25:K 个一组翻转链表
  8. Linux fcntl函数详解
  9. C语言实现飞机订票系统
  10. 通用技术标模板,技术方案书
  11. Python在气象与海洋中的实践技术应用
  12. JAVA基础——对象与引用概念(转载)
  13. 适合后端开发人员的html笔记
  14. XILINX偶然加载不成功的问题原因及解决方法
  15. 云计算、物联网、大数据到底是什么?
  16. 什么是股票配资app?
  17. 什么是大数据?以及大数据的5V特性
  18. iview input blur事件
  19. python培训浦东
  20. java民生银行支付接口_民生付商户端开发手册和接口规范V1.0.0.doc

热门文章

  1. opencv专题:图像增强之拉普拉斯锐化
  2. 一场关于物理学本质的争论:实验是检验科学的唯一标准吗?
  3. 【统计学】【2014】双相情感障碍的数学建模、预测和远程监测
  4. 关于Fluent网格尺寸的疑惑
  5. 物联网的特点对行业的作用
  6. 记录javacv-platform截取rtsp图片和拍摄MP4
  7. 三大微分中值定理证明方法(罗尔定理、拉格朗日中值定理、柯西中值定理)
  8. 互联网大厂开启智慧养猪,网友:以后可能没机会养猪了
  9. 分享一个日记软件ediary
  10. Oracle实例与数据库的概念详细解释