首页 > 试题广场 >

小红的精选笔记

[编程题]小红的精选笔记
  • 热度指数:410 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
小红在小红书上面发布了n篇笔记,其中第i篇笔记的点赞数量为a_i,评论数为b_i。现在小红准备选择k篇笔记作为“精选笔记合集”,合集的优秀程度为:所有笔记点赞数之和乘以评论数的最小值。
现在小红想知道,最终合集最大的优秀度是多少?

输入描述:
第一行输入两个正整数n,k,代表笔记的数量,以及小红准备选择的合集大小。
第二行输入n个正整数a_i,代表每篇笔记的点赞数。
第三行输入n个正整数b_i,代表每篇笔记的评论数。
1\leq n,a_i,b_i \leq 10^5
1\leq k \leq n


输出描述:
一个正整数,代表最终最大的优秀度。
示例1

输入

4 2
1 2 3 4
3 4 2 1

输出

10

说明

选第二篇和第三篇即可。
//排序,优先队列维护最大点赞数
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int k = in.nextInt();
        List<Integer> a = new ArrayList<>();
        List<Integer> b = new ArrayList<>();
        for(int i = 0; i < n; i++) {
            a.add(in.nextInt());
        }
        for(int i = 0; i < n; i++) {
            b.add(in.nextInt());
        }

        List<int[]> list = new ArrayList<>();
        for(int i = 0; i < n; i++) {
            list.add(new int[]{a.get(i), b.get(i)});
        }

        Collections.sort(list, (o1, o2) -> {
            if(o1[1] != o2[1]) {
                return Integer.compare(o2[1], o1[1]);
            }else {
                return Integer.compare(o2[0], o1[0]);
            }
        });
        PriorityQueue<Integer> heap = new PriorityQueue<>();
        long sum = 0; 
        long max = 0;
        for(int[] arr : list) {
            int ai = arr[0];
            int bi = arr[1];
            heap.add(ai);
            sum += ai;
            if(heap.size() > k) {
                int remove = heap.poll();
                sum -= remove;
            }
            if(heap.size() == k) {
                long current = sum * bi;
                if(current > max) {
                    max = current;
                }
            }
        }
        System.out.print(max);
    }
}

发表于 2025-03-27 16:06:30 回复(0)
import java.util.*;

public class Main {

    static class Note {
        int like;
        int comment;
        public Note(int like, int comment) {
            this.like = like;
            this.comment = comment;
        }
    }

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

        int[] likes = new int[n];
        int[] comments = new int[n];

        for (int i = 0; i < n; i++) likes[i] = sc.nextInt();
        for (int i = 0; i < n; i++) comments[i] = sc.nextInt();

        // 把笔记封装进 Note 类中
        List<Note> notes = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            notes.add(new Note(likes[i], comments[i]));
        }

        // 按评论数从大到小排序(从可能的最大 min(b) 枚举)
        notes.sort((a, b) -> b.comment - a.comment);

        // 小顶堆维护点赞最多的 k 篇笔记
        PriorityQueue<Integer> minHeap = new PriorityQueue<>();
        long sum = 0;
        long maxScore = 0;

        for (Note note : notes) {
            minHeap.offer(note.like);
            sum += note.like;

            if (minHeap.size() > k) {
                sum -= minHeap.poll(); // 只保留点赞数最大的前 k 篇
            }

            if (minHeap.size() == k) {
                maxScore = Math.max(maxScore, sum * note.comment);
            }
        }

        System.out.println(maxScore);
    }
}

发表于 2025-05-17 18:00:02 回复(0)