[Blue Bridge Cup] [2019 Tenth Zhenti] Suffix expression (detail!)

Suffix expression is also called inverse Polish, here is a data structure problem
Insert picture description here

Ideas

The error-prone point is that the maximum value is the order of the numbers, the larger numbers are added, and the smaller numbers are subtracted. This is to treat the minus sign as a minus.

Right should be: because they can add and subtract numbers, so has the role of minus can affect change number of other symbols and subtract . This is the core idea
analysis:

  • In fact, suffix expressions can be added in parentheses. Putting a negative sign in front of a minus sign enclosed in parentheses can make all the minus signs in the brackets become positive signs.
  • The above rule shows that we can arbitrarily adjust the negative sign to the positive sign
  • To sum up: m minus signs, the largest minus m times (unchanged sign), the smallest minus 1 times (the remaining m-1 times the sign changes), so you can choose any number of times from 1 to m. That is to say, as long as the number of minus signs is greater than 0, it must be subtracted at least once, and at most all minus signs can be subtracted.
  • Then take the number of negative numbers to find the relationship with the number of minus signs, n + m + 1 number, m is the number of minus signs, cnt is the number of minus signs:
    1. No minus sign, the simplest all add up That ’s it.
    2. When there are minus signs and all negative numbers, because the maximum can only change the sign of m-1 numbers, so keep a maximum negative number for addition (the absolute value is the smallest), and let the remaining negative numbers change to
    3 . When there is a minus sign, and not all negative numbers, because the maximum can only change the sign of m-1 numbers, so you can change all negative numbers
    4. There is a minus sign, and when there is no negative number, the less the minus sign, the better Therefore, since it must be subtracted at least once, leave the chance of subtraction to the smallest integer.

Code

import java.util.*;
public class Main {
    static Scanner in = new Scanner(System.in);
    static int n, m;
    static int[] a;
//    static int result;

    public static void main(String[] args) {
        n = in.nextInt();
        m = in.nextInt();
        int bound=m+n+1;
        int cnt=0;
        long sum=0;
        a=new int[bound];

        for (int i = 0; i <bound ; ++i) {
           a[i] = in.nextInt();
           sum+=a[i];
           if(a[i]<0) cnt++;      //计算负数个数
        }
        Arrays.sort(a);

//        不含负号
        if(m==0){
           System.out.println(sum);
           return;
        }
//        含有负号
        if(cnt!=0){    //有负数
            if(cnt!=bound) for (int i=0;i<cnt;i++) sum-=2*a[i];
            else    for (int i=0;i<cnt-1;i++) sum-=2*a[i];
        }
        else sum-=2*a[0];    //无负数

        System.out.println(sum);
    }
}

Published 24 original articles · won 10 · visited 5101

Guess you like

Origin blog.csdn.net/Fzidx/article/details/105632241