【題解】Codeforces 1304C. Air Conditioner

【題目敘述】http://codeforces.com/contest/1304/problem/C

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
 
struct c{
    int t, l, h;
};
bool cmp(c x, c y){
    return x.t < y.t;
}
int q, n, m, mn, mx, T, L, H, pre;
bool flag;
vector <c> v;
 
int main() {
    cin >> q;
    while (q--){
        cin >> n >> m;
        v.clear();
        for (int i = 0; i < n; i++){
            cin >> T >> L >> H;
            v.push_back({T, L, H});
        }
        sort(v.begin(), v.end(), cmp);
        mn = m;
        mx = m;
        pre = 0;
        flag = true;
        for (int i = 0; i < n; i++){
            if (mn-(v[i].t-pre) > v[i].h || mx+(v[i].t-pre) < v[i].l){
                flag = false;
                break;
            }
            mn = max(mn-(v[i].t-pre), v[i].l);
            mx = min(mx+(v[i].t-pre), v[i].h);
            pre = v[i].t;
        }
        if (flag) cout << "YES\n";
        else cout << "NO\n";
    }
}
分享本文 Share with friends