Borrow the classroom dichotomous answer + prefix sum

Link: https://ac.nowcoder.com/acm/problem/16564
Source: Niuke.com

在大学期间,经常需要租借教室。大到院系举办活动,小到学习小组自习讨论,都需要向学校申请借教室。教室的大小功能不同,借教室人的身份不同,借教室的手续也不一样。

面对海量租借教室的信息,我们自然希望编程解决这个问题。

我们需要处理接下来n天的借教室信息,其中第i天学校有ri个教室可供租借。共有m份订单,每份订单用三个正整数描述,分别为dj, sj, tj,表示某租借者需要从第sj天到第tj天租借教室(包括第sj天和第tj天),每天需要租借dj个教室。

我们假定,租借者对教室的大小、地点没有要求。即对于每份订单,我们只需要每天提供dj个教室,而它们具体是哪些教室,每天是否是相同的教室则不用考虑。

借教室的原则是先到先得,也就是说我们要按照订单的先后顺序依次为每份订单分配教室。如果在分配的过程中遇到一份订单无法完全满足,则需要停止教室的分配,通知当前申请人修改订单。这里的无法满足指从第sj天到第tj天中有至少一天剩余的教室数量不足dj个。

现在我们需要知道,是否会有订单无法完全满足。如果有,需要通知哪一个申请人修改订单。

For 10% of the data, 1≤n, m≤10;
for 30% of the data, 1≤n, m≤1000;
for 70% of the data, 1≤n, m≤105;
for 100% of the data , There are 1≤n, m≤106, 0≤ri, dj≤109, 1≤sj≤tj≤n.

Ideas

Take the first few days as the interval subscript. The data range of m, n, sj and tj is 10^6. You must use the prefix and complete the operation on the interval, otherwise it will definitely time out.

It is not possible to judge one order by one order, so it will be t. It can be divided into two orders to judge whether there are orders that cannot be satisfied for x orders. Each check() uses the prefix and the interval to process, and quickly completes the interval operation of the first x orders.

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll pro[1200000];
ll day[1200000];
ll cpy[1200000];
ll n,m;
typedef struct node{
    
    
    ll l,r;
    ll room;
}Gnode;
Gnode num[1200000];
ll min(ll x,ll y){
    
    
    return x < y? x:y;
}
bool check(ll x){
    
    
    for(int i = 1;i <= n;i++)
        cpy[i] = day[i];
    for(int i = 1;i <= x;i++){
    
    
        cpy[num[i].l] -= num[i].room;
        cpy[num[i].r+1] += num[i].room; 
    }
    for(int i = 2;i <= n;i++){
    
    
        cpy[i] += cpy[i-1];
        if(cpy[i] < 0)
            return true;
    }
    return false;
       
}
int main(){
    
    
    ll  ans = 0x3f3f3f3f;
    cin >> n >> m;
    for(int i = 1;i <= n;i++)
        cin >> day[i];
    for(int i = n;i > 1;i--)
        day[i] = day[i] - day[i-1];
    for(int i = 1;i <= m;i++)
        cin >> num[i].room >> num[i].l >> num[i].r;
    ll left,right,middle;
    left = 1;right = n;
    middle = (left + right) >> 1;
    while(left <= right){
    
    
        if(check(middle)){
    
    
            ans = min(ans,middle);
            right = middle -1;
        }else left = middle + 1;
        middle = (left + right) >> 1;
            
    }
    if(ans != 0x3f3f3f3f)
        cout << -1 << "\n" << ans;
    else cout << 0 ;
    return 0;
}

Guess you like

Origin blog.csdn.net/RunningBeef/article/details/113998501