C++ 代码,通过该代码可以找出数学竞赛的优胜者
假设我们有二个大小为 n 的数组 P 和 T。并有另外一个数字 c。Amal 和 Bimal 准备参加一项数学竞赛。有 n 个问题。第 i 题的初始分数为 P[i],解该题需要 T[i]。P 和 T 均按递增顺序排序。此处 c 是损失积分的常量。如果在 x 时刻(比赛开始后 x 分钟)提交某题,则获得的分数最大为 max(0, P[i] - c*x)。Amal 将按顺序 1、2、...n 解题,而 Bimal 将按 n、n-1、...1 的顺序解题。我们必须确定谁将获得最高分数。如果两者分数相同,则为“平局”。
因此,如果输入为 c = 2;P = [50, 85, 250];T = [10, 15, 25],则输出将为 Amal。
步骤
为解决该问题,我们将遵循以下步骤 −
n := size of P m := 0 ans1 := 0 ans2 := 0 for initialize i := 0, when i < n, update (increase i by 1), do: m := m + T[i] ans1 := ans1 + maximum of (0, P[i] - c * m) m := 0 for initialize i := n - 1, when i > 1, update (decrease i by 1), do: m := m + T[i] ans2 := ans2 + maximum of (0, P[i] - c * m) if ans1 > ans2, then: return "Amal" otherwise when ans1 < ans2, then: return "Bimal" Otherwise return "Tie"
示例
让我们看以下实现,以便更好地理解 −
#include <bits/stdc++.h>
using namespace std;
string solve(int c, vector<int> P, vector<int> T){
int n = P.size();
int m = 0, ans1 = 0, ans2 = 0;
for (int i = 0; i < n; i++){
m += T[i];
ans1 += max(0, P[i] - c * m);
}
m = 0;
for (int i = n - 1; i > 1; i--){
m += T[i];
ans2 += max(0, P[i] - c * m);
}
if (ans1 > ans2)
return "Amal";
else if (ans1 < ans2)
return "Bimal";
else
return "Tie";
}
int main(){
int c = 2;
vector<int> P = { 50, 85, 250 };
vector<int> T = { 10, 15, 25 };
cout << solve(c, P, T) << endl;
}输入
2, { 50, 85, 250 }, { 10, 15, 25 }输出
Amal
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP