贪心算法 -- 最小延迟调度

转自:https://blog.csdn.net/bqw18744018044/article/details/80285414

总结:

  首先,证明贪心的时候交换论证是万能的!其次,这一点如果要满足,也就是,如果你要用交换论证法,那么首先要保证交换逆序后,对其他的没有影响!如果有影响,那就只能像【POJ - 3253】Fence Repair 这道题一样,用优先队列去解决了。


1. 单区间调度问题
问题定义:存在单一资源,有一组以时间区间形式表示的资源请求reqs={req-1, req-2, …, req-n},第i个请求希望占用资源一段时间来完成某些任务,这段时间开始于begin(i)终止于end(i)。如果两个请求req-i和req-j在时间区间上没有重叠,则说这两个请求是相容的,求出这组请求的最大相容子集(最优子集)。举个例子:有一间多媒体课室,某一个周末有多个社团想要申请这间课室去举办社团活动,每个社团都有一个对应的申请时间段,比如周六上午8:00-10:00。求出这间课室在这个周末最多能满足几个社团的需求。

解决方案:贪心算法,优先选择最早结束的需求,确保资源尽可能早地被释放,把留下来满足其他需求的时间最大化。具体伪代码如下所示,算法结束后集合A中会保留所有相容请求,A的大小即是最大相容数量。

初始化R是所有需求的集合,A为空集
对R中的需求Ri,根据结束时间从早到晚排序
for Ri in R, do
  if Ri与A中的请求相容
    A = A并Ri
  endIf
endFor
return A

上述伪代码的C++实现如下,

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

const int MAX_SIZE = 100;

struct Request {
  int begin, end;
} req[MAX_SIZE];

bool operator<(const Request& req1, const Request& req2) {
  return req1.end < req2.end;
}

int main() {
  int requestNum;
  cin >> requestNum;
  if (requestNum > MAX_SIZE) {
    cout << "请求数量过多" << endl;
    return 0;
  }
  for (int i = 0; i < requestNum; ++i) {
    cin >> req[i].begin >> req[i].end;
  }

  sort(req, req + requestNum);

  vector<Request> rvec;
  rvec.push_back(req[0]);
  for (int i = 1; i < requestNum; ++i) {
    if (rvec[rvec.size() - 1].end <= req[i].begin) {
      rvec.push_back(req[i]);
    }
  }

  cout << "最大兼容量: " << rvec.size() << endl;
  return 0;
}

2. 多区间调度问题
问题定义:存在多个(或者无限多个)相同的资源,有一组以时间区间形式表示的资源请求reqs={req-1, req-2, …, req-n},第i个请求希望占用资源一段时间来完成某些任务,这段时间开始于begin(i)终止于end(i)。如果两个请求req-i和req-j在时间区间上没有重叠,则说这两个请求是相容的,用尽可能少的资源满足所有请求(求最优资源数量)。举个例子:有很多间课室,某个周末有多个社团需要申请课室办活动,每个社团都有一个对应的申请时间,求最少需要多少间课室才能够满足所有社团的需求(在这个问题之中时间重叠的社团需要安排在其他课室,即会使用到多个资源,需要考虑多个资源上的调度安排,故称为多区间调度)。

解决方案:贪心算法,将需求按照开始时间的早晚进行排序,然后开始为这些资源打标签,每个标签代表都一个资源,需求req-i被打上标签k表示该请求分配到的资源是k。遍历排序后的需求,如果一个需求与某个已分配资源上的其他安排不冲突,则把该需求也放进该资源的安排考虑中;如果冲突,那么应该要给此需求分配新的资源,已用资源数量加一。具体操作的伪代码如下所示。

对n个需求按照开始时间从早到晚进行排序
假设排序后的需求记为{R1, R2, ..., Rn}
初始化tagSize = 1;
for i=1 to n, do:
  tags = {1,2,...,tagSize};
  for j = 1 to i-1, do:
    if Rj与Ri时间区间重叠产生冲突:
      tags = tags - {Rj的标签};
    endIf
  endFor
  if tags为空集:
    tagSize += 1;
    将标签tagSize贴在Ri上
  EndIf
  else:
    在tags剩下的标签中随便挑一个贴给Ri
  endElse
endFor
此时每个请求上都贴有标签,每个标签对应其申请的资源编号,此时的tagSize就是至少需要的资源数量
return tagSize;


上述伪代码的C++实现如下:

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

const int MAX_SIZE = 100;

struct Request {
  int begin, end, tag;
} req[MAX_SIZE];

bool operator<(const Request& req1, const Request& req2) {
  return req1.begin < req2.begin;
}

int main() {
  int requestNum;
  cin >> requestNum;
  if (requestNum > MAX_SIZE) {
    cout << "请求数量过多" << endl;
    return 0;
  }
  for (int i = 0; i < requestNum; ++i) {
    cin >> req[i].begin >> req[i].end;
  }

  sort(req, req + requestNum);

  int tagSize = 1;
  req[0].tag = 0;
  bool tags[MAX_SIZE];
  for (int i = 1; i < requestNum; ++i) {
    memset(tags, 1, sizeof(tags));
    for (int j = 0; j < i; ++j) {
      if (req[j].end > req[i].begin) {
        tags[req[j].tag] = false;
      }
    }
    bool isTagsEmpty = true;
    int tag;
    for (int j = 0; j < tagSize; ++j) {
      if (tags[j]) {
        isTagsEmpty = false;
        tag = j;
        break;
      }
    }
    if (isTagsEmpty) {
      req[i].tag = tagSize;
      ++tagSize;
    } else {
      req[i].tag = tag;
    }
  }

  cout << "最小资源使用量: " << tagSize << endl;
  return 0;
}


3. 最小延迟调度问题
问题定义:存在单一资源和一组资源请求reqs={req-1, req-2, …, req-n},与前面两个问题不同,这里的资源从时刻0开始有效(开始接受申请,开始可以被使用),每个请求req-i都有一个截止时间ddl(i),每个请求都要占用资源一段连续的时间来完成任务,占用时间为time(i)。每个请求都希望自己能在ddl之前完成任务,不同需求必须被分在不重叠的时间区间(单一资源,同一时刻只能满足一个请求)。假设我们计划满足每个请求,但是允许某些请求延迟(即某个请求在ddl之后完成,延误工期),确定一种合理的安排,使得所有请求的延期时间中的最大值,是所有可能的时间安排情况中最小的。从时刻0开始,为每个请求req-i分配一个长度time(i)的时间区间,把区间标记为[begin(i), end(i)],其中end(i) = begin(i) + time(i)。如果end(i) > ddl(i),则请求req-i被延迟,延迟时间为delay(i) = end(i) - ddl(i);否则delay(i) = 0。合理安排需求,使得maxDelay = max{delay(1), delay(2), …, delay(n)}是所有可能的安排中最小的。

解决方案:贪心算法,按照截止时间ddl排序,越早截止的任务越早完成。该算法是一个没有空闲的最优调度,即从时刻0开始都有在处理请求,直到最后一个请求执行完释放资源之后才空闲。伪代码如下所示。

将需求按照截止时间进行排序
假设排序后的截止时间为ddl[1]<=...<=ddl[n]
start = 0;
maxDelay = 0;
for i = 1 to n, do:
  begin[i] = start;
  end[i] = start + time[i];
  start = end[i] + time[i];
  if maxDelay < end[i] - ddl[i]:
    L = end[i] - ddl[i];
  endIf
endFor
则每个任务安排的时间区间为[begin[i], end[i]],所有任务中最大的延迟为maxDelay,maxDelay为所有可能的任务安排中最小的延迟
return maxDelay;

上述代码的C++实现如下:

#include <iostream>
#include <algorithm>
using namespace std;

const int MAX_SIZE = 100;

struct Request {
  int time, ddl;
  int begin, end;
} req[MAX_SIZE];

bool operator<(const Request& req1, const Request& req2) {
  return req1.ddl < req2.ddl;
}

int main() {
  int requestNum;
  cin >> requestNum;
  if (requestNum > MAX_SIZE) {
    cout << "请求数量过多" << endl;
    return 0;
  }
  for (int i = 0; i < requestNum; ++i) {
    cin >> req[i].time >> req[i].ddl;
  }

  sort(req, req + requestNum);

  int start = 0, maxDelay = 0;
  for (int i = 0; i < requestNum; ++i) {
    req[i].begin = start;
    req[i].end = start + req[i].time;
    start += req[i].time;
    if (maxDelay < req[i].end - req[i].ddl) {
      maxDelay = req[i].end - req[i].ddl;
    }
  }

  cout << "最小的最大延迟: " << maxDelay << endl;
  return 0;
}

转自:https://blog.csdn.net/hongchh/article/details/52183614

代码格式不做调整,详情请去原博主博客中看。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441332.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

php成行排列,一个php实现的生成排列的算法

function perm($s, $n, $index){if($n 0){return ;}else{$nIndex count($index); //可用的字符串下标$res array();foreach($index as $i > $v){$tmp $index;unset($tmp[$i]); //去掉当前的前缀/* 调试信息&#xff0c;便于理解echo "len $n , cur $i , index:\n&q…

【CodeForces - 1051C 】Vasya and Multisets (模拟)

题干&#xff1a; Vasya has a multiset ss consisting of nn integer numbers. Vasya calls some number xxnice if it appears in the multiset exactly once. For example, multiset {1,1,2,3,3,3,4}{1,1,2,3,3,3,4} contains nice numbers 22 and 44. Vasya wants to spl…

apache2+支持php7,Ubuntu14.04下配置PHP7.0+Apache2+Mysql5.7

Apache步骤一&#xff1a;安装apacheronyaoubuntu:~$ sudo apt install apache2安装好后&#xff0c;在浏览器上输入localhost(服务器端&#xff0c;请输入你的IP地址)&#xff0c;回车就会看到&#xff1a;PHP7.0步骤二&#xff1a;Ubuntu14.04下的默认源是PHP5.0&#xff0c;…

php怎么添加验证码,PHP添加验证码以及使用

现在很多页面在使用表单提交时&#xff0c;都会使用到验证码的使用、如何制做一个验证码呢&#xff1f;这里有一个用PHP的方法 以及代码1、首先在php.ini 配置文件里面把GD库打开 // 在php.ini里面找到 extensionphp_gd2.dll 把前面的分号删去。2、代码&#xff1a;<?php …

【CodeForces - 1051D】Bicolorings (dp,类似状压dp)

题干&#xff1a; You are given a grid, consisting of 22 rows and nn columns. Each cell of this grid should be colored either black or white. Two cells are considered neighbours if they have a common border and share the same color. Two cells AA and BB be…

oracle内存锁,Oracle OCP之硬解析在共享池中获取内存锁的过程

(1)在父游标的名柄没有找到SQL语句的文本&#xff1a;select * from gyj_t1 where id1;2、释放library cache Latch3、获得shared pool Latch(1)搜索FreeList 空闲Chunk(2)搜索LRU上可覆盖的chunk(3)搜索R-FreeList 空闲Chunk(4)如果没空间了&#xff0c;直接ORA-04031错误4、释…

【CodeForces - 214B】Hometask (模拟,有坑)

题干&#xff1a; Furik loves math lessons very much, so he doesnt attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You ar…

php 修改文件属性命令行,Linux_linux中如何通过命令修改文件属性,ls -l即可查看目录信息-rw - phpStudy...

linux中如何通过命令修改文件属性ls -l即可查看目录信息-rwxr-xr-x 1 xura xura 1753786 2010-05-09 09:54 Grad分别对应的是&#xff1a;文件属性 连接数 文件拥有者 所属群组 文件大小 文件修改时间 文件名例如&#xff1a;d   rwx   r-x  r-x第一个字符指定了文件类型。在…

【 HDU - 1796】How many integers can you find (容斥原理,二进制枚举或者dfs)

题干&#xff1a; Now you get a number N, and a M-integers set, you should find out how many integers which are small than N, that they can divided exactly by any integers in the set. For example, N12, and M-integer set is {2,3}, so there is another set {2,…

aix解锁oracle用户,aix用户被锁定的解决办法

原/etc/security/lastlog文件&#xff1a;oracle:time_last_login 1212750668tty_last_login /dev/pts/2host_last_login 10.126.10.200unsuccessful_login_count 18time_last_unsuccessful_login 1212750699tty_last_unsuccessful_login /dev/pts/2host_last_unsuccessf…

【CodeForces - 205B 】Little Elephant and Sorting (思维)

题干&#xff1a; The Little Elephant loves sortings. He has an array a consisting of n integers. Lets number the array elements from 1 to n, then the i-th element will be denoted as ai. The Little Elephant can make one move to choose an arbitrary pair of…

oracle外键有什么用,深入理解Oracle索引(20):外键是否应该加索引

先表明我的立场、我是绝对支持外键一定要加索引&#xff01;虽然在高版本的Oracle里、对这个要求有所降低、但依然有如下原因&#xff1a;① 死锁外键未加索引是导致死锁的最主要原因、因为无论更新父表主键、或者删除一个父表记录、都会在子表加一个表锁这就会不必要的锁定更多…

【CodeForces - 1027B 】Numbers on the Chessboard (没有营养的找规律题,无聊题)

题干&#xff1a; You are given a chessboard of size nnnn. It is filled with numbers from 11 to n2n2 in the following way: the first ⌈n22⌉⌈n22⌉ numbers from 11 to ⌈n22⌉⌈n22⌉ are written in the cells with even sum of coordinates from left to right f…

php mysql int 日期格式化 string,MYSQL int类型字段的时间存放和显示 和 php的时间存放函数...

mysql&#xff1a;int类型字段的时间存放UPDATE tablename SET add_time UNIX_TIMESTAMP(NOW())int类型字段的时间显示SELECT FROM_UNIXTIME(add_time) FROM tablenamephp时间戳函数&#xff1a;time() 获取当前时间戳 结果&#xff1a;1232553600strtotime() 转换为时间戳da…

【CodeForces - 1060C】Maximum Subrectangle (思维,预处理前缀和,dp,枚举长度)

题干&#xff1a; You are given two arrays aa and bb of positive integers, with length nn and mmrespectively. Let cc be an nmnm matrix, where ci,jai⋅bjci,jai⋅bj. You need to find a subrectangle of the matrix cc such that the sum of its elements is at m…

oracle按照指定顺序读取,oracle按照指定顺序进行排序

之前在网上查了下按照指定顺序进行排序的方法&#xff0c;根据charindex来处理排序&#xff0c;但是在oracle发现不行&#xff0c;因为oracle没有charindex函数&#xff0c;然后使用instr代替了charindex&#xff0c;然后又在网上搜了另外一种方实验如下&#xff1a;1.新建表CR…

【Codeforces 631C 】Report(单调栈,思维模拟)

题干&#xff1a; Each month Blake gets the report containing main economic indicators of the company "Blake Technologies". There are n commodities produced by the company. For each of them there is exactly one integer in the final report, that d…

oracle开放视图,Oracle视图

Oracle视图在Oracle中&#xff0c;视图是实际上并不存在的虚拟表。它存储在Oracle数据字典中&#xff0c;不存储任何数据。可以在调用时执行。通过连接一个或多个表的查询创建视图。Oracle创建视图句法&#xff1a;参数&#xff1a;view_name&#xff1a;它指定要创建的Oracle …

【CodeForces - 214C 】Game (拓扑排序,思维)

题干&#xff1a; Furik and Rubik love playing computer games. Furik has recently found a new game that greatly interested Rubik. The game consists of n parts and to complete each part a player may probably need to complete some other ones. We know that th…

php$this-conn可以不先定义吗,CodeIgniter 是不是支持PDO 查询?还是本来就不支持

CodeIgniter 是否支持PDO 查询&#xff1f;还是本来就不支持&#xff1f;本帖最后由 default7 于 2014-11-15 19:34:55 编辑配置CodeIgniter 的database 连接方式为PDO 类型&#xff0c;但是怎么都查询不到数据&#xff0c;却可以查处有多少记录&#xff01;自带代码仔细跟踪代…