hihocoder-任务分配
来源:互联网 发布:为什么学java 编辑:程序博客网 时间:2024/06/08 06:14
Description
Given N tasks with starting time and end time ( S1, E1 ), ( S2, E2 ), …, ( SN, EN ), try to distribute and finish them with the minimum number of machines.
At the same time there is at most one running task on each machine and one task should keep running on one machine.
Input
The first line contains 1 integer N (1 ≤ N ≤ 100000), the number of tasks.
The following N lines each contain 2 integers Si and Ei,(0 ≤ Si < Ei ≤ 1000000000), the starting and end time of i-th task.
Output
The minimum number of machine needed.
题解
题目大意是给定n个任务的起始时间,求问最少需要多少台机器。
首先我们对几个随意的任务进行画图分析,可以分为两种情况—>1.两个任务开始时间一样,但是两者任务时间长度不同;2.两个任务开始时间不同,两者任务时间长度相同或不同。
采用贪心的策略解决,我们对于情况一选择时间长度小的。情况二选择时间长度早的。这样可以得到当前机器应该运行的任务单。这里就不详细举例了,请自己脑补。
至于重开机器的情况,我们的操作的效果是等价的,假设当前执行任务为S,任务池中存在一个可以连接S的任务P,那么一个可以连接S的先任务必然可以连接P(S的开始时间早于P).
最后我们可以容易得到对所有任务按照开始时间排序,候选结束时间排序。然后采用优先队列对任务的序列进行维护即可。
数据结构分析
采用一个类T来存放任务的开始start和结束end信息
任务池
实现池
运行过程
输入
q容器内(输入初始)
细心的童鞋会发现2,3位置反了,那是因为我重写了运算符
que容器内变化(优先队列)
[ ] 初始状态
[ ] q[0]
[ ] q[1]
[ ] q[2]
[ ] q[3]
[ ] End.
———–在AC之路上一路向东
附上源代码
#include <iostream>#include <queue>using namespace std;class T {public: int s,e; T(int a, int b):s(a), e(b){ }};bool operator < (const T &t1,const T &t2){ if(t1.s != t2.s) return t1.s > t2.s; else return t1.e > t2.e;}priority_queue<T> q;void slove(){ priority_queue<int,vector<int>, greater<int>> que; que.push(0); int tmp; while (!q.empty()){ T t = q.top(); q.pop(); tmp = que.top(); que.pop(); if (tmp <= t.s) { que.push(t.e); }else{ que.push(t.e),que.push(tmp); } } cout<<que.size()<<endl;}int main(){ int n,v1,v2; //while(){ cin >> n; // for (int i = 0; i < n; i++) { cin>>v1>>v2; q.push(T(v1,v2)); } slove(); //} return 0;}
- hihocoder #1309 : 任务分配
- hihocoder-任务分配
- [hihoCoder] 任务分配 解题报告
- 任务分配hihocoder 1309(离散化 )
- 任务分配
- 任务分配
- 任务分配
- 任务的详细分配
- ACM 任务分配问题
- 【任务分配】解题报告
- 项目经理如何分配任务
- linux---任务分配(PBS)
- hadoop任务分配
- 项目经理如何分配任务
- 任务分配及管理
- 项目经理如何分配任务
- TYVJ 1431 分配任务
- 分配任务第一天
- RabbitMQ理解
- Hdu 2476 String painter【思维+区间Dp】好题~
- 回音消除技术概述
- android-OKHttp
- 使用GCC编译的常用功能
- hihocoder-任务分配
- MySql优化-你的SQL命中索引了
- MySQL查询本周、上周、本月、上个月份数据的sql代码
- Flink学习笔记 --- WaterMark机制 and low WaterMark 保序机制
- Android常用开源项目(二十三)
- js生成二维码并下载
- 把MySQL Group Replication发挥到极致
- 利用FormData上传文件不跳转页面
- linux防御攻击