Codeforces G. Nick and Array(贪心)
题目描述:
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,也就是变绝对值大的为正数就好了。
知识点:
template <class ForwardIterator, class T, class Compare>
ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last,
const T& val, Compare comp);//自定义比较函数
lower_bound( begin,end,num,greater<type>() ):
从数组的begin位置到end-1位置二分查找第一个小于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
对应的upper_bound同理(具体见参考文章)
代码:
#include <iostream>
#include <algorithm>
#define max_n 100005
using namespace std;
struct node
{
int val;
int ord;
};
node a[max_n];
int n;
int cmp1(node a,node b)
{
return a.val<b.val;
}
int cmp2(node a,node b)
{
return a.ord<b.ord;
}
int bisearch(int num,int l,int r)
{
int mid = ;
while(l<r)
{
mid = (l+r)>>;
//cout << l << " " << r << endl;
if(a[mid].val<num)
{
l = mid+;
}
else //if(a[mid].val>num)
{
r = mid;
}
}
return mid;
}
int main()
{
cin >> n;
for(int i = ;i<n;i++)
{
cin >> a[i].val;
a[i].ord = i;
if(a[i].val>=)
{
a[i].val = -a[i].val-;
}
}
/*for(int i = 0;i<n;i++)
{
cout << a[i].val << " ";
}
cout << endl;*/
if(n%!=)
{
sort(a,a+n,cmp1);
a[].val = -a[].val-;
sort(a,a+n,cmp2);
for(int i = ;i<n;i++)
{
cout << a[i].val << " ";
}
cout << endl;
}
else
{ for(int i = ;i<n;i++)
{
cout << a[i].val << " ";
}
cout << endl;
}
return ;
}
参考文章:
讲解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
Codeforces G. Nick and Array(贪心)的更多相关文章
- Codeforces Round #569 (Div. 2) 题解A - Alex and a Rhombus+B - Nick and Array+C - Valeriy and Dequ+D - Tolik and His Uncle
A. Alex and a Rhombus time limit per test1 second memory limit per test256 megabytes inputstandard i ...
- Codeforces 442C Artem and Array(stack+贪婪)
题目连接:Codeforces 442C Artem and Array 题目大意:给出一个数组,每次删除一个数.删除一个数的得分为两边数的最小值,假设左右有一边不存在则算作0分. 问最大得分是多少. ...
- Codeforces Round #504 D. Array Restoration
Codeforces Round #504 D. Array Restoration 题目描述:有一个长度为\(n\)的序列\(a\),有\(q\)次操作,第\(i\)次选择一个区间,将区间里的数全部 ...
- codeforces Gym 100338E Numbers (贪心,实现)
题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...
- ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (思维,贪心)
ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (思维,贪心) Trace 问答问题反馈 只看题面 35.78% 1000ms 262144K There's a beach in t ...
- [Codeforces 1214A]Optimal Currency Exchange(贪心)
[Codeforces 1214A]Optimal Currency Exchange(贪心) 题面 题面较长,略 分析 这个A题稍微有点思维难度,比赛的时候被孙了一下 贪心的思路是,我们换面值越小的 ...
- Codeforces 754A Lesha and array splitting(简单贪心)
A. Lesha and array splitting time limit per test:2 seconds memory limit per test:256 megabytes input ...
- 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 #258 (Div. 2) . Sort the Array 贪心
B. Sort the Array 题目连接: http://codeforces.com/contest/451/problem/B Description Being a programmer, ...
随机推荐
- 开启和安装Kubernetes k8s 基于Docker For Windows
0.最近发现,Docker For Windows Stable在Enable Kubernetes这个问题上是有Bug的,建议切换到Edge版本,并且采用下文AliyunContainerServi ...
- linux减少服务器带宽的方法
linux减少服务器带宽的方法用百度静态资源公共库http://cdn.code.baidu.com/ 不仅可以不使用服务器流量 而且还有cdn加速比方说http://apps.bdimg.com/l ...
- 搭建mqtt服务器apollo
使用的apollo,官网太慢,附上百度云下载地址: 链接:https://pan.baidu.com/s/1NIq6R71hlyPuaUBwPoMPNg 提取码:36vw 原文链接:https://b ...
- C++ 智能指针 boost::scoped_ptr分析
1.scoped_ptr的实现原理及特性 特性:scoped_ptr和auto_ptr类似,但最大的区别就是不能转让管理权限,也就是说scoped_ptr禁止用户进行拷贝和赋值 实现原理:如何才能禁止 ...
- Spring中WebMvcConfigurer用到的JDK8特性
闲来无聊,随便翻看项目,发现WebMvcConfigurerAdapter已经过时了,它的作用也不用说了,就是起到适配器的作用,让实现类不用实现所有方法,可以根据实际需要去实现需要的方法. @Depr ...
- jmeter jtl 文件
一.获取.jtl文件 使用非 GUI 模式,即命令行模式运行 JMeter .执行完成jmeter后,会生成jtl文件. 1.1. 命令介绍 1)先cmd进入到jmeter的bin文件目录下(这里是 ...
- 最新版Prometheus+Grafana+node-exporter炫酷界面
一.概述 理论知识就不多介绍了,参考链接: https://www.cnblogs.com/xiao987334176/p/9930517.html 本文使用2台服务器,来搭建. 环境 操作系统 do ...
- MySQl数据库面试题
1. MySQL中索引什么作用? 索引的定义和创建的目的 1) 索引是对数据库表中一列或者多列的值进行排序的一种结构,使用索引可快速访问数据库表中的特定信息 2) 索引的分类:主键索引,唯一索引,常规 ...
- docker容器的使用整理
2019/10/24, docker 19.03.4 摘要:docker容器常用命令整理 gitbooks文档 docker脚本安装 使用官方脚本安装docker,从阿里云下载: curl -fsSL ...
- MPAndroid 的学习
1.MPAndroid 的github的地址: https://github.com/PhilJay/MPAndroidChart#documentation 2.使用步骤: 在build.gradl ...