Hard deck/reindexing d1

034 : 수를 묶어서 최댓값 만들기

서버관리자 페페 2023. 6. 13. 16:17

Priority

// 5,  4,  3,  2,  1

// -1, -2, -3, -4, -5

 

Priority reversal

// 1 2 3 4 5

// -5. -4. -3. -2. -1

 

 

 

 

 

 

 

 

D2F는 container 공간이 먼저 필요

그다음 나눠서 delivery

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

1 이상 위상공간에서는 모두 곱셈작업이 일어난다

 

계산은 recursive bottom처럼 마지막에만 생각하면 됨

- 3이 남거나

- 2가 남거나

 

pq는 그냥 더해주면 되고, mq는 0이 있으면 곱해서 없앰

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

-

 

 

import java.util.Collections;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        // ISC
        Scanner sc = new Scanner(System.in);
        int V = sc.nextInt();

        // D2F
        PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
        PriorityQueue<Integer> mq = new PriorityQueue<>();
        int zeroPlate = 0;
        int onePlate = 0;

        for (int i = 1; i <= V; i++) {
            int p = sc.nextInt();
            if (p == 1) {
                onePlate++;
            } else if (p == 0) {
                zeroPlate++;
            } else if (p > 1) {
                pq.offer(p);
            } else {
                mq.offer(p);
            }
        }

        // OP
        int plate = onePlate;

        while (pq.size() > 1) {
            int d1 = pq.remove();
            int d2 = pq.remove();
            plate += (d1*d2);
        }
        if (!pq.isEmpty()) {
            plate += pq.remove();
        }

        while (mq.size() > 1) {
            int d1 = pq.remove();
            int d2 = pq.remove();
            plate += (d1*d2);
        }
        if (!mq.isEmpty()) {
            if (zeroPlate != 0) {
                return;
            }
        }


        // OEC
        System.out.println(plate);
    }
}

'Hard deck > reindexing d1' 카테고리의 다른 글

026 : DFS와 BFS 출력  (0) 2023.06.13
036 : 최솟값을 만드는 괄호 배치 찾기  (1) 2023.06.13
033 : 카드 정렬하기  (7) 2023.06.13
032 : 동전 개수의 최솟값 구하기  (2) 2023.06.13
043 : 최대 공약수 구하기  (1) 2023.06.13