牛客题解 | 奖学金
1.牛客题解 | 几个岛2.牛客题解 | 分割后处理3.牛客题解 | 分布式集群消息传递4.牛客题解 | 分玩具5.牛客题解 | 分田地6.牛客题解 | 分石头7.牛客题解 | 分苹果8.牛客题解 | 分贝壳9.牛客题解 | 列表补全10.牛客题解 | 删数11.牛客题解 | 删除公共字符12.牛客题解 | 删除重复字符13.牛客题解 | 删除重复字符_114.牛客题解 | 判断一棵满二叉树是否为二叉搜索树15.牛客题解 | 判断题16.牛客题解 | 包裹运输17.牛客题解 | 区间表达18.牛客题解 | 升级蓄水池19.牛客题解 | 单词缩写20.牛客题解 | 卡中心密码安全规范21.牛客题解 | 卡中心美食家22.牛客题解 | 厨艺大赛奖金23.牛客题解 | 友好城市24.牛客题解 | 双素数25.牛客题解 | 双色塔26.牛客题解 | 双袋购物27.牛客题解 | 发奖金28.牛客题解 | 古巴比伦迷宫29.牛客题解 | 句子反转30.牛客题解 | 吃鱼31.牛客题解 | 合唱团32.牛客题解 | 合并区间33.牛客题解 | 合并果子34.牛客题解 | 商品交易35.牛客题解 | 回合制游戏36.牛客题解 | 回文37.牛客题解 | 回文串38.牛客题解 | 回文序列39.牛客题解 | 回文数索引40.牛客题解 | 回文数组41.牛客题解 | 回文素数42.牛客题解 | 图的遍历43.牛客题解 | 图的闭包44.牛客题解 | 地下迷宫45.牛客题解 | 地牢逃脱46.牛客题解 | 地鼠逃跑计划47.牛客题解 | 堆棋子48.牛客题解 | 堆棋子_149.牛客题解 | 塔50.牛客题解 | 外卖满减51.牛客题解 | 多数组中位数52.牛客题解 | 多数组第 K 小数53.牛客题解 | 大家来扫雷54.牛客题解 | 大巴车(数组分块,按块翻转,块内不变)55.牛客题解 | 大数乘法56.牛客题解 | 大整数相乘57.牛客题解 | 头条校招58.牛客题解 | 奇数位丢弃
59.牛客题解 | 奖学金
60.牛客题解 | 奶牛编号61.牛客题解 | 好奇的薯队长62.牛客题解 | 如何添加运算符63.牛客题解 | 字母交换64.牛客题解 | 字母卡片65.牛客题解 | 字母数字混合排序66.牛客题解 | 字符串中找出连续最长的数字串67.牛客题解 | 字符串交错组成68.牛客题解 | 字符串价值69.牛客题解 | 字符串分割70.牛客题解 | 字符串匹配71.牛客题解 | 字符串压缩算法72.牛客题解 | 字符串复制73.牛客题解 | 字符串提取74.牛客题解 | 字符串旋转75.牛客题解 | 字符串是否由子串拼接76.牛客题解 | 字符串替换77.牛客题解 | 字符串替换_178.牛客题解 | 字符串最小变换次数79.牛客题解 | 字符串的排列80.牛客题解 | 字符串的旋转81.牛客题解 | 字符串相乘82.牛客题解 | 字符串组合83.牛客题解 | 字符串计数84.牛客题解 | 字符串距离85.牛客题解 | 字符串连连看86.牛客题解 | 字符串通配87.牛客题解 | 字符串问题88.牛客题解 | 字符混编89.牛客题解 | 字符编码90.牛客题解 | 字符覆盖91.牛客题解 | 字符迷阵92.牛客题解 | 字符迷阵_193.牛客题解 | 字符集合94.牛客题解 | 孙悟空的徒弟95.牛客题解 | 学数学96.牛客题解 | 安置路灯97.牛客题解 | 完成括号匹配98.牛客题解 | 实现字通配符*99.牛客题解 | 密码检查100.牛客题解 | 密码破译题目
解题思路
这是一个贪心算法问题。需要计算最少的复习时间来达到平均分要求。应该优先提高单位时间收益最大的课程分数。
关键点:
- 计算需要提高的总分数
- 按单位时间收益排序
- 考虑满分限制
- 贪心选择最优方案
算法步骤:
- 计算目标总分
- 按单位时间收益排序
- 贪心选择提分方案
- 累计所需时间
代码
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
long long solve(int n, int r, int avg, vector<pair<int, int>>& courses) {
// 计算目标总分
long long targetSum = (long long)avg * n;
long long currentSum = 0;
// 计算当前总分
for (auto& course : courses) {
currentSum += course.first;
}
// 如果当前分数已经达标
if (currentSum >= targetSum) {
return 0;
}
// 按单位时间收益排序
sort(courses.begin(), courses.end(),
[](const pair<int, int>& a, const pair<int, int>& b) {
return a.second < b.second;
});
long long needScore = targetSum - currentSum;
long long totalTime = 0;
// 贪心选择
for (auto& course : courses) {
int currentScore = course.first;
int timePerScore = course.second;
// 计算当前课程可以提高的分数
long long canImprove = r - currentScore;
long long improve = min(canImprove, needScore);
totalTime += improve * timePerScore;
needScore -= improve;
if (needScore <= 0) break;
}
return totalTime;
}
};
int main() {
int n, r, avg;
// 持续读取输入直到EOF
while (cin >> n >> r >> avg) {
vector<pair<int, int>> courses(n);
for (int i = 0; i < n; i++) {
cin >> courses[i].first >> courses[i].second;
}
Solution solution;
cout << solution.solve(n, r, avg, courses) << endl;
}
return 0;
}
import java.util.*;
public class Main {
static class Solution {
public long solve(int n, int r, int avg, int[][] courses) {
// 计算目标总分
long targetSum = (long)avg * n;
long currentSum = 0;
// 计算当前总分
for (int[] course : courses) {
currentSum += course[0];
}
// 如果当前分数已经达标
if (currentSum >= targetSum) {
return 0;
}
// 按单位时间收益排序
Arrays.sort(courses, (a, b) -> Integer.compare(a[1], b[1]));
long needScore = targetSum - currentSum;
long totalTime = 0;
// 贪心选择
for (int[] course : courses) {
int currentScore = course[0];
int timePerScore = course[1];
// 计算当前课程可以提高的分数
long canImprove = r - currentScore;
long improve = Math.min(canImprove, needScore);
totalTime += improve * timePerScore;
needScore -= improve;
if (needScore <= 0) break;
}
return totalTime;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 持续读取输入直到EOF
while (sc.hasNext()) {
int n = sc.nextInt();
int r = sc.nextInt();
int avg = sc.nextInt();
int[][] courses = new int[n][2];
for (int i = 0; i < n; i++) {
courses[i][0] = sc.nextInt(); // 平时成绩
courses[i][1] = sc.nextInt(); // 单位时间
}
Solution solution = new Solution();
System.out.println(solution.solve(n, r, avg, courses));
}
sc.close();
}
}
class Solution:
def solve(self, n, r, avg, courses):
# 计算目标总分
target_sum = avg * n
current_sum = sum(score for score, _ in courses)
# 如果当前分数已经达标
if current_sum >= target_sum:
return 0
# 按单位时间收益排序
courses.sort(key=lambda x: x[1])
need_score = target_sum - current_sum
total_time = 0
# 贪心选择
for score, time in courses:
# 计算当前课程可以提高的分数
can_improve = r - score
improve = min(can_improve, need_score)
total_time += improve * time
need_score -= improve
if need_score <= 0:
break
return total_time
# 持续读取输入直到EOF
while True:
try:
# 读取每组测试数据
n, r, avg = map(int, input().split())
courses = []
for _ in range(n):
a, b = map(int, input().split())
courses.append((a, b))
solution = Solution()
print(solution.solve(n, r, avg, courses))
except EOFError:
break
算法及复杂度
- 算法:贪心算法
- 时间复杂度:,主要是排序的时间
- 空间复杂度:,不考虑输入数组
合集:
牛客笔试大厂真题题解2
分类:
牛客笔试大厂真题题解2
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现