首页 > 试题广场 >

小苯送礼物

[编程题]小苯送礼物
  • 热度指数:5072 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
小苯是“小红书app”的一名博主,这天他想要给自己的“铁粉”送一些礼物。

他有 n 名粉丝,编号从 1n,但他只能选择其中 k 名送礼物,他决定选择其中对他支持力度最大的前 k 名粉丝。
(如果两名支持力度相同,则优先选择收藏数更多的,如果都一样,则优先选择编号更小的(因为这意味着他关注小苯的时间更早))

具体的:每名粉丝如果每给小苯点一次赞,则他对小苯就增加了 1 点支持力度,如果他每收藏小苯的一篇文章,则他对小苯增加 2 点支持力度。

现在小苯想知道,他应该选择哪 k 名粉丝送出礼物,请你帮帮他吧。

输入描述:
输入包含 n+1行。
第一行两个正整数 n, k\ (1 \leq k \leq n \leq 10^5),分别表示对小苯有过支持的粉丝个数,以及小苯选择送礼的粉丝个数。
接下来 n 行,每行两个整数 x_i, y_i\ (0 \leq x_i, y_i \leq 10^5),表示第 i 位粉丝给小苯点过 x 次赞,收藏过 y 个小苯的文章。


输出描述:
输出包含一行 k 个正整数,表示小苯选择出送礼物的粉丝们的编号。(按照升序输出)
示例1

输入

4 2
1 2
2 1
3 0
1 3

输出

1 4
import java.util.*;
import java.io.*;
import java.util.stream.Collectors;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String[] line1 = bf.readLine().split(" ");
        int n = Integer.parseInt(line1[0]), k = Integer.parseInt(line1[1]);
        ArrayList<Fan> list = new ArrayList<>();
        for (int i = 1; i <= n; i++) {
            String[] line = bf.readLine().split(" ");
            list.add(new Fan(i, Integer.parseInt(line[0]), Integer.parseInt(line[1])));
        }
        list.sort(new Comparator<Fan>() {
            @Override
            public int compare(Fan o1, Fan o2) {
                int i = o2.score - o1.score;
                i = i == 0 ? o2.col - o1.col : i;
                i = i == 0 ? o1.id - o2.id : i;
                return i;
            }
        });
        int[] ids = new int[k];
        for (int i = 0; i < k; i++) {
            ids[i] = list.get(i).id;
        }
        Arrays.sort(ids);
        System.out.println(Arrays.stream(ids).boxed().map(String::valueOf).collect(Collectors.joining(" ")));
    }
}
class Fan {
    int id;
    int zan;//点赞数
    int col;//收藏数
    int score;
    public Fan(int id, int zan, int col) {
        this.id = id;
        this.zan = zan;
        this.col = col;
        this.score = zan * 1 + col * 2;
    }
}

发表于 2025-09-21 18:01:41 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        PriorityQueue<fans> maxHeap = new PriorityQueue<>((a, b) -> a.support == b.support ? a.have == b.have ? a.num - b.num : b.have - a.have : b.support - a.support);
        int n = in.nextInt();
        int k = in.nextInt();
        for(int i = 0; i < n; i++){
            int good = in.nextInt();
            int have = in.nextInt();
            fans fan = new fans(i + 1, good, have);
            maxHeap.add(fan);
        }
        List<Integer> list = new ArrayList<>();
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < k; i++){
            list.add(maxHeap.poll().num);
        }
        Collections.sort(list);
        for(int i = 0; i < k; i++)
            sb.append(list.get(i)).append(" ");
        sb.deleteCharAt(sb.length() - 1);
        System.out.println(sb.toString());
    }
    static class fans{
        public int num;
        public int support;
        public int good;
        public int have;
        public fans(int num, int good, int have){
            this.num = num;
            this.good = good;
            this.have = have;
            this.support = good + 2 * have;
        }
    }
}
发表于 2025-09-04 23:37:23 回复(0)
import java.util.*;

public class Main {
    static class Fan {
        int support;  // 支持力度 = 点赞数 + 2*收藏数
        int collect;  // 收藏数,用于支持力度相同时的排序
        int id;       // 粉丝编号

        Fan(int support, int collect, int id) {
            this.support = support;
            this.collect = collect;
            this.id = id;
        }
    }

    public static void main(String[] args) {
        // 使用Scanner接收输入
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int k = scanner.nextInt();

        Fan[] fans = new Fan[n];
        for (int i = 0; i < n; i++) {
            int x = scanner.nextInt();  // 点赞数
            int y = scanner.nextInt();  // 收藏数
            // 计算支持力度并存储粉丝信息
            fans[i] = new Fan(x + 2 * y, y, i + 1);
        }

        // 自定义排序规则
        Arrays.sort(fans, (a, b) -> {
            // 首先按支持力度降序排列
            if (a.support != b.support) {
                return Integer.compare(b.support, a.support);
            }
            // 支持力度相同则按收藏数降序排列
            if (a.collect != b.collect) {
                return Integer.compare(b.collect, a.collect);
            }
            // 前两者都相同则按编号升序排列
            return Integer.compare(a.id, b.id);
        });

        // 提取前k名粉丝的编号
        int[] result = new int[k];
        for (int i = 0; i < k; i++) {
            result[i] = fans[i].id;
        }

        // 按编号升序排序结果
        Arrays.sort(result);

        // 输出结果
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < k; i++) {
            sb.append(result[i]);
            if (i < k - 1) {
                sb.append(" ");
            }
        }
        System.out.println(sb.toString());

        // 关闭Scanner
        scanner.close();
    }
}

发表于 2025-08-29 20:50:15 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        HashMap<Long, Long> map = new HashMap<>();
        while (in.hasNextLong()) {
            Long i = in.nextLong(); //支持粉丝数
            Long y = in.nextLong(); //送礼粉丝个数
            for (int j = 1; j <= i; j++) {
                map.put((long) j, in.nextLong() + in.nextLong() * 2);
            }
            //将entrySet放入到list集合里进行排序
            ArrayList<Map.Entry<Long, Long>> arrayList = new ArrayList<>
            (map.entrySet());
            //
            Collections.sort(arrayList, new Comparator<Map.Entry<Long, Long>>() {
                @Override
                public int compare(Map.Entry<Long, Long> o1,
                                   Map.Entry<Long, Long> o2) {
                    // Value 降序:e2 与 e1 比较
                    int valueCompare = o2.getValue().compareTo(o1.getValue());
                    if (valueCompare != 0) {
                        return valueCompare;
                    }
                    // Value 相同时,Key 升序
                    return o1.getKey().compareTo(Long.valueOf(o2.getKey()));
                }
            });
            //转换成linkedHashMap 保持顺序输出
            LinkedHashMap<Long, Long> hashMap = new LinkedHashMap<>();
            for (Map.Entry<Long, Long> entry : map.entrySet()) {
                hashMap.put(entry.getKey(), entry.getValue());
            }

            int count = 0;
            ArrayList<Long> list = new ArrayList<>();
            for (Map.Entry<Long, Long> entry : arrayList) {
                if (count < y) {
                    list.add(entry.getKey());
                    count++;
                } else {
                    break;
                }
            }
            Collections.sort(list);
            for (Long l : list) {
                System.out.print(l + " ");
            }
        }
    }
}



发表于 2025-08-14 10:55:15 回复(2)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    static class Funs{
        int like;
        int favorite;
        int support;
        int number;
        Funs(int like,int favorite,int number){
            this.like=like;
            this.favorite=favorite;
            this.support=like+2*favorite;
            this.number=number;
        };
    }

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int k=sc.nextInt();
        Funs[] funs=new Funs[n];
        PriorityQueue<Funs> pq=new PriorityQueue<Funs>(k,new Comparator<Funs>(){
            public int compare(Funs o1,Funs o2){
                // 1. 支持力度大的排前面
                if (o1.support != o2.support) {
                    return o1.support - o2.support; // 小在前,越小越劣,越容易被踢
                }
                // 2. 收藏数多的排前面
                if (o1.favorite != o2.favorite) {
                    return o1.favorite - o2.favorite; // 同理
                }
                // 3. 编号小的排前面(关注更早)
                return o2.number - o1.number; // 编号越大越劣(排在前面更容易被踢)

            }
        });
        for(int i=0;i<n;i++){
            int like=sc.nextInt();
            int favorite=sc.nextInt();
            funs[i]=new Funs(like,favorite,i+1);
            pq.add(funs[i]);
            if(pq.size()>k){
                pq.poll();
            }
        }

        List<Funs> funsList=new ArrayList<>(pq);
        funsList.sort((a,b)->a.number-b.number);
        StringBuilder sb=new StringBuilder();
        for(Funs f:funsList){
            sb.append(f.number+" ");
        }
        System.out.println(sb.toString());

    }
}
发表于 2025-05-17 17:59:10 回复(0)