JZOJ5431 捕老鼠

JZOJ 5341

Description

为了加快社会主义现代化,建设新农村,农夫约(Farmer Jo)决定给农庄里的仓库灭灭鼠。于是,猫被农夫约派去捕老鼠。 猫虽然擅长捕老鼠,但是老鼠们太健美了,身手敏捷,于是猫想到了一个绝妙的办法:它决定点燃纯艾条,用烟熏老鼠。 农夫约的农庄里有N 个仓库,排成了一排,编号为1~N。

假设猫在第i 个仓库点燃艾条,烟雾就会充满该仓库,并向左右扩散Ai的距离,接着所有|i-j|<=Ai 的仓库j 的老鼠被消灭。 猫是一只爱护空气环境的好猫,它希望知道最少需要多少支艾条,才可以消灭所有老鼠。

Input

第一行:一个正整数,代表N。
第二行:N 个非负整数,第i 个数代表Ai。

Output

第一行:一个整数,代表答案。

Sample Input

10
2 0 1 1 0 3 1 0 2 0

Sample Output

3

Hint

Data Constraint

20%的数据:N <= 20
60%的数据:N <= 10^3
100%的数据:N <= 5*10^5,Ai <= N

c++

include

include

include

include

include

include <string.h>

include

include <math.h>

define ll long long

define N 500005

define db double

define P putchar

define G getchar

define mo 1000000007

using namespace std;
char ch;
void read(int &n)
{
n=0;
ch=G();
while((ch<'0' || ch>'9') && ch!='-')ch=G();
ll w=1;
if(ch=='-')w=-1,ch=G();
while('0'<=ch && ch<='9')n=n10+ch-'0',ch=G();
n
=w;
}

int max(int a,int b){return a>b?a:b;}
int min(int a,int b){return a<b?a:b;}

void write(int x)
{
if(x>9) write(x/10);
P(x%10+'0');
}

struct node
{
int x,l,r;
}t[4*N];

int n,f[N],a[N],opl,opr,x,opx,ops;

void make(int x,int l,int r)
{
t[x].l=l;t[x].r=r;
t[x].x=2147483647;
if(l==r)return;
int m=(l+r)/2;
make(x+x,l,m);
make(x+x+1,m+1,r);
}

void work(int x)
{
if(t[x].l>=opl && t[x].r<=opr)
{
if(opx1)t[x].x=min(t[x].x,ops);
if(opx
2)ops=min(ops,t[x].x);
return;
}
if(t[x].l==t[x].r)return;
int m=(t[x].l+t[x].r)/2;
if(opl<=m)work(x+x);
if(m<opr)work(x+x+1);

t[x].x=min(t[x+x].x,t[x+x+1].x);

}

int main()
{
freopen("cat.in","r",stdin);
freopen("cat.out","w",stdout);
read(n);
for(int i=1;i<=n;i++)
{
read(x);
if(i+x<=n)a[i+x]=max(a[i+x],2*x+1);else a[n]=max(a[n],n-(i-x)+1);
}

memset(f,127,sizeof(f));

make(1,1,n);
for(int i=1;i<=n;i++)
{
    opl=i-a[i];opr=i-1;
    if(opl<=0)f[i]=1;
    else
    {
        ops=f[i]-1;
        opx=2; 
        if(opl<=opr)work(1);
        f[i]=ops+1;
    }
    ops=f[i];opx=1;
    opl=opr=i;
    work(1);
}

write(f[n]);

}

原文地址:https://www.cnblogs.com/tham/p/7850902.html