题目大意:一个人要去考试,一门考试要花r小时准备,e小时后考,考试持续l小时,给出一组数据,问能不能完成所有的考试?
算法分析:这是个水题,按照先考先复习的思路,按照考试时间排序,只要r>e就输出NO,否则的话就把下一轮考试的时间减去此次考试的e+l,再循环判断。
实现代码如下:
#include <iostream> #include <algorithm> using namespace std; #define MAX 100005 struct Exam { int r, e, l; bool operator<(const Exam &ex) const { return e < ex.e; } } a[MAX]; int n; int main() { //freopen("1.txt","r",stdin); int t; cin >> t; int cas = 0; while (t--) { int flag = 1; cin >> n; for (int i = 0; i < n; ++i) scanf("%d%d%d", &a[i].r, &a[i].e, &a[i].l); sort(a,a+n); for (int j = 0; j < n; ++j) { if (a[j].r > a[j].e) { printf("Case #%d: NO\n", ++cas); flag = 0; } else { a[j+1].e -= a[j].e + a[j].l; } } if (flag) printf("Case #%d: YES\n",++cas); } }