小红结账(美团2024届秋招笔试第三场编程真题)


核心思想

模拟就完了

代码

import java.util.*;

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

        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        Long[] res = new Long[m + 1];
        Arrays.fill(res, 0L);
        LinkedList<Integer> q= new LinkedList<>();
        for(int i = 1; i <= n; i++){
            int k = scanner.nextInt();
            int c = scanner.nextInt();
            for(int j = 1; j <= k - 1; j++){
                q.add(scanner.nextInt());
            }
            int x = (int) Math.ceil(1.0 * c / k);
            while (!q.isEmpty()){
                int cur = q.pollFirst();
                res[cur] += x;
            }
        }

        for(int i = 1; i <= m; i++){
            if(i == 1)
                System.out.print(res[i]);
            else
                System.out.print(" "+res[i]);
        }
    }
}
posted @ 2024-04-02 10:27  Shie1d  阅读(33)  评论(0)    收藏  举报