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