P1083 classroom noip by 2012 to improve the group semi-finals

After reading about topics we think will be the solution of violence

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int N=1e6+5;
int sum[N],d,r[N],s,t,n,m;
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,&s,&t);
        for(int j=s;j<=t;j++)
        {
            sum[j]=sum[j]+d;
        if(r[j]<sum[j])
        {
            cout<<"-1"<<endl<<i;
            return 0;
         }
        }
    }
    cout<<"0";
    return 0;
}

If you can mix 45 minutes in the examination room, and that is good.

Differential Array


Set a diff [] array.

So what is the difference array it:

With diff [i] records the current item on one of the difference: diff [i] = a [i] -a [i-1];

So if we know the diff [0 ... i], you can find the a [i]

Where is it powerful is that it?

If we want a [l], a [l + 1] ... a [r] value at the same time plus or minus a number, you can do so

diff[ l ]+=x;

diff[ r+1 ]-=x;

Because for this series, only two adjacent difference change. Can be O (1) modified.

For this question

If you want to check whether the first to i-th orders have to be modified,

Then it so according to the above said method, the d [1] to d [i] is applied to the corresponding one interval.

bool check(int per)
{
    memset(diff,0,sizeof(diff));
    for(int i=1;i<=per;i++)
    {
        diff[s[i]]+=d[i];
        diff[t[i]+1]-=d[i];
    }
    for(int i=1;i<=n;i++)
    {
        need[i]=need[i-1]+diff[i];
        if(need[i]>r[i]) return true;
    }
    
    return false;
}

所以我们就从1到n跑一遍check就可以了对不对?

别急,先看看能不能二分。

这道题里,如果第 i 份订单需要修改,后面的订单也无法满足。满足单调性。

那么就可以二分了,节约大把时间。

while(l<=r)
    {
        mid=(l+r)>>1;
        if(check(mid))//找到一个可行解,保存下来,看看有没有更优的(更靠前 
        {
            ans=mid;
            r=mid-1;
        }
        else l=mid+1;
    }

本文结束,谢谢大佬

顾z 的个人中心 - 洛谷 | 计算机科学教育新生态
https://www.luogu.org/user/87960

_皎月半洒花 的个人中心 - 洛谷 | 计算机科学教育新生态
https://www.luogu.org/user/28313

Guess you like

Origin www.cnblogs.com/Neptune0/p/11844345.html