NOIP2012----借教室

#include<stdio.h>

int r[1000005], d[1000005], s[1000005], t[1000005];
int c[1000005];
int n, m;
int now = 0;//记录当前已经算出的改变量
int check(int mid) {
    int i;
    if (now > mid) {//如果当前已经算出的改变量大于mid,则需要返回去
        for (i = mid+1; i <=now; i++) {//c用来记录每一天教师的改变量
            c[t[i] + 1] += d[i];
            c[s[i]] -= d[i];
        }
    }
    else {
        for (i = now + 1; i <= mid; i++) {
            c[t[i] + 1] -= d[i];
            c[s[i]] += d[i];
        }
    }
    now = mid;
    int sum = 0;
    for(i=1;i<=n;i++){
        sum += c[i];
        if (sum > r[i]) {//改变的即需要的教师多余那天可以提供的教室
            return 0;
        }
    }
    return 1;
}

int main() {
    scanf("%d %d", &n, &m);
    int i, j;
    for (i = 1; i <= n; i++) {
        scanf("%d", &r[i]);
    }
    for (i = 1; i <= m; i++) {
        scanf("%d %d %d", &d[i], &s[i], &t[i]);
    }
    int le, ri,mid;
    le = 1; ri = m;
    int ans = 0;
    while (le <= ri) {
        mid = (le + ri) / 2;
        if (check(mid)) {
            le = mid + 1;
        }
        else {
            ans = mid;
            ri = mid - 1;
        }
    }
    if (!ans) {
        printf("0");
    }
    else {
        printf("-1\n%d", ans);
    }
    return 0;
}

 

posted @ 2018-03-11 21:00  Carol淋  阅读(119)  评论(0编辑  收藏  举报