Submission #76484509


Source Code Expand

import java.util.*;

class Main {
    static class Gem {
        int id;
        int color;
        long value;

        Gem(int id, int color, long value) {
            this.id = id;
            this.color = color;
            this.value = value;
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        if (!sc.hasNextInt()) return;
        
        int n = sc.nextInt();
        int k = sc.nextInt();
        int m = sc.nextInt();

        List<Gem> allGems = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            int color = sc.nextInt();
            long val = sc.nextLong();
            allGems.add(new Gem(i, color, val));
        }

        //  Sort all gems globally by value in descending order
        allGems.sort((g1, g2) -> Long.compare(g2.value, g1.value));

        // Track how many times each color appears in our current selection
        Map<Integer, Integer> selectedColorCounts = new HashMap<>();
        
        // Min-Heap to easily find the lowest-value "duplicate" gem in our hand
        PriorityQueue<Gem> duplicatesInHand = new PriorityQueue<>((g1, g2) -> Long.compare(g1.value, g2.value));

        long currentSum = 0;

        // Greedily pick the top K gems
        for (int i = 0; i < k; i++) {
            Gem gem = allGems.get(i);
            currentSum += gem.value;
            selectedColorCounts.put(gem.color, selectedColorCounts.getOrDefault(gem.color, 0) + 1);
        }

        //  Categorize the chosen K gems. 
        // If a gem's color appears more than once, it's a potential duplicate we can drop!
        for (int i = 0; i < k; i++) {
            Gem gem = allGems.get(i);
            if (selectedColorCounts.get(gem.color) > 1) {
                duplicatesInHand.add(gem);
            }
        }

        //  Find the best available gems outside our hand for the missing colors
        // Map to store only the HIGHEST value gem for each color that we haven't picked yet
        Map<Integer, Gem> bestMissingGems = new HashMap<>();
        for (int i = k; i < n; i++) {
            Gem gem = allGems.get(i);
            // If we don't have this color in our hand at all
            if (!selectedColorCounts.containsKey(gem.color)) {
                // Since allGems is sorted by value, the first time we see a missing color, 
                // it is guaranteed to be the highest value gem for that color.
                if (!bestMissingGems.containsKey(gem.color)) {
                    bestMissingGems.put(gem.color, gem);
                }
            }
        }

        // Max-Heap to easily find the highest-value missing color gem available
        PriorityQueue<Gem> availableNewColors = new PriorityQueue<>((g1, g2) -> Long.compare(g2.value, g1.value));
        availableNewColors.addAll(bestMissingGems.values());

        // Forcefully swap if our distinct color count is less than M
        int currentDistinctColors = selectedColorCounts.size();

        while (currentDistinctColors < m) {
            // If we run out of duplicates to sacrifice or new colors to bring in,
            // then it's impossible to fix (though constraints say a choice is always possible)
            if (duplicatesInHand.isEmpty() || availableNewColors.isEmpty()) {
                break;
            }

            Gem outGem = duplicatesInHand.poll();
            
            // Check if this gem is still actually a duplicate. 
            // (As we remove gems, a color's count drops. If it hits 1, it's no longer a duplicate!)
            if (selectedColorCounts.get(outGem.color) <= 1) {
                continue; 
            }

            Gem inGem = availableNewColors.poll();

            // Perform the swap
            currentSum -= outGem.value;
            currentSum += inGem.value;

            selectedColorCounts.put(outGem.color, selectedColorCounts.get(outGem.color) - 1);
            selectedColorCounts.put(inGem.color, 1);

            currentDistinctColors++;
        }

        System.out.println(currentSum);
    }
}

Submission Info

Submission Time
Task C - Variety
User renu_
Language Java24 (OpenJDK 24.0.2)
Score 300
Code Size 4178 Byte
Status AC
Exec Time 966 ms
Memory 83960 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 300 / 300
Status
AC × 3
AC × 23
Set Name Test Cases
Sample sample-01.txt, sample-02.txt, sample-03.txt
All 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, sample-01.txt, sample-02.txt, sample-03.txt
Case Name Status Exec Time Memory
04.txt AC 74 ms 40460 KiB
05.txt AC 564 ms 76552 KiB
06.txt AC 570 ms 80484 KiB
07.txt AC 665 ms 83960 KiB
08.txt AC 71 ms 40228 KiB
09.txt AC 104 ms 42828 KiB
10.txt AC 144 ms 48088 KiB
11.txt AC 478 ms 70928 KiB
12.txt AC 804 ms 83228 KiB
13.txt AC 819 ms 80096 KiB
14.txt AC 903 ms 80200 KiB
15.txt AC 819 ms 78656 KiB
16.txt AC 771 ms 77072 KiB
17.txt AC 847 ms 77912 KiB
18.txt AC 932 ms 80480 KiB
19.txt AC 955 ms 82492 KiB
20.txt AC 869 ms 80192 KiB
21.txt AC 838 ms 79128 KiB
22.txt AC 522 ms 83456 KiB
23.txt AC 966 ms 79612 KiB
sample-01.txt AC 70 ms 40212 KiB
sample-02.txt AC 69 ms 40608 KiB
sample-03.txt AC 66 ms 40112 KiB