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, ...
随机推荐
- Spring Shell入门介绍
目录 Spring Shell是什么 入门实践 基础配置 简单示例 注解@ShellMethod 注解@ShellOption 自定义参数名称 设置参数默认值 为一个参数传递多个值 对布尔参数的特殊处 ...
- js:对象之间的复制
1.:复制obj1,不管obj2是否有这个属性,但是ojb2中的特有属性会保留 var obj1={id:1,name:'zhangsan'} var obj2={}; for (var prop i ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- Dubbo_异常_服务注册运行正常但是Dubbo-Admin看不到服务
出自:https://www.cnblogs.com/gossip/p/6021698.html 一.背景: 1.Dubbo服务正常注册到ZooKeeper 2.客户端调用Dubbo服务正常 二.原因 ...
- Spring Cloud 微服务技术整合
微服务架构风格是一种使用一套小服务来开发单个应用的方式途径,每个服务运行在自己的进程中,并使用轻量级机制通信,通常是HTTP API,这些服务基于业务能力构建,并能够通过自动化部署机制来独立部署,这些 ...
- 递归下降和LL(1)语法分析
什么是自顶向下分析法 在语法分析过程中一般有两种语法分析方法,自顶向下和自底向上,递归下降分析和LL(1)都属于是自顶向下的语法分析 自顶向下分析法的过程就像从第一个非终结符作为根节点开始根据产生式进 ...
- Dockerfile语法梳理
Dockerfile语法 我们先来看一下上篇的 Dockerfile #获取base image FROM adoptopenjdk/openjdk8:latest #类似于执行 linux指令 RU ...
- 在ensp上的动态NAT的配置
原理 实验模拟 搭建实验拓扑 相关参数 配置静态NAT ,一对一映射 首先设置静态路由,使路由器能够访问 我们ping一下抓一下包 发现我们出去的包已经封装成为了另外一个ip 配置动态NAT ,一对一 ...
- Java课堂笔记1
1. Java严格区分大小写 2. 一个源文件public主类名必须和文件名完全一致 3. 命名规则严格要求,字母.数字.下划线.美元符号$.下划线_组成,其中不能以数字开头,也不能使用Java的 ...
- 网站登录注册-Session 和token的总结
1.为什么要使用session 因为http本身是无状态协议,无法确定你的本次请求和上次请求是不是你发送的.如果要进行类似论坛登陆相关的操作,就实现不了了. 2.Session 生成方式 浏览器第一次 ...