题目描述:

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=1and 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

input

4

2 2 2 2

output

-3 -3 -3 -3

input

1

0

output

0

input

3

-3 -3 2

output

-3 -3 2

思路:

题目是要通过给定的变换,让最后得到的数列的积最大。可以看到正数做了一次变换后绝对值就变大了,负数相反,而且做两次同样的变换相当于没做变换。把所有数都变味负数,如果这个时候元素的个数为偶数,把所有整数变到负数就ok了;如果是奇数个,就想一想该把哪一个负数变成正数乘积最大。

哪一个呢?首先-1肯定不能乱变,变了就是0。

刚开始以为是最大的那个数(绝对值最小除了-1外)变成正数,我就先排了个序,还用了什么二分lower_bound,upper_bound,又加个<type>()变成相反地意思,后来发现输出要按顺序,就改写成结构体形式,自己写了一个二分搞得很复杂。

最后,去的应该是是绝对值最大的元素。-_-||就不用费尽心思把-1挑出去了。

为什么呢?证明一下:假设0<a1<a2<...<an,a1*a2*...*an的积(忽略符号,a1,a2,...,看成是对应元素的绝对值),现在忽略负号的影响,也就是经过变换现在假设他们只要将一个数做变换成正,最后乘积就是正数的情况下,变a1为a1-1,和变an为an-1哪个大。

(a1-1)*a2*...*an-a1*a2*...*(an-1)=(a1-an)*(a2*...*a(n-1))<0,也就是变绝对值大的为正数就好了。

知识点:

1 template <class ForwardIterator, class T, class Compare>
2 ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last,
3                                const T& val, Compare comp);//自定义比较函数

lower_bound( begin,end,num,greater<type>() ):

从数组的begin位置到end-1位置二分查找第一个小于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。

对应的upper_bound同理(具体见参考文章)

代码:

 1 #include <iostream>
 2 #include <algorithm>
 3 #define max_n 100005
 4 using namespace std;
 5 struct node
 6 {
 7     int val;
 8     int ord;
 9 };
10 node a[max_n];
11 int n;
12 int cmp1(node a,node b)
13 {
14     return a.val<b.val;
15 }
16 int cmp2(node a,node b)
17 {
18     return a.ord<b.ord;
19 }
20 int bisearch(int num,int l,int r)
21 {
22     int mid = 0;
23     while(l<r)
24     {
25         mid = (l+r)>>1;
26         //cout << l << " " << r << endl;
27         if(a[mid].val<num)
28         {
29             l = mid+1;
30         }
31         else //if(a[mid].val>num)
32         {
33             r = mid;
34         }
35     }
36     return mid;
37 }
38 int main()
39 {
40     cin >> n;
41     for(int i = 0;i<n;i++)
42     {
43         cin >> a[i].val;
44         a[i].ord = i;
45         if(a[i].val>=0)
46         {
47             a[i].val = -a[i].val-1;
48         }
49     }
50     /*for(int i = 0;i<n;i++)
51     {
52         cout << a[i].val << " ";
53     }
54     cout << endl;*/
55     if(n%2!=0)
56     {
57         sort(a,a+n,cmp1);
58         a[0].val = -a[0].val-1;
59         sort(a,a+n,cmp2);
60         for(int i = 0;i<n;i++)
61         {
62             cout << a[i].val << " ";
63         }
64         cout << endl;
65     }
66     else
67     {
68
69         for(int i = 0;i<n;i++)
70         {
71             cout << a[i].val << " ";
72         }
73         cout << endl;
74     }
75     return 0;
76 }

参考文章:

讲解lower_bound(虽然这题好像用不上)

brandong,关于lower_bound( )和upper_bound( )的常见用法,https://blog.csdn.net/qq_40160605/article/details/80150252

Andywu_0010,lower_bound()函数和upper_bound()函数,以及二分查找,https://i.cnblogs.com/EditPosts.aspx?postid=11249693&update=1

转载于:https://www.cnblogs.com/zhanhonhao/p/11249693.html

Codeforces G. Nick and Array(贪心)相关推荐

  1. CodeForces 596B Wilbur and Array 贪心

    给出原始{ai}={0},{bi}.每次修改{ai..n}+1或-1,求最小操作次数使{ai}=={bi}. 累计相邻两数差即可. 因为差最大10^9,数字有2*10^5,要long long. #i ...

  2. codeforces G - Almost Increasing Array 动态规划、动态开点线段树

    题意 给出一个序列,允许删除一个元素,并将任意元素的值修改为任意整数,问最少修改多少个元素使得序列变成严格单调递增的序列? 题解 这道题目很具有启发性: 不考虑删除元素,原数列各个数值减去他们下标得到 ...

  3. codeforces Gym 100338E Numbers (贪心,实现)

    题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...

  4. CodeForces 1514A Perfectly Imperfect Array

    CodeForces 1514A Perfectly Imperfect Array 题意: 给你n个数,是否存在一个数不是平方数 题解: 先开方,转int,判断是否等于平方 代码: #include ...

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

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

  6. CodeForces - 1480D1 Painting the Array I(贪心)

    题目链接:点击查看 题目大意:给出一个长度为 nnn 的序列,现在要求拆分成两个子序列,使得两个子序列的贡献之和最 大.对于一个序列的贡献就是,去掉相邻且相同的字母后的长度,即 ∑i=1n[a[i]! ...

  7. Educational Codeforces Round 39 G Almost Increasing Array

    传送门 非减子序列 普通lcs Dp TLE ,换成upper_bound 竟然过了,神奇 #include <bits/stdc++.h> using namespace std; ty ...

  8. Codeforces 671E Organizing a Race (贪心、线段树)

    题目链接 https://codeforces.com/contest/671/problem/E 题解 完全不会做--基本是抄lk的代码 ruogu的题解: https://www.luogu.co ...

  9. CodeForces - 1480D2 Painting the Array II(dp)

    题目链接:点击查看 题目大意:给出一个长度为 nnn 的序列,现在要求拆分成两个子序列,使得两个子序列的贡献之和最 小.对于一个序列的贡献就是,去掉相邻且相同的字母后的长度,即 ∑i=1n[a[i]! ...

最新文章

  1. 干掉 Postman?测试接口直接生成API文档,这个文档工具真香!
  2. 机器学习的9个基础概念和10种基本算法总结
  3. 通过反射获取子类和父类定义的属性
  4. Java并发编程(五)JVM指令重排
  5. linux下jetty简单配置
  6. VS 2008 .NET Framework 3.5 Training Kit
  7. 我的世界java版gamemode指令_我的世界切换生存和创造模式的命令是什么?
  8. Hadoop安全实践
  9. 输出1/n(是循环小数的,只输出第一个循环节)
  10. centos7编译 openjdk8
  11. 服务器虚拟化底层系统安装,Hyper-V是底层的虚拟机程序,位于操作系统和硬件之间,很薄一层...
  12. C++ limits头文件的用法numeric_limits
  13. 轻量级ORM框架Dapper应用二:使用Dapper实现CURD操作
  14. tftp命令linux,tftp命令使用详解
  15. carrot 2 LingoClusteringAlgorithm , STCClusteringAlgorithm 和 BisectingKMeansClusteringAlgorithm算法比较
  16. 数据采集及采集工具八爪鱼的使用
  17. 硬盘整数分区大小计算公式(硬盘分区计算)
  18. python机器视觉车牌识别_车牌识别系统中的机器视觉技术
  19. 中国信通院发布《区块链赋能新型智慧城市白皮书(2019年)》解读(附全文下载)
  20. win10桌面无法新建文件夹解除管理员权限方法

热门文章

  1. 杰里之AC695N/AC696N 蓝牙耳机PCB LAYOUT 说明【篇】
  2. 你在迷茫吗?测试/开发工程师是干什么的?论正视自己,直面人生的重要性......
  3. 关于Eslint语法检查报“error Extra semicolon semi”错误的解决办法
  4. 《计算机科学》期刊投稿心得
  5. 支持向量机(Support Vector Machine)原理总结(二)
  6. java实现fcfs_JAVA从入门到精通之实现进程调度算法 FCFS
  7. 使用原生Js实现随机点名
  8. Learning Log:输入一个天数,计算天数包含多少周和剩余的天数
  9. 硅谷 AI 之王 Sam Altman : 如何通过创业取得成功 | How to Succeed with a Startup
  10. WSockExpert抓包工具