P1083 借教室(差分)

题意:

LP1083

思路:差分数组直接求出在那一天教室不够用,然后On求出那个人要求教室太多。总体O(n)的复杂度

#include 
#define ll long long
using namespace std;
const int N = 1e6 + 10;
int n, m, a[N];
ll p[N];
struct node {
    int d, s, t;
} no[N];
int main() {
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    for(int i = 1; i <= m; i++) {
        scanf("%d%d%d", &no[i].d, &no[i].s, &no[i].t);
        p[no[i].s] += no[i].d, p[no[i].t + 1] -= no[i].d;
    }
    ll now = 0;
    for(int i = 1; i <= n; i++) {
        now += p[i];
        p[i] = now;
    }
    int f = 0;
    for(int i = 1; i <= n; i++)
        if(p[i] > a[i]) {
            f = i;
            break;
        }
    if(f)
        for(int i = 1; i <= m; i++)
            if(no[i].s <= f && no[i].t >= f) {
                a[f] -= no[i].d;
                if(a[f] < 0) {
                    f = i;
                    break;
                }
            }
    if(f)
        printf("-1\n%d", f);
    else
        printf("0");
    return 0;
}

 

你可能感兴趣的:(思维)