Taking the Ruler: PIPI’s Goal Ⅲ

Taking the Ruler: PIPI’s Goal Ⅲ

question:

insert image description here
insert image description here

Ideas:

  First sort the array R. If you directly enumerate a, b, and c, the time complexity is O(n^3), and it will definitely time out. Then we will consider using the ruler method to solve this problem.
  We only enumerate a, and then let b=a+1, that is, b is the next digit of a, and c=n. The enumeration process is as follows:
  if R[a]+R[b]+R[c]==0 at this time, then the requirements are met, we output the result, and then make b++, c- -, and then continue to judge the sum.
  If R[a]+R[b]+R[c]<0 at this time, then obviously our value is small. Since we are enumerating a, a cannot be moved at this time, then only b or c can be moved, and only b can be moved to make it bigger, so let b++, and then continue to judge the sum.
  If R[a]+R[b]+R[c]>0 at this time, then obviously our value is too large. a cannot be moved, then only b or c can be moved, and only c can be moved to make it smaller, so let c- -, and then continue to judge the sum.
  When b is after c or b and c overlap, we have enumerated all the current cases of a, then a moves to the next bit, and continues the enumeration judgment process

  The title requires us to output non-repeating answers, so we define < a, b, c > as a triple TriGroup, and use a HashSet to deduplicate. It should be noted that we must rewrite the equals method of TriGroup, because our logic here is that when the values ​​of a, b, and c in the triplet are equal, the two TriGroups are considered to be the same, and the default equals method is It is realized by == judgment. The hashCode method must also be rewritten, because the add method of HashSet actually calls the put method of its built-in HashMap, which uses the hash value of the key, and the hash value is calculated using the hashCode method, so we Rewrite the hashCode method so that it returns the hash value determined jointly by a, b, and c

code:

import java.util.*;

public class Main {
    
    
    static int[] array = new int[1005];
    static Set<TriGroup> set = new HashSet<>();
    public static void main(String[] args) {
    
    
        int a, b, c, n, i, aIndex, bIndex, cIndex;
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextInt()) {
    
    
            set.clear();
            n = scanner.nextInt();
            for (i = 0; i < n; i++) {
    
    
                array[i] = scanner.nextInt();
            }
            Arrays.sort(array, 0, n);
            for (i = 0; i < n - 2; i++) {
    
    
                aIndex = i;
                bIndex = i + 1;
                cIndex = n - 1;
                a = array[aIndex];
                b = array[bIndex];
                c = array[cIndex];
                while (bIndex < cIndex) {
    
    
                    if (a + b + c < 0) {
    
    
                        bIndex++;
                        b = array[bIndex];
                    }
                    if (a + b + c > 0) {
    
    
                        cIndex--;
                        c = array[cIndex];
                    }
                    if (a + b + c == 0 && cIndex > bIndex) {
    
    
                        TriGroup triGroup = new TriGroup(a, b, c);
                        if (!set.contains(triGroup)) {
    
    
                            set.add(triGroup);
                            System.out.println(a + " " + b + " " + c);
                        }
                        bIndex++;
                        b = array[bIndex];
                        cIndex--;
                        c = array[cIndex];
                    }
                }
            }
        }
    }


}

class TriGroup {
    
    
    public int a;
    public int b;
    public int c;

    public TriGroup(int a, int b, int c) {
    
    
        this.a = a;
        this.b = b;
        this.c = c;
    }

    @Override
    public boolean equals(Object o) {
    
    
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        TriGroup triGroup = (TriGroup) o;
        return a == triGroup.a && b == triGroup.b && c == triGroup.c;
    }

    @Override
    public int hashCode() {
    
    
        return Objects.hash(a, b, c);
    }
}

Guess you like

Origin blog.csdn.net/qq_44709990/article/details/123202388