HDU 6180 贪心记录

来源:互联网 发布:mac怎么登陆远程桌面 编辑:程序博客网 时间:2024/06/07 11:51

Schedule

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 153428/153428 K (Java/Others)
Total Submission(s): 266    Accepted Submission(s): 105


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.
 

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
 


题意给出一系列的操作时间段,开头和结尾,要求用最少的电脑来完成,电脑中途不得关机,任务不得重叠,求电脑数最小情况下的最小电脑开机总时间,只要中途电脑开着,没工作也要计入时间。

思路,用两个数组分别记录每台电脑的开机和关机时间,对于每次任务,用一个数组存,用一个flag标记它是开始还是结束,先从前往后扫一遍,然后每次读入的是开始的话,sum + 1,去与当前的 ans 比较,比它大则开多台电脑,否则不用多开,如果是结束状态的话 sum - 1,每次开机都记录开机时间。

然后从后往前扫一遍,是结束的,sum + 1 然后与 ans 比较,比它大就关一台机,同时记录时间。

#include<cstdio>#include<iostream>#include<algorithm>#include<string.h>using namespace std;#define maxn 100005#define ll long longstruct node{int x,flag;}p[maxn * 2];ll le[maxn],ri[maxn];ll sum,ans;bool cmp(node a,node b){if(a.x == b.x){return a.flag < b.flag;}return a.x < b.x;}int main(){int t,n;scanf("%d",&t);while(t--){scanf("%d",&n);for(int i = 1;i <= n;i++){scanf("%lld %lld",&p[i * 2 - 1].x,&p[i * 2].x);p[i * 2 - 1].flag = 1;p[i * 2].flag = -1;}n <<= 1;sort(p + 1,p + 1 + n,cmp);sum = ans = 0;for(int i = 1;i <= n;i++){sum += p[i].flag;if(sum > ans){ans = sum;le[sum] = p[i].x;}ans = max(ans,sum);}sum = ans = 0;for(int i = n;i >= 1;i--){sum -= p[i].flag;if(sum > ans){ans = sum;ri[sum] = p[i].x;}ans = max(ans,sum);}sum = 0;for(int i = 1;i <= ans;i++){sum += ri[i] - le[i];}printf("%lld %lld\n",ans,sum);}return 0;}


原创粉丝点击