Description:
给出n天的空教室数量r[i],和m个使用教室的订单,数量d[i]、开始时间s[i]、结束时间t[i](包括端点),按照订单的次序分配教室。是否能完全满足订单,如果不能,求订单编号。
Analysis:
二分答案,满足单调性:如果第k个订单不能满足,那么k之后的订单也不能满足。
差分与前缀和:diff 差分,sum求和,表示第 i 天需要的教室数量,和r进行比较
Code
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int MAX_N = 1000010;
int d[MAX_N],s[MAX_N],t[MAX_N],r[MAX_N],n,m;
int diff[MAX_N],sum[MAX_N];
bool C(int x)
{
memset(diff,0,sizeof(diff));
for(int i = 1;i <= x;++i)
{
diff[s[i]] += d[i];
diff[t[i] + 1] -= d[i];
}
for(int i = 1;i <= n;++i)
{
sum[i] = sum[i-1] + diff[i];
if(sum[i] > r[i]) return 0;
}
return 1;
}
void solve()
{
if(C(m)){
printf("0");
return;
}
int lb = 1,ub = m;
while(lb < ub)
{
int mid = (lb + ub)/2;
if(C(mid)) lb = mid+1;
else ub = mid;
}
printf("-1
%d",lb);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i = 1;i <= n;++i)
{
scanf("%d",&r[i]);
}
for(int i = 1;i <= m;++i)
{
scanf("%d%d%d",&d[i],&s[i],&t[i]);
}
solve();
return 0;
}