天池 在线编程 高效作业处理服务(01背包DP)
生活随笔
收集整理的這篇文章主要介紹了
天池 在线编程 高效作业处理服务(01背包DP)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 1. 題目
- 2. 解題
1. 題目
https://tianchi.aliyun.com/oj/231188302809557697/235445278655844967
Twitter正在測試一種名為Pigeon的新工作處理服務。
Pigeon可以用任務實際持續時間的兩倍處理任務,并且每個任務都有一個權重。
此外,Pigeon在一個小時內只能服務一個有限的持續時間(最大運行時間)。
給定Pigon服務的最大運行時間,任務的實際運行時間和權重,確定Pigon服務在一小時內可以實現的最大總權重。
輸入包括以下參數:
n : 任務數量 weights : 每個任務的權重 tasks : 每個任務實際持續時間 p : Pigeon一小時的最大運行時間 示例 樣例1 輸入: 4 [2,4,4,5] [2,2,3,4] 15 輸出: 10 說明:你可以運行0、1、2號任務, 將花費2 * (2 + 2 + 3) = 14 分鐘并獲得 2 + 4 + 4 = 10 權重。樣例2 輸入: 3 [3,2,2] [3,2,2] 9 輸出: 4 說明:你可以運行1、2號任務, 將花費2 * (2 + 2) = 8 分鐘并獲得 2 + 2 = 4 權重。2. 解題
class Solution { public:/*** @param n: the number of tasks* @param weights: the weight for every task* @param tasks: the actual duration of every task* @param p: maximum runtime for Pigeon in an hour* @return: the maximum total weight that the Pigeon service can achieve in an hour*/int maxWeight(int n, vector<int> &weights, vector<int> &tasks, int p) {// write your code herevector<vector<int>> dp(n, vector<int>(p+1, -1));// dp[i][j] 表示處理完 i 任務,花費時間為 j 時 的最大權重和dp[0][0] = 0;if(tasks[0]*2 <= p)dp[0][tasks[0]*2] = weights[0];for(int i = 1; i < n; i++){dp[i] = dp[i-1];//復制上一次的狀態,當前的任務不做for(int t1 = 0; t1 < p; t1++){if(dp[i-1][t1] != -1 && t1+2*tasks[i] <= p){ //當前任務做dp[i][t1+2*tasks[i]] = max(dp[i][t1+2*tasks[i]], dp[i-1][t1]+weights[i]);}}}return *max_element(dp[n-1].begin(), dp[n-1].end());} };53ms C++
第4題:https://tianchi.aliyun.com/oj/231188302809557697/235445278655844964
解題:LeetCode 834. 樹中距離之和(樹上DP)*
我的CSDN博客地址 https://michael.blog.csdn.net/
長按或掃碼關注我的公眾號(Michael阿明),一起加油、一起學習進步!
總結
以上是生活随笔為你收集整理的天池 在线编程 高效作业处理服务(01背包DP)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 01.神经网络和深度学习 W2.神经网络
- 下一篇: LeetCode MySQL 1194.