HDU6180(贪心)
来源:互联网 发布:大学 知乎 编辑:程序博客网 时间:2024/06/06 00:39
Schedule
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 153428/153428 K (Java/Others)Total Submission(s): 181 Accepted Submission(s): 73
Problem Description
There are N schedules, the i-th schedule has start time si and end time ei (1 <= i <= N). There are some machines. Each two overlapping schedules cannot be performed in the same machine. For each machine the working time is defined as the difference between timeend and timestart , where time_{end} is time to turn off the machine and timestart is time to turn on the machine. We assume that the machine cannot be turned off between the timestart and the timeend .
Print the minimum number K of the machines for performing all schedules, and when only uses K machines, print the minimum sum of all working times.
Print the minimum number K of the machines for performing all schedules, and when only uses K machines, print the minimum sum of all working times.
Input
The first line contains an integer T (1 <= T <= 100), the number of test cases. Each case begins with a line containing one integer N (0 < N <= 100000). Each of the next N lines contains two integers si and ei (0<=si<ei<=1e9) .
Output
For each test case, print the minimum possible number of machines and the minimum sum of all working times.
Sample Input
131 34 62 5
Sample Output
2 8
Source
2017 Multi-University Training Contest - Team 10
Recommend
liuyiding | We have carefully selected several similar problems for you: 6181 6180 6179 6178 6177
解题思路:首先把所有任务按照左端点排个序,然后从左往右扫,我们每次看当前任务的s值比我们已经用过的机器中t值是否要大,如果是,则选择一个机器的t值离s值最近的机器来完成这个任务,否则开一台新的机器。我们用一个multiset维护一下就行
#include <bits/stdc++.h>using namespace std;const int maxn = 100000 + 10;typedef long long LL;struct task{ LL s, t; bool operator <(const task &res) const { return s < res.s; }}T[maxn];struct machine{ LL s, t; bool operator <(const machine &res) const{ return t > res.t; }};int n;//priority_queue<machine> q;multiset<machine> ss;multiset<machine>::iterator it;//machine term[maxn];int cnt;int main(){ //freopen("C:\\Users\\creator\\Desktop\\in1.txt","r",stdin) ; //IO::Begin() ; //freopen("C:\\Users\\creator\\Desktop\\out.txt","w",stdout) ; int Case; scanf("%d", &Case); while(Case--) { ss.clear(); scanf("%d", &n); for(int i = 1; i <= n; i++) { scanf("%lld%lld", &T[i].s, &T[i].t); } sort(T + 1, T + n + 1); //while(!q.empty()) q.pop(); LL sum = 0; int ans = 0; machine test; task temp; for(int i = 1; i <= n; i++) { if(ss.empty()) { ans++; test.s = T[i].s; test.t = T[i].t; ss.insert(test); } else { test.s = 0; test.t = T[i].s; it = ss.lower_bound(test); //it = lower_bound(ss.begin(), ss.end(), test); if(it == ss.end()) { test.s = T[i].s; test.t = T[i].t; ss.insert(test); ans++; } else { test = *it; test.t = T[i].t; ss.erase(it); ss.insert(test); } } } for(it = ss.begin(); it != ss.end(); ++it) { test = *it; sum += test.t - test.s; } printf("%d %lld\n", ans, sum); } return 0;}
阅读全文
1 0
- HDU6180(贪心)
- HDU6180
- 多校10_1010 hdu6180 贪心+STL:multiset
- hdu6180-贪心&互不重叠的线段的组数-Schedule
- HDU6180-Schedule
- HDU6180 Schedule
- HDU6180-Schedule
- 贪心!
- 贪心~
- 贪心
- 贪心
- 贪心
- 贪心
- 贪心
- 贪心
- 贪心
- 贪心
- 贪心
- deep learning 深度学习的一点tricks
- js--缓动动画、匀速运动、无缝滚动
- Elevators UVALive
- Javascript 实现城市选择控件
- Git命令大全
- HDU6180(贪心)
- 教你避免内存泄露
- HTTP请求头和响应头
- PHP编程效率的20个要点 (2015-09-25 14:37:49) 转载 ▼ 用单引号代替双引号来包含字符串,这样做会更快一些。因为PHP会在双引号包围的字符串中搜寻变量,单引号则 不会,
- Unity Canvas UI line Renderer
- 前端对后台传过来是数组进行重新排序显示
- 【二叉树经典问题】106. Construct Binary Tree from Inorder and Postorder Traversal
- hdu6170(dp)
- spring cloud教程---Eureka