Codeforces Round #569 (Div. 2) B. Nick and Array

原文链接:https://codeforces.com/contest/1180/problem/B

题意:给你n个数,你要进行一次或者多次操作(操作的方式a[i]=-a[i]-1),让它们的乘积最大,当然也可以不进行操作,答案有多组。
思路:根据(操作的方式a[i]=-a[i]-1),我们发现,正数进行操作后,绝对值变大了,那么想让全部数乘积最大,就让这组数全变成负数(是不是好奇,如果存在0的问题,0进行操作可以变为(-1)),然后判断个数n的奇偶,偶数就直接输出,奇数的话把最小(除去-1)的变为正数就行了.
代码:

 1 #include"iostream"
 2 #include"algorithm"
 3 #include"cstring"
 4 #include"cstdio"
 5 #include"cmath"
 6 using namespace std;
 7 #define ll long long
 8 int main(){
 9     std::ios::sync_with_stdio(false);
10     ll n;
11     while(cin>>n){
12      ll a[n],b[n];
13      for(int i=0;i<n;i++) {
14          cin>>a[i];
15          if(a[i]==0) a[i]=-1;
16          if(a[i]>0) a[i]=a[i]*(-1)-1;
17          b[i]=a[i];
18      }
19      sort(b,b+n);
20      if(n%2==0){
21          for(int i=0;i<n;i++) cout<<a[i]<<" ";
22          cout<<endl;
23      }
24      else{
25          int flag=0,tt=0;
26          for(int i=0;i<n;i++){
27              if(b[i]!=1){
28                  tt=b[i];
29                  break;
30              }
31          }
32         for(int i=0;i<n;i++){
33         if(a[i]==tt&&flag==0){
34            a[i]=a[i]*(-1)-1;
35             cout<<a[i]<<" ";
36             flag++;}
37         else{
38           cout<<a[i]<<" ";
39         }
40       }
41      }
42     cout<<endl;
43    }
44    return 0;
45 }

原文地址:https://www.cnblogs.com/huangdf/p/12222397.html

时间: 2024-10-06 00:01:57

Codeforces Round #569 (Div. 2) B. Nick and Array的相关文章

Codeforces Round #258 (Div. 2) B. Sort the Array(简单题)

题目链接:http://codeforces.com/contest/451/problem/B ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢迎光临天资小屋:http://user.qzone.qq.com/593830943/ma

Codeforces Round #510 (Div. 2) D. Petya and Array (权值线段树)

题目地址:http://codeforces.com/contest/1042/problem/D 题意:给你n个数,问有多少个区间的和的值小于t 分析:区间和问题,常常用到前缀和来进行预处理,所以先预处理出前缀和数组sum sum[i]代表前i个数的和,那么sum[i]的贡献就是,  当i<k<=n时,存在多少个k,使sum[k]<t+sum[i] 也就是求在[i+1,n]中,小于t+sum[i]的数有多少. 所以我们可以类比于询问一个数是区间第几大的方法,使用权值线段树来解决,这里因

Codeforces Round #258 (Div. 2/B)/Codeforces451B_Sort the Array

解题报告 http://blog.csdn.net/juncoder/article/details/38102391 对于给定的数组,取对数组中的一段进行翻转,问翻转后是否是递增有序的. 思路: 仅仅要找到最初递减的区域,记录区域内最大和最小的值,和区间位置. 然后把最大值与区间的下一个元素对照,最小值与区间上一个元素对照. 这样还不够,可能会出现两个或两个以上的递减区间,这样的情况直接pass,由于仅仅能翻转一次. #include <iostream> #include <cstd

Codeforces Round #374 (Div. 2) D. Maxim and Array

题解: 模拟题 虽然是个大模拟题,但是很考验思维的活跃度. 刚开始做的时候知道怎么做.但是自己的想法情况很多.分类枚举总是把自己混淆 这题教会我的是,要仔细思考分类的方式 这个题.根据枚举负数的个数奇偶来判断 为奇数:那么绝对值最小的数,如果是正,+x,是负,-x; 为偶数:那么使得绝对值最小的数.改变符号,如果正-x,负+x. 代码: #include<bits/stdc++.h> #define maxn 200010 #define mod 1000000007 #define ll l

Codeforces Round #258 (Div. 2) B. Sort the Array

Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of n distinct integers. Unfortunately, the size of a is too small. You want a bigger array! Your friends agree to give you a bigger array,

Codeforces Round #179 (Div. 2)---C. Greg and Array

Greg has an array a?=?a1,?a2,?-,?an and m operations. Each operation looks as: li, ri, di, (1?≤?li?≤?ri?≤?n). To apply operation i to the array means to increase all array elements with numbers li,?li?+?1,?-,?ri by value di. Greg wrote down k queries

Codeforces Round #617 (Div. 3) 题解

目录 Codeforces Round #617 (Div. 3) 题解 前言 A. Array with Odd Sum 题意 做法 程序 B. Food Buying 题意 做法 程序 C. Yet Another Walking Robot 题意 做法 程序 D. Fight with Monsters 题意 做法 程序 E1. String Coloring (easy version) 题意 做法 程序 E2. String Coloring (hard version) 题意 做法

Codeforces Round #245 (Div. 1)——Tricky Function

l and dished out an assist in the Blackhawks' 5-3 win over the Nashville Predators.Shaw said just playing with the Blackhawks was enough motivation for him."Positive, I'm playing in the NHL," Shaw said after Sunday's win. "What can't you be

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i