CF1038D Slime

Luo Gu CF1038D Slime

Topic Portal

Translation of the meaning of problems

Subject to the effect:

There are the n- the n- only slime qwq, each a fraction of slime, slime can swallow up every time a slime left or the right (if they had them), then the score will be minus ta how much is swallowed scores of slime, slime asked last remaining fraction up to

Input formats:

A first line integer n- n-

The second line n- n- integers, fractions showing Slime

Output formats:

An integer that is the maximum score

### 题目大意:
有$n$只史莱姆qwq,每只史莱姆有一个分数,每次一只史莱姆可以吞掉左边的或者右边的史莱姆(要是有的话),然后ta的分数会减去被吞的史莱姆的分数,问最后剩下的史莱姆分数最大为多少

### 输入格式:
第一行一个整数$n$

第二行$n$个整数,表示史莱姆的分数

### 输出格式:
一个整数,即最大分数

Title Description

There are nn slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it.

Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists).

When a slime with a value xx eats a slime with a value yy , the eaten slime disappears, and the value of the remaining slime changes to x - yxy .

The slimes will eat each other until there is only one slime left.

Find the maximum possible value of the last slime.

Input Format

The first line of the input contains an integer nn ( 1 \le n \le 500,0001≤n≤500000 ) denoting the number of slimes.

The next line contains nn integers a_i*a**i* ( -10^9 \le a_i \le 10^9−109≤ai≤109 ), where a_iai is the value of ii -th slime.

Output Format

Print an only integer — the maximum possible value of the last slime.

Sample input and output

Input # 1 copy

Output # 1 copy

Input # 2 Copy

Output # 2 Copy

Description / Tips

In the first example, a possible way of getting the last slime with value 44 is:

  • Second slime eats the third slime, the row now contains slimes 2, -1, 12,−1,1
  • Second slime eats the third slime, the row now contains slimes 2, -22,−2
  • First slime eats the second slime, the row now contains 44

In the second example, the first slime can keep eating slimes to its right to end up with a value of 44 .

Subject to the effect:

A number of columns, you can subtract the number of adjacent, asked that the final number could be reduced to the maximum after the last number is how much.

answer:

This is a mathematical derivation of the problem:

First, we discuss the classification: This problem There are three major conditions:

All positive, all negative, positive numbers are also negative.

Let's look:

If there are both positive and negative, then we can subtract negative numbers positive numbers, let the smaller the cut, but larger in absolute value, and finally by subtracting a positive number, you can get the maximum value, so although we have been to reduce minus, but the absolute value has not changed, the final output is the answer "no loss", so if there are both positive and negative , should be the final answer to all the absolute number and .

If the whole is positive, then we consider by just reasoning, it must produce negative, positive minus negative finally get a bigger positive number, so we subtract as much as possible with a little big. Similarly, if the whole is negative, it is with a large minus small. So, if the whole positive whole negative , it should be the final answer to all the absolute numbers and subtract all the numbers in the absolute value of the minimum number twice .

Note that the code :( Laid determination of n = 1)

#include<cstdio>
#include<cmath>
#include<algorithm>
#define ll long long
using namespace std;
int n;
int pos,neg;
ll sum,minn=1e9;
int main()
{
    scanf("%d",&n);
    if(n==1)
    {
        ll a;
        scanf("%lld",&a);
        printf("%lld",a);
        return 0;
    }
    for(int i=1;i<=n;i++)
    {
        ll a;
        scanf("%lld",&a);
        if(a>0)
            pos++;
        if(a<0)
            neg++;
        sum+=abs(a);
        minn=min(minn,abs(a));
    }
    if(pos>0 && neg>0)
        printf("%lld",sum);
    else
        printf("%lld",sum-minn*2);
    return 0;
}

Guess you like

Origin www.cnblogs.com/fusiwei/p/11353090.html