JZOJ5361. 【NOIP2017提高A组模拟9.14】捕老鼠

87 篇文章 0 订阅
30 篇文章 0 订阅

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

Data Constraint

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

题解

稍微转化一下,题目的意思就是:
已知一些线段,求用最少的线段覆盖1~n。
很容易想到一个dp
fi 表示在前i个的位置都已经被覆盖完了,
转移显然。

简单优化一下就可以了。

code

#include<queue>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include <cstring>
#include <string.h>
#include <cmath>
#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=n*10+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(opx==1)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]);
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值