工作排序問題

Problem statement:

問題陳述:

Given an array of jobs where every job has a deadline and a profit. Profit can be earned only if the job is finished before the deadline. It is also given that every job takes a single unit of time, so the minimum possible deadline for any job is 1. How to maximize total profit if only one job can be scheduled at a time. Print the sequence of jobID order to maximize total profit.

給定一系列工作,每個工作都有截止日期和利潤。 只有在截止日期之前完成工作,才能賺取利潤。 還假定每個工作都占用一個單位時間,因此任何工作的最小可能截止期限為1。如果一次只能安排一個工作,那么如何使總利潤最大化。 打印jobID訂單的順序以最大化總利潤。

Input example

輸入例

JobIDDeadlineProfit
1430
2220
3260
4230
5110
6480
職位編號 最后期限 利潤
1個 4 30
2 2 20
3 2 60
4 2 30
5 1個 10
6 4 80

Output example: 4 3 1 6

輸出示例:4 3 1 6

Problem explanation:

問題說明:

First of all, forget about the greedy algorithm. Let’s just solve with our intuition. Since the problem is to maximize profit intuition says to select jobs in decreasing order according to their profit. That means to select the maximum profit one first, then the 2nd maximum profit one and so on. While selecting jobs we only need to keep track whether it can be finished in deadline.

首先,忘掉貪婪算法 。 讓我們憑直覺來解決。 由于問題在于最大化利潤,因此直覺表示要根據他們的利潤以降序選擇工作。 這意味著首先選擇一個最大利潤,然后選擇第二個最大利潤,依此類推。 在選擇工作時,我們只需要跟蹤它是否可以在截止日期之前完成。

So let’s start...

所以我們開始吧...

It can be seen from the job table that, there are four jobs with the scheduled deadline at most 2. Thus we can only select a maximum of 2 jobs from these 4 jobs since each job take 1 unit time to process. (local observation)

從作業表中可以看到,有四個作業的排定的截止日期最多為2個。因此,由于每個作業需要1個單位時間來處理,因此我們只能從這4個作業中最多選擇2個作業。 (當地觀察)

At time 0:

在時間0:

Select maximum profit one with deadline at most 2
Job id: 3, deadline: 2, valid choice, process the job
Profit at time 0 : 60

選擇最大利潤之一,截止日期最多為2
職位編號:3,截止日期:2,有效選擇,處理職位
時間0:60的利潤

At time 1:

在時間1:

Select maximum profit one from remaining jobswith deadline at most 2
Job id: 4, deadline: 2, valid choice, process the job
Profit at time 1 : 60+30
That’s why can’t choose job with ID 5 & 2

從剩余工作中選擇最大利潤之一,截止日期最多為2
職位編號:4,期限:2,有效選擇,處理職位
時間1的利潤:60 + 30
這就是為什么不能選擇ID 5和2的工作

At time 2:

在時間2:

Select maximum from remaining one with deadline greater than 2
Job id: 6, deadline: 4, valid choice, process the job
Profit at time 2 : 60+30+80

從截止日期大于2的剩余一項中選擇最大值
職位編號:6,截止日期:4,有效選擇,處理職位
時間2的利潤:60 + 30 + 80

At time 3:

在時間3:

Select job
With job id: 1, deadline : 4, valid choice, process the job
Job sequence : 3 4 6 1
Finally total profit= 60+30+80+30=200

選擇工作
職位編號:1,截止日期:4,有效選擇,處理職位
作業順序:3 4 6 1
最終總利潤= 60 + 30 + 80 + 30 = 200

No other choice could have resulted better (you can check it!!!!). Thus the solution is optimized and we have found the maximum solution.

沒有其他選擇可以產生更好的效果(您可以檢查!!!!)。 因此,解決方案已經過優化,我們找到了最大的解決方案。

Now, revise what we have done. We have actually sorted the job table according to max profit & then have made the local best choice at each iteration to reduce the problem size & ultimately to reach the goal. Simply speaking, we have used the greedy technique intuitively & greedy algorithm has successfully solved the job sequencing problem.

現在,修改我們所做的。 我們實際上已經根據最大利潤對作業表進行了排序,然后在每次迭代中都做出了局部最佳選擇,以減少問題的規模并最終達到目標。 簡而言之,我們直觀地使用了貪婪技術 ,貪婪算法成功解決了工作排序問題。

Now to code simply follow the below steps which are nothing but what we did solving the previous example:

現在,只需按照以下步驟進行編碼,這些步驟不過是我們解決上一個示例所做的工作:

  1. Create a class to define jobs

    創建一個類來定義作業

  2. class job
    {
    public:
    int jobid;  //job id
    int deadline; //deadline
    int profit; //profit of the job
    };
    
    
  3. To take input we have used the concept of array of pointers to the job objects. job *obj[n]; //array of pointer to jobs(jobs namely)

    為了接受輸入,我們使用了指向作業對象的指針數組的概念。 工作* obj [n]; //指向作業的指針數組(即作業)

  4. maxProfit function()

    maxProfit函數()

    • Sort all jobs in decreasing order of profit.
    bool mycompare(job *x,job *y)//boolean function
    {
    //sort as per decreasing profite
    return x->profit>y->profit; 
    }
    sort(obj,obj+n,mycompare);
    
    
  5. Find the maximum deadline, let it be max.

    找到最大截止日期,使其為max

  6. Create store[max] to store job sequence

    創建store [max]以存儲作業序列

    Create slot[max] to mark occupied slots

    創建slot [max]以標記占用的插槽

  7. For i=0:no of jobs

    對于i = 0:沒有工作

  8. // now pick the job with max deadline from 
    // that deadline traverse array backto find an empty slot
    for(int j=(obj[i]->deadline)-1;j>=0;j--)
    {
    if(slot[j]==false){	// slot is empty
    // count the total profit
    profit+=obj[i]->profit;
    store[j]=obj[i]->jobid;
    slot[j]=true;
    break;
    }
    }
    
    
  9. Print the store array to find job sequence and print profit which is maximum profit output

    打印商店數組以查找工作順序并打印利潤 (最大利潤輸出)

C ++實現作業排序問題 (C++ implementation of Job sequencing problem)

#include<bits/stdc++.h>
using namespace std;
// define the class for the job
class job
{
public:
int jobid;
int deadline;
int profit;
};
// our compare function to sort
bool mycompare(job *x,job *y)//boolean function
{
//sort as per decreasing profit
return x->profit>y->profit; 
}
int maxProfit(job** obj,int n){
int max=0,profit=0;;
//find maximum deadline
for(int i=0;i<n;i++){
if(i==0){
max=obj[i]->deadline;
}
else{
if(obj[i]->deadline>max)
max=obj[i]->deadline;
}
}
sort(obj,obj+n,mycompare);
// create array of max deadline size
int store[max]={0};	// empty array initially
bool slot[max]={false}; //all slots empty initially
for(int i=0;i<n;i++)
{	
// now pick the job with max deadline and from 
// that deadline traverse array back to find an empty slot
for(int j=(obj[i]->deadline)-1;j>=0;j--)
{
if(slot[j]==false)	// slot is empty
{	
// count the total profit
profit+=obj[i]->profit;
store[j]=obj[i]->jobid;
slot[j]=true;
break;
}
}
}
// printing the job sequence
cout<<"jobs sequence is:"<<"\t";
for(int i=0;i<max;i++)
{	
if(slot[i])
cout<<store[i]<<"  ";
}
return profit; //return profit
}
int main()
{	
int n,size,max,totalprofit=0;
cout<<"enter the no. of jobs:";
cin>>n;
job *obj[n]; //array of pointer to jobs(jobs namely) 
// user input and finding maximum deadline
for(int i=0;i<n;i++)
{	
obj[i]=(job*)malloc(sizeof(struct job));
cout<<"enter jobid,deadline and profit of job "<<i+1<<endl;
cin>>obj[i]->jobid;
cin>>obj[i]->deadline;
cin>>obj[i]->profit;
}
totalprofit=maxProfit(obj,n); //total profit
cout<<"\ntotal profit is "<<totalprofit<<"\n";
return 0;	
}

Output

輸出量

enter the no. of jobs:6
enter jobid,deadline and profit of job 1
1 4 30
enter jobid,deadline and profit of job 2
2 2 20
enter jobid,deadline and profit of job 3
3 2 60  
enter jobid,deadline and profit of job 4
4 2 30  
enter jobid,deadline and profit of job 5
5 1 10  
enter jobid,deadline and profit of job 6
6 4 80  
jobs sequence is:       4  3  1  6
total profit is 200 

翻譯自: https://www.includehelp.com/icp/job-sequencing-problem.aspx

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377307.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377307.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377307.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

牛客網與leetcode刷題(高頻題中簡單or中等的)

目錄1、反轉鏈表2、排序3、先序中序后序遍歷4、最小的k個數5、子數組的最大累加和6、 用兩個棧實現隊列7、142. 環形鏈表 II8、20. 有效的括號9、最長公共子串(動態規劃),磕磕絆絆10、二叉樹之字形層序遍歷11、重建二叉樹12、LRU緩存13、合并兩個有序鏈表15、大數加法16、一個二…

AMUL的完整形式是什么?

AMUL&#xff1a;阿南德牛奶聯盟有限公司 (AMUL: Anand Milk Union Limited) AMUL is an abbreviation of Anand Milk Union Limited. It is an Indian milk product cooperative dairy organization that is based in the small town of Anand in the state of Gujarat. AMUL …

mochiweb 源碼閱讀(十一)

大家好&#xff0c;今天周六&#xff0c;繼續接著上一篇&#xff0c;跟大家分享mochiweb源碼。上一篇&#xff0c;最后我們看到了mochiweb_socket_server:listen/3函數&#xff1a; listen(Port, Opts, State#mochiweb_socket_server{sslSsl, ssl_optsSslOpts}) ->case moch…

Android下拉刷新完全解析,教你如何一分鐘實現下拉刷新功能 (轉)

轉載請注明出處&#xff1a;http://blog.csdn.net/guolin_blog/article/details/9255575 最 近項目中需要用到ListView下拉刷新的功能&#xff0c;一開始想圖省事&#xff0c;在網上直接找一個現成的&#xff0c;可是嘗試了網上多個版本的下拉刷新之后發現效果都不怎么理 想。有…

Python中的append()和extend()

列出append()方法 (List append() method) append() method is used to insert an element or a list object to the list and length of the List increased by the 1. append()方法用于將元素或列表對象插入列表&#xff0c;并且列表長度增加1。 Syntax: 句法&#xff1a; …

紅黑樹的實現

目錄1、紅黑樹原理1、紅黑樹性質2、變換規則&#xff08;從插入結點的角度來講&#xff09;1.變色2.左旋3.右旋3、刪除結點需要注意的地方2、代碼1、定義結點以及構造函數2、定義紅黑樹類以及聲明它的方法3、左旋4、右旋5、插入操作6、修正操作7、刪除操作3、參考鏈接1、紅黑樹…

118 - ZOJ Monthly, July 2012

http://acm.zju.edu.cn/onlinejudge/showContestProblems.do?contestId339 都是賽后做的。。。弱爆了 A題是找由2和5組成的數字的個數 直接打個表就行了 只是比賽的時候不知道怎么打表啊。。 View Code #include<cstdio> #include<cstring> #include<algorith…

edp1.2和edp1.4_EDP??的完整形式是什么?

edp1.2和edp1.4EDP??&#xff1a;電子數據處理 (EDP: Electronic Data Processing) EDP is an abbreviation of Electronic Data Processing. It alludes to the functioning of operations of commercial data, documents processing of storing, with the use of a compute…

高效讀書心得

1.盡量閱讀中文版 雖然有人英文很強&#xff0c;有的翻譯很差&#xff0c;但AnyWay 中文閱讀與理解的時間&#xff0c;略讀與快速定位感興趣內容的速度還是要快一些。 2.即時批注、總結筆記與交流 雖然愛書&#xff0c;但發現最有效的讀書方式還是不斷的制造脂批本&…

《MySQL——增刪改查以及常用語法》

目錄登錄和退出MySQL服務器基本語法&#xff08;增刪改查&#xff09;登錄和退出MySQL服務器 # 登錄MySQL 密碼 $ mysql -u root -p12345612 # 退出MySQL數據庫服務器 exit;基本語法&#xff08;增刪改查&#xff09; -- 顯示所有數據庫 show databases;-- 創建數據庫 CREA…

WCF簡介

一、簡介 WCF是Windows Communication Foundation縮寫&#xff0c;是Microsoft為構建面向服務的應用提供的分布式通信編程框架&#xff0c;是.NET Framework 3.5的重要組成部分。使用該框架&#xff0c;開發人員可以構建跨平臺、安全、可靠和支持事務處理的企業級互聯應用解決方…

css鏈接樣式_CSS中的樣式鏈接

css鏈接樣式CSS樣式鏈接 (CSS Styling Links) The links in CSS can be styled in various ways to make our website more presentable and attractive. The links can also be styled depending on their states e.g. visited, active, hover, etc. CSS中的鏈接可以通過各種方…

《MySQL——約束》

目錄主鍵約束唯一主鍵非空約束默認約束外鍵約束主鍵約束 -- 主鍵約束 -- 使某個字段不重復且不得為空&#xff0c;確保表內所有數據的唯一性。 CREATE TABLE user (id INT PRIMARY KEY,name VARCHAR(20) );-- 聯合主鍵 -- 聯合主鍵中的每個字段都不能為空&#xff0c;并且加起…

UIControl事件

CHENYILONG BlogUIControl事件 FullscreenUIControl事件1.UIControlEventTouchDown單點觸摸按下事件&#xff1a;用戶點觸屏幕&#xff0c;或者又有新手指落下的時候。2.UIControlEventTouchDownRepeat多點觸摸按下事件&#xff0c;點觸計數大于1&#xff1a;用戶按下第二、三、…

C++ 為什么要使用#ifdef __cplusplus extern C { #endif

經常看到別人的頭文件 有這樣的代碼 #ifdef __cplusplus extern "C" { #endif// C 樣式 的函數#ifdef __cplusplus } #endif 為什么要這樣呢&#xff1f; 因為 C 語言不支持重載函數 也就是同名函數&#xff0c;參數卻不一樣,C支持&#xff0c;其編譯器對函數名的處理…

css中的媒體查詢_CSS中的媒體查詢

css中的媒體查詢CSS | 媒體查詢 (CSS | Media Queries) Creating a web page is not an easy task as it requires loads of content and data so that it becomes strongly responsive to the users. To do that various contents are even added e.g.: resources, informativ…

SharePoint2013安裝組件時AppFabric時出現1603錯誤,解決方法:

采用PowerShell命令批量下載必備組件: 下載完成后&#xff0c;采用批處理命令安裝必備組件。 注&#xff1a;SPS2013安裝必備組件及批處理下載地址&#xff1a; 需要將必備組件放在安裝文件的PrerequisiteInstallerFiles文件夾中&#xff0c;將PreReq2013.bat放在安裝文件根目錄…

《MySQL——數據表設計三大范式》

目錄數據表設計范式第一范式第二范式第三范式數據表設計范式 第一范式 數據表中的所有字段都是不可分割的原子值。 字段值還可以繼續拆分的&#xff0c;就不滿足第一范式&#xff0c;如下&#xff1a; 下面這個&#xff0c;更加貼合第一范式&#xff1a; 范式設計得越詳細&…

三道簡單樹型dp+01背包~~hdu1561,poj1947,zoj3626

以前學樹型dp就是隨便的看了幾道題&#xff0c;沒有特別注意樹型dp中的小分類的總結&#xff0c;直到上次浙大月賽一道很簡單的樹型dp都不會&#xff0c;才意識到自己太水了&#xff5e;&#xff5e;come on&#xff01; hdu1561&#xff1a;題目給出了很多棵有根樹&#xff0c…

css 字體圖標更改顏色_在CSS中更改字體

css 字體圖標更改顏色CSS字體屬性 (CSS font properties ) Font properties in CSS is used to define the font family, boldness, size, and the style of a text. CSS中的字體屬性用于定義字體系列 &#xff0c; 粗體 &#xff0c; 大小和文本樣式 。 Syntax: 句法&#xf…