JZOJ4307. 【NOIP2015模拟11.3晚】喝喝喝

这里写图片描述

分析

题目的意思就是要求不包含某些数对(坏对)连续的区间个数。
所以我们就应该先考虑如何快速找到一个坏对。
我们从后向前枚举,
一开始设右端点为n,枚举到一个数,我们用 n 的时间来枚举这个数的约数,
通过一个桶来判断与这个数形成坏对的位置,
之后改变右端点。
总的时间复杂度 O(nn)

code

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string.h>
#include <cmath>
#include <math.h>
#define N 1000003
#define ll long long
using namespace std;
int n,k,a[N],l,r,h[N],t,mx,w;
ll ans;
char ch;
void write(ll x)
{
     if(x>9) write(x/10);
     putchar(x%10+'0');
}
void read(int &n)
{
    n=0;
    ch=getchar();
    while((ch<'0' || ch>'9') && ch!='-')ch=getchar();
    int w=1;
    if(ch=='-')w=-1,ch=getchar();
    while('0'<=ch && ch<='9')n=n*10+ch-'0',ch=getchar();
    n*=w;
}
int main()
{
    freopen("drink.in","r",stdin);
    freopen("drink.out","w",stdout);
    memset(h,127,sizeof(h));
    read(n);read(k);
    for(int i=1;i<=n;i++)read(a[i]);
    r=n;
    for(int i=n;i;i--)
    {
        t=a[i]-k;
        if(t>=0)
        {
            for(int j=1;j<=sqrt(t);j++)
                if(t%j==0)
                {
                    if(j>k && h[j]<=n)r=min(r,h[j]);
                    if(t/j>k && h[t/j]<=n)r=min(r,h[t/j]);
                }
            if(t==0 && mx>k)r=min(w-1,r);
        }
        h[a[i]]=i-1;
        if(a[i]>k)
        {
            mx=a[i];
            w=i;
        }
        ans=ans+r-i+1;  
    }
    write(ans);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值