工作排序问题

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.mzph.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…