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
1 3 1 3 4 6 2 5
Sample Output
2 8
Source
Recommend
解题思路:首先把所有任务按照左端点排个序,然后从左往右扫,我们每次看当前任务的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;
}