当前位置:网站首页>Maximized array sum after 13 K negations
Maximized array sum after 13 K negations
2022-07-27 11:09:00 【DHU yanghualin (UV allergy)】
13 K The maximum array and... After negation
author : Turbo The time limit : 1S chapter : curriculum design
Problem description :
Give you an array of integers nums And an integer k , Modify the array as follows :
Select a subscript i And will nums[i] Replace with -nums[i] .
Repeating this process happens to k Time . You can select the same subscript multiple times i .
After modifying the array in this way , Returns an array of The maximum possible and .
Example 1:
Input :
3
4 2 3
1
Output :5
explain : Select subscript 1 ,nums Turn into [4,-2,3] .
Example 2:
Input :
4
3 -1 0 2
3
Output :6
explain : Select subscript (1, 2, 2) ,nums Turn into [3,1,0,2] .
Example 3:
Input :
5
2 -3 -1 5 -4
2
Output :13
explain : Select subscript (1, 4) ,nums Turn into [2,3,-1,5,4] .
Enter description :
Enter three lines :
Enter an integer in the first line n Represents an array nums The length of .
Second line input n Integers represent the elements of the array .
On the third line, enter an integer representation k.
Tips :
1 <= n <= 10^4
-100 <= nums[i] <= 100
1 <= k <= 10^4
The output shows that :
Output an integer to represent the result .
Input example :
4
1 2 3 4
3
Output example :
8、
Classification seems a little complicated But the answer is correct I have to upload it quickly, students It's too late to explain It's almost over
#include<iostream>
#include<algorithm>
using namespace std;
int negative_number = 0;
int zero_nuumber = 0;
bool cmp(int x, int y)
{
return x < y;
}
int main()
{
int arr[100001] = { 0 };
int n = 0;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> arr[i];
if (arr[i] < 0)
{
negative_number++;
}
else if (arr[i] == 0)
{
zero_nuumber++;
}
}
int k = 0;
cin >> k;
if (negative_number > 0)// There are negative numbers
{
int total = 0;
sort(arr, arr + n, cmp);
if (k > n)
{
if (k % 2 == 0)
{
for (int i = 0; i < n; i++)
{
if (arr[i] < 0)
{
arr[i] = -arr[i];
}
}
for (int i = 0; i < n; i++)
{
total += arr[i];
}
}
else if (k % 2 != 0)
{
if (zero_nuumber > 0)
{
for (int i = 0; i < k; i++)
{
arr[i] = -arr[i];
if (arr[i] == 0)
{
break;
}
}
for (int i = 0; i < n; i++)
{
total += arr[i];
}
}
else if (zero_nuumber == 0)
{
for (int i = 0; i < n; i++)
{
if (arr[i] < 0)
{
arr[i] = -arr[i];
}
}
sort(arr, arr + n, cmp);
arr[0] = -arr[0];
for (int i = 0; i < n; i++)
{
total += arr[i];
}
}
}
cout << total;
}
else if (k <= n)
{
if (k < negative_number)
{
for (int i = 0; i < k; i++)
{
arr[i] = -arr[i];
}
for (int i = 0; i < n; i++)
{
total += arr[i];
}
}
else if (k > negative_number)
{
if (zero_nuumber > 0)
{
for (int i = 0; i < k; i++)
{
if (arr[i] < 0)
{
arr[i] = -arr[i];
}
else if (arr[i] == 0)
{
break;
}
}
for (int i = 0; i < n; i++)
{
total += arr[i];
}
}
else if (zero_nuumber == 0)
{
for (int i = 0; i < k; i++)
{
if (arr[i] < 0)
{
arr[i] = -arr[i];
}
}
for (int i = 0; i < n; i++)
{
total += arr[i];
}
}
}
cout << total;
}
}
else if (negative_number == 0)// All numbers are positive
{
if (k % 2 == 0)
{
int total = 0;
for (int i = 0; i < n; i++)
{
total += arr[i];
}
cout << total;
}
else if (k % 2 != 0)
{
int total = 0;
sort(arr, arr + n, cmp);
arr[0] = -arr[0];
for (int i = 0; i < n; i++)
{
total += arr[i];
}
cout << total;
}
}
return 0;
}边栏推荐
- Ten year structure five year life-07 young and vigorous transformation
- What is the issuing price of NFT (Interpretation of NFT and establishment of NFT world outlook)
- Pyqt5 rapid development and practice 4.2 QWidget
- SQL Server2000 database error
- IMG SRC is empty or SRC does not exist, and the picture has a white border
- SQL Server2000数据库错误
- pyquery 的使用
- Internal and external troubles of digital collection NFT "boring ape" bayc
- C language 2: find the maximum value of three numbers, find the middle value of three numbers, and write program steps
- Using skills of word
猜你喜欢

One stop monitoring of the software and hardware infrastructure of the whole university, and Suzhou University replaces PostgreSQL with time series database

解决 ImportError: cannot import name 'abs' 导入tensorflow报错

NFT leaderboard -nft real offer latest address: NFT leaderboard.com

Tcp/ip protocol

Where is the big data open source project, one-stop fully automated full life cycle operation and maintenance steward Chengying (background)?

MySQL installation (RPM package)

ECCV 2022 | complete four tracking tasks at the same time! Unicorn: towards the unification of target tracking

Wenzhou University X kangaroo cloud: how to "know well" in the construction of higher talent education

黑白像素分布对迭代次数的影响

Derivation of the detailed expansion sto overlap integrals
随机推荐
Self optimization of wireless cell load balancing based on machine learning technology
IO流_数据输入输出流的概述和讲解
Where is the big data open source project, one-stop fully automated full life cycle operation and maintenance steward Chengying (background)?
Views, triggers and stored procedures in MySQL
Redis+caffeine two-level cache enables smooth access speed
Internal and external troubles of digital collection NFT "boring ape" bayc
WebRTC实现简单音视频通话功能
已解决SyntaxError: (unicode error) ‘unicodeescape‘ codec can‘t decode bytes in position 2-3: truncated
ECCV 2022 | complete four tracking tasks at the same time! Unicorn: towards the unification of target tracking
Learning notes uni app
对象数组去重
Learning notes - simple server implementation
The difference of iteration number and information entropy
Open source project - taier1.2 release, new workflow, tenant binding simplification and other functions
学习笔记-微信支付
IO流_字符流、IO流小结、IO流案例总结
Wilderness search --- search iterations
学习笔记-uni-app
Sort th in antd table to prevent hovering color change +table hovering row color change +table header color change
Cancer DDD