该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
知道是排序+贪心,但总报超时,不知何故,请哪位帮忙给看下,想法就是挑最晚结束的那个Cow,但不明白那里不对。多谢了
#include
#include
#include
#include
using namespace std;
typedef pair INTV_PAIR;
typedef vector INTV_MAP;
bool compare(const INTV_PAIR &x, const INTV_PAIR &y)
{
return x.first < y.first;
}
int solve() {
INTV_MAP endWorkIntervel;
long N = 0,T = 0;
long start = 0, end = 0;
long res = 1;
long lastTime = 0;
long firstTime = 1;
scanf("%ld%ld", &N, &T);
long headTag = 0;
long tailTag = 0;
for(int ii=0; ii
{
scanf("%ld%ld", &start, &end);
if(start == 1)
{
headTag = 1;
lastTime = end;
}
if(end >= T)
tailTag = 1;
endWorkIntervel.push_back(make_pair(start, end));
}
if(headTag == 0 || tailTag == 0)
return -1;
sort(endWorkIntervel.begin(), endWorkIntervel.end());
int i = 0;
int maxIndex = -1;
int maxEnd = 0;
int index = 0;
while(lastTime < T)
{
maxIndex = -1;
for(i = index; i < N; ++i)
{
if(endWorkIntervel[i].first <= lastTime)
{
if(endWorkIntervel[i].second > maxEnd)
{
maxEnd = endWorkIntervel[i].second;
maxIndex = i;
}
}
else
{
index = i;
break;
}
}
if(maxIndex > 0)
{
res++;
lastTime = maxEnd;
}
if(i == N && lastTime < T)
return -1;
}
return res;
}
int main() {
long res = solve();
printf("%d\n", res);
return 0;
}