As this term is going to end, DRD needs to prepare for his final exams.
DRD has n exams. They are all hard, but their difficulties are different. DRD will spend at least ri hours on the i -th course before its exam starts, or he will fail it. The i -th course's exam will take place ei hours later from now, and it will last for li hours. When DRD takes an exam, he must devote himself to this exam and cannot (p)review any courses. Note that DRD can review for discontinuous time.
So he wonder whether he can pass all of his courses.
No two exams will collide.
DRD has n exams. They are all hard, but their difficulties are different. DRD will spend at least ri hours on the i -th course before its exam starts, or he will fail it. The i -th course's exam will take place ei hours later from now, and it will last for li hours. When DRD takes an exam, he must devote himself to this exam and cannot (p)review any courses. Note that DRD can review for discontinuous time.
So he wonder whether he can pass all of his courses.
No two exams will collide.
There are T cases following. In each case, the first line contains an positive integer n≤105 , and n lines follow. In each of these lines, there are 3 integers ri,ei,li , where 0≤ri,ei,li≤109 .
2 3 3 2 2 5 100 2 7 1000 2 3 3 10 2 5 100 2 7 1000 2
Case #1: NO Case #2: YES
题意:
有n门考试,每场考试有需要复习的时间,开始时间及持续时间。可以在非连续的时间复习,考试时不能复习。问能否通过全部的考试。
思路:
此题为贪心算法,先复习将要最早需要考试的科目。(通过快速排序,对考试时间进行一级排序,对复习时间进行二级排序),判断该科目是否能够通过两个条件:
1、当前时间+复习时间>考试时间,说明不足以通过考试
2、当前时间>考试时间,说明错过考试
细节:
每次课程顺利通过之后,当前时间应该为某一门的考试时间+考试时长!
代码:
#include<bits/stdc++.h>
using namespace std;
struct test
{
int r,e,l;
};
int comp(const test &a,const test &b)//e小时后开始考试,r是需要的时间,l是考试的时间!
{
if(a.e<b.e) return 1;
if(a.e>b.e) return 0;
if(a.r<b.r) return 1;
}
int main()
{
int t,n,i,j,num=0;;
test te[100000];
cin>>t;
while(t--)
{
memset(te,0,sizeof(te));
num++;
cin>>n;
int flag=0;
for(i=0;i<n;i++)
{
cin>>te[i].r>>te[i].e>>te[i].l;;
if(te[i].r>te[i].e)//若需要复习时间大于考试时间则不能通过
flag=1;
}
if(flag==0)
{
int time=te[0].r+te[0].l;
sort(te,te+n,comp);
for(i=1;i<n;i++)
{
if(te[i].e>=time+te[i].r)//考试时间大于复习时间
time+=(te[i].r+te[i].l);//当前时间为考试时间和考试时长之后
else
{
flag=1;
break;
}
}
}
if(flag==1)
cout<<"Case #"<<num<<": NO"<<endl;
else
cout<<"Case #"<<num<<": YES"<<endl;
}
return 0;
}