首页 > 试题广场 >

小苯的比赛上分

[编程题]小苯的比赛上分
  • 热度指数:2697 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
\hspace{15pt}有一款著名的大型多人电子竞技游戏网站“喜爱福”,通常会举办一些比赛。选手通常只有一个账号,但一些人会“开小号”以提高最高分数。

\hspace{15pt}小苯是一名忠实玩家,他拥有 n 个账号,每个账号当前的分数为 a_i

\hspace{15pt}st****lk 的名言是:“只要你永远使用分数最低的账号参赛,那么你的 \max \mathrm{Rating} 将单调不降。”这里的 \max \mathrm{Rating} 指玩家所有账号中最高分的值。

\hspace{15pt}已知小苯会牢记此名言,并且在记录的 m 场比赛中,每次都使用当前分数最低的账号参赛。假设第 j 场比赛会让该账号分数增加 b_j,请你计算每场比赛结束后,小苯的 \max \mathrm{Rating}

输入描述:
\hspace{15pt}第一行输入两个正整数 n, m\left(1 \leqq n, m \leqq 10^5\right),分别表示账号数量和比赛场次。

\hspace{15pt}第二行输入 n 个整数 a_1, a_2, \dots, a_n\left(0 \leqq a_i \leqq 10^9\right),表示各账号初始分数。

\hspace{15pt}第三行输入 m 个整数 b_1, b_2, \dots, b_m\left(0 \leqq b_j \leqq 10^9\right),其中第 j 个数表示第 j 场比赛结束后账号分数的增加值。


输出描述:
\hspace{15pt}输出 m 行,第 j 行输出第 j 场比赛结束后,小苯的 \max \mathrm{Rating}
示例1

输入

5 6
1145 1500 1600 1538 1222
10 400 500 1000 2000 10000

输出

1600
1600
1722
2500
3538
11555

说明

共比赛了 6 场,每场结束后均输出小苯所有账号中的最高分。

初始分数最低的账号分数为 1145,第一场比赛后其分数变为 1145 + 10 = 1155,最高分依旧为 1600,故输出 1600。

第二场比赛使用当前最低分账号 1155 参赛,分数增加 400 变为 1555,最高分仍为 1600,故输出 1600。

第三场比赛使用当前最低分 1222 参赛,分数增加 500 变为 1722,此时最高分为 1722,故输出 1722。

以此类推,得到后续输出。
推荐
#include <bits/stdc++.h>
using namespace std;

void solve() {
    int n, m;
    cin >> n >> m;
    multiset<int> st;
    for(int i = 1, x; i <= n; i++) {
        cin >> x;
        st.emplace(x);
    }
    while(m -- ) {
        int x;
        cin >> x;
        int mn = *st.begin();
        st.erase(st.begin());
        st.emplace(mn + x);
        cout << *st.rbegin() << endl;
    }
}


signed main () {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    int _ = 1;
    while(_ -- ) {
        solve();
    }
    return 0;
}

编辑于 2025-02-18 17:13:13 回复(0)
import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        // 使用BufferedReader提高大输入效率
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] parts = br.readLine().split(" ");
        int accountNum = Integer.parseInt(parts[0]);
        int raceNum = Integer.parseInt(parts[1]);

        TreeMap<Integer, Integer> tm = new TreeMap<>();

        // 读取初始账号分数
        parts = br.readLine().split(" ");
        for (int i = 0; i < accountNum; i++) {
            int score = Integer.parseInt(parts[i]);
            tm.put(score, tm.getOrDefault(score, 0) + 1);
        }

        // 读取所有比赛的分数增加值
        parts = br.readLine().split(" ");
        for (int i = 0; i < raceNum; i++) {
            int getRating = Integer.parseInt(parts[i]);
            int minRating = tm.firstKey();
            int minCount = tm.get(minRating);

            if (minCount == 1) {
                tm.remove(minRating);
            } else {
                tm.put(minRating, minCount - 1);
            }

            int newRating = minRating + getRating;
            tm.put(newRating, tm.getOrDefault(newRating, 0) + 1);
            System.out.println(tm.lastKey());
        }
    }
}

发表于 2025-08-06 20:39:05 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            int m = in.nextInt();
            TreeMap<Integer, Integer> zhanghao = new TreeMap<>();//创建一个TreeMap存放账号分数及账号出现的次数
            while(n-- > 0){
                int originrating = in.nextInt();
                zhanghao.put(originrating, zhanghao.getOrDefault(originrating, 0) + 1);
            }
            while(m-- > 0){
                int minzhanghao = zhanghao.firstKey();
                int currentrating;//读取最低的账号分数
                if(zhanghao.get(minzhanghao) == 1){//如果最低的账号分数只是一个账号,删除账号,比赛后添加新的分数进TreeMap
                    zhanghao.remove(minzhanghao);
                    currentrating = minzhanghao + in.nextInt();
                    zhanghao.put(currentrating, zhanghao.getOrDefault(currentrating, 0) + 1);
                }else{//如果最低的账号分数存在多个账号,另账号的value - 1,比赛后添加新的分数进TreeMap
                    int value = zhanghao.get(minzhanghao);
                    zhanghao.put(minzhanghao, value - 1);
                    currentrating = minzhanghao + in.nextInt();
                    zhanghao.put(currentrating, zhanghao.getOrDefault(currentrating, 0) + 1);
                }
                System.out.println(zhanghao.lastKey());
            }
        }
    }
}
发表于 2025-08-04 17:01:34 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    private static PriorityQueue<Integer> heap = new PriorityQueue<>();
    private static int maxTating = Integer.MIN_VALUE;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        for(int i = 0 ; i < n ; i++){
            int score = sc.nextInt();
            maxTating = Math.max(maxTating,score);
            heap.offer(score);
        }
        int[] match = new int[m];
        for(int i = 0 ; i < m ; i++){
            match[i] = sc.nextInt();
        }

        List<Integer> ans = resolve(match);
        for(int max : ans){
            System.out.println(max);
        }
        sc.close();
        
    }

    private static List<Integer> resolve(int[] match){
        int m = match.length;
        List<Integer> ans = new ArrayList<>(m);
        for(int each : match){
            int currentScore = heap.poll();
            currentScore += each;
            maxTating = Math.max(maxTating,currentScore);
            ans.add(maxTating);
            heap.offer(currentScore);
        }
        return ans;
    }
}

发表于 2025-03-06 19:49:05 回复(0)