题解 | 小苯送礼物

小苯送礼物

https://www.nowcoder.com/practice/466e02d2177845589ab5fa5decc2857f

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextInt()) {
            int n = sc.nextInt();
            int k = sc.nextInt();
            int[][] data = new int[n][3];
            
            for (int i = 0; i < n; i++) {
                int c = sc.nextInt();
                int d = sc.nextInt();
                data[i][0] = c + 2 * d;  // 支持力度
                data[i][1] = d;          // 收藏数
                data[i][2] = i + 1;      // 编号
            }
            
            Arrays.sort(data, (a, b) -> {
                if (a[0] != b[0]) return b[0] - a[0];
                if (a[1] != b[1]) return b[1] - a[1];
                return a[2] - b[2];
            });
            
            int[] result = new int[k];
            for (int i = 0; i < k; i++) {
                result[i] = data[i][2];
            }
            Arrays.sort(result);
            
            for (int num : result) {
                System.out.print(num + " ");
            }
            System.out.println();
        }
    }
}

有可能运行超时,多试两次

全部评论

相关推荐

12-03 03:32
安徽大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务