你咯的定级真是雾……二分入门水题
前缀和维护一下借的天数,二分查找答案,就酱~!
#include<bits/stdc++.h> using namespace std; const int N = 1000050; int n,m; long long r[N],d[N],a[N]; int s[N],t[N]; bool check(int mid) { memset(a,0,sizeof(a)); for(int i = 1; i <= mid; i++){ //前缀和记录要借的 a[s[i]] += d[i]; a[t[i] + 1] -= d[i]; } for(int i = 1; i <= n; i++){ a[i] += a[i-1]; //前缀和处理完毕~?ヽ(°▽°)ノ? if(a[i] > r[i])return 0; } return 1; } signed main() { cin>>n>>m; for(int i = 1; i <= n; i++ )cin>>r[i]; for(int i = 1; i <= m; i++ )scanf("%d%d%d",&d[i],&s[i],&t[i]); if(check(m))printf("0"); else { int l = 1,r = m; while(l < r){ int mid = l + (r - l) / 2; if(check(mid)){ l = mid + 1; //根据mid定义可知 此时前段都能借到,那就向后查找 } else r = mid; }printf("-1 %d",l); } }