首页 > 试题广场 >

小苯的比赛上分

[编程题]小苯的比赛上分
  • 热度指数: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 sys, heapq

n, m = map(int, sys.stdin.readline().split())
a, b = list(map(int,sys.stdin.readline().split())), list(map(int,sys.stdin.readline().split()))
heapq.heapify(a)
max_a = max(a)
for j in b:
    min_a = heapq.heappop(a)
    min_a += j
    heapq.heappush(a, min_a)
    max_a = max(max_a, min_a)
    print(max_a)

发表于 2025-12-09 12:05:47 回复(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 {
    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)
import heapq

n, m = map(int, input().split())
scores = list(map(int, input().split()))
changes = list(map(int, input().split()))

max_rating = max(scores)
heap = [(score, i) for i, score in enumerate(scores)]
heapq.heapify(heap)

for change in changes:
    min_score, min_index = heapq.heappop(heap)
    new_score = min_score + change
    scores[min_index] = new_score
    max_rating = max(max_rating, new_score)
    heapq.heappush(heap, (new_score, min_index))
    print(max_rating)

发表于 2025-03-05 15:05:46 回复(2)
#include <iostream>
using namespace std;
#include<set>
#include<vector>
int main() {
  int n,m;
  cin>>n>>m;
  multiset<int>s;
  vector<int>v;
  while(n--){
    int a;
    cin>>a;
    s.insert(a);
  }
  for(int i=0;i<m;i++){
     int b;
     cin>>b;
     v.push_back(b);
  }
  for(int i=0;i<m;i++){
    int now_use_score=(*s.begin());
    int afterscore=now_use_score+v[i];
    s.erase(s.begin());
    s.insert(afterscore);
    auto pos=s.end();
    pos--;
    cout<<(*pos)<<endl;
  }
}

发表于 2025-12-11 21:00:24 回复(0)
//这题的c#解法,因为牛客的在线编程对c#只支持到.net4.0所以这个代码是提交不了的,只能图一乐。。。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class Program
{

    public static void Main()
    {
        PriorityQueue<int,int> pq= new PriorityQueue<int, int>();
        string[] inputs1 = (Console.ReadLine() ?? "").Split(" ");
        int.TryParse(inputs1[0], out int numOfAcc);
        int.TryParse(inputs1[1], out int numOfGame);

        string[] scoreOfAccounts = (Console.ReadLine() ?? "").Split(" ");
        string[] scoreOfGames = (Console.ReadLine() ?? "").Split(" ");

        //用一个maxRating来储存当前最高分账号
        int maxRating = 0;

        //比赛开始前先得到maxRating并把所有的账号分数入优先队列
        foreach (var score in scoreOfAccounts)
        {
            int.TryParse(score, out int currAccScore);

            if(currAccScore > maxRating)
                maxRating = currAccScore;
           
            pq.Enqueue(currAccScore, currAccScore);
        }
       
        //每次出列并使用最低分账号打比赛后,对比这个打完比赛后的账号的分数是不是最大,然后入优先队列
        foreach (var increasemnt in scoreOfGames)
        {
            int currMinAcc = pq.Dequeue();
            int.TryParse(increasemnt, out int increasemntValue);
            int newAccScore = currMinAcc + increasemntValue;
            if(maxRating < newAccScore)
                maxRating = newAccScore;
           
            Console.WriteLine(maxRating);
            pq.Enqueue(newAccScore,newAccScore);
        }

    }
}

发表于 2025-12-03 10:30:02 回复(0)
#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,m,x,temp;
    multiset<int> pq;
    cin>>n>>m;
    for(int i=0;i<n;i++){
        cin>>x;
        pq.insert(x);
    }
    for(int i=0;i<m;i++){
        cin>>x;
        temp=*(pq.begin());
        pq.erase(pq.begin());
        temp+=x;
        pq.insert(temp);
        cout<<*(--(pq.end()))<<endl;
    }
    return 0;}
发表于 2025-11-04 00:04:13 回复(0)
import heapq
import sys
n,m=map(int,input().split())
lst1=list(map(int,sys.stdin.readline().strip().split()))
lst2=list(map(int,sys.stdin.readline().strip().split()))
pq1=[]
pq2=[]
count=dict()
for i in lst1:
    heapq.heappush(pq1,i)
    heapq.heappush(pq2,-i)
    count[i]=count.get(i,0)+1
for i in lst2:
    x=0
    while pq1:
        x=pq1[0]
        if count.get(x,0)>0:
            count[x]-=1
            break
        else:
            heapq.heappop(pq1)
    heapq.heappush(pq1,x+i)
    count[x+i]=count.get(x+i,0)+1
    heapq.heappush(pq2,-(x+i))
    print(-pq2[0])




发表于 2025-10-08 17:52:43 回复(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)
#include <bits/stdc++.h>
#include <queue>
#include <vector>
using namespace std;

int main() {
    int n,m;
    cin >> n >> m;
    priority_queue<long long, vector<long long>, greater<long long> > z账号初始分;    
    vector<long long> b比赛增加值;
    long long max = -1;                         // 记录最大值(比赛结束后的多个号中最大分数)
    while(n){
        long long t;
        cin >> t;
        if(t > max ){
            max = t;
        }
        z账号初始分.push(t);
        n--;
    }
    while(m){
        long long t;
        cin >> t;
        b比赛增加值.push_back(t);
        m--;    
    }
    for(int i=0;i<b比赛增加值.size();i++){
        int t = 0;
        t = z账号初始分.top() + b比赛增加值[i];
        if(t > max){
            max = t;
        }
        z账号初始分.pop();
        z账号初始分.push(t);
        cout << max << endl;
    }
}
找最大值很简单,直接一个一个算的时候找就行
发表于 2025-08-01 20:18:33 回复(0)
import heapq

n, m = map(int, input().split())
li1 = list(map(int, input().split()))
minqueue = []
maxqueue = []
for i in li1:
    heapq.heappush(minqueue, i)
    heapq.heappush(maxqueue, -i)
li2 = list(map(int, input().split()))
max_val = heapq.heappop(maxqueue)
for i in range(m):
    min_val = heapq.heappop(minqueue)
    new_val = min_val + li2[i]
    heapq.heappush(minqueue, new_val)
    heapq.heappush(maxqueue, -new_val)
    current = maxqueue[0]
    if current < max_val:
        max_val = current
    print(-max_val)

发表于 2025-07-20 20:16:26 回复(0)
#include <bits/stdc++.h>

using namespace std;
int n,m;
const int N = 1e5+10;
int a[N],b[N];

int main()
{
    cin>>n>>m;
    for(int i=0;i<n;++i)cin>>a[i];
    for(int j=0;j<m;++j)cin>>b[j];
    priority_queue<int,vector<int>,greater<int> >pq;
    int ma=-1;
    for(int i=0;i<n;++i){
        pq.push(a[i]);
        ma=max(ma,a[i]);
    }
    for(int i=0;i<m;++i)
    {
        int t=pq.top();
        pq.pop();
        t+=b[i];
        ma=max(ma,t);
        pq.push(t);
        cout<<ma<<'\n';
    }
    return 0;
}

发表于 2025-05-19 18:30:15 回复(0)
1
发表于 2025-03-11 19:54:18 回复(0)
import java.util.PriorityQueue;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int accounts = in.nextInt();
        int plays = in.nextInt();
        int max_ = 0;
        int[] playsArray = new int[plays];
        PriorityQueue<Integer> pq = new PriorityQueue<>();
        for(int i = 0; i < accounts; i++){
            int rating = in.nextInt();
            max_ = Math.max(max_, rating);
            pq.offer(rating);
        } 
        for(int i = 0; i < plays; i++) playsArray[i] = in.nextInt();
        for(int i = 0; i < plays; i++){
            int now = pq.poll();
            now += playsArray[i];
            max_ = Math.max(max_, now);
            pq.offer(now);
            System.out.println(max_);
        }
    }
}
优先队列
过了就是成功
发表于 2025-03-07 00:16:06 回复(0)
c++ 解法,

参考剑指offer最大的k个数

可以通过使用一个类似最小堆的容器来优化

这道题的关键点应该就是找到分数变动后分数最低的账户。

如果使用简单粗暴的遍历比较获取最小值的方法,将会超时。

剑指offer是求最大数,故使用的是基于红黑树的multiset实现的最大堆。

这里是最小堆,所以应该使用queue头文件当中的priority_queue.

priority_queue<int, vector<int>, greater<int>> minheap;


代码实现
#include <functional>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;

int minium_pos(vector<int>& ivc) {
    int min = 0;

    for (int i = 0; i < ivc.size(); i++) {
        if (ivc[i] < ivc[min])
            min = i;
    }
    return min;
}

int main() {

    int n, m;
    cin >> n >> m;

    vector<int> account(n);

    for (int& x : account) {
        cin >> x;
    }

    vector<int> changes(m);
    for (int& x : changes) {
        cin >> x;
    }

    int min = 0, max_value = 0;

    priority_queue<int, vector<int>, greater<int>> minheap;

    for (int i = 0; i < n; i++) {
        minheap.push(account[i]);
        if (account[i] > max_value)
            max_value = account[i];
    }

    for (int i = 0; i < changes.size(); i++)
    {

        int new_min = minheap.top() + changes[i];

        if(new_min > max_value)
            max_value = new_min;

        minheap.pop();
        minheap.push(new_min);
        
        cout << max_value << endl;
    }
}


发表于 2025-02-16 22:45:43 回复(0)
import java.util.PriorityQueue;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
PriorityQueue<Integer> smallPQ = new PriorityQueue<>();

in.nextLine();
int max = 0;
for (int i = 1; i <= n; i++) {
int a = in.nextInt();
max = Math.max(max, a);
smallPQ.add(a);
}

in.nextLine();
while (m-- > 0) {
int incr = in.nextInt();
int a = smallPQ.poll() + incr;
max = Math.max(max, a);
smallPQ.add(a);
System.out.println(max);
}
}
}
发表于 2025-02-15 16:48:40 回复(0)