【中石油OJ-5623】 Division into Two

题目描述

There is a set consisting of N distinct integers. The i-th smallest element in this set is Si. We want to divide this set into two sets, X and Y, such that:

The absolute difference of any two distinct elements in X is A or greater.
The absolute difference of any two distinct elements in Y is B or greater.
How many ways are there to perform such division, modulo 109+7? Note that one of X and Y may be empty.

Constraints
All input values are integers.
1≤N≤105
1≤A,B≤1018
0≤Si≤1018(1≤i≤N)
Si<Si+1(1≤i≤N−1)

输入

The input is given from Standard Input in the following format:

N A B
S1
:
SN

输出

Print the number of the different divisions under the conditions, modulo 109+7.

样例输入


5 3 7

1

3

6

9

12

样例输出

5

提示

There are five ways to perform division:

X={1,6,9,12}, Y={3}
X={1,6,9}, Y={3,12}
X={3,6,9,12}, Y={1}
X={3,6,9}, Y={1,12}
X={3,6,12}, Y={1,9}


      看到两个集合,后面比前面大第一个就想到的dp,设一个a和b数组,a[i]代表的是第i个数在a集合中的情况,b[i]代表的是第i个数在b集合中的情况,ru1和ru2是伪指针,指向前面的那个和第i个数分别符合a集合和b集合条件的数,
比如s[ru1]的话就说明要么他是第一个数,要么他和s[i]的差值就是a数组要求的那个数,首先输入数,然后按照大小排序,从第一个开始,以放入a集合为例,如果是s[i-1]符合条件,那么从a[i-1]和b[i-1]到来的结果都符合条件,
因为如果a[i-1]符合条件,那么b[i-1]最近的a也是i-2,而b集合对a集合不造成影响,所以如果s[i-1]符合那么就是a[i-1]+b[i-1],而如果符合的不是他前一个,那么他就必须把中间的那些数全都放入另一个集合,否则这个集合的
条件就会被破坏,而这样的话就无法肯定他下面的b也可以用,因为这个b和后面的b可能不能同时放入,但是他后一位的b肯定是包括了前面可以进入这一条线的所有情况,所以直接a[i]=b[ru1+1]即可,否则就说明这个数根本就不
能被放进来,直接得0,最后就是完全没有符合的,就说明全都在另一个集合里,直接把另一个集合的数赋值过来就可以了。


PS:如果中间有间隔,那么中间需要确定的是必须要中间能走通才可以被取用,所以我设置一个pan数组,1是a1可以连续取用的,2是b1可以连续取用的,如果前后相同就说明两者在同一段里,可以连续放入集合,即可以形成通路。

代码:

#include<stdio.h>
#include<algorithm>
#define LL long long
#define lowbit(x) x&-x
using namespace std;
const int MAXN = 100010;
const int inf = 0x3f3f3f3f;
const double eps = 1e-9;
const int mo = 1e9+7;
LL a[100005];LL b[100005];LL pan1[100005];LL pan2[100005];LL s[100005];
int main()
{
    int n;LL a1,b1;
    while(~scanf("%d%lld%lld",&n,&a1,&b1)){
        int ru1=1,ru2=1;pan1[1]=1;pan2[1]=1;
        for(int i=1;i<=n;i++){
            scanf("%lld",&s[i]);
        }
        sort(s+1,s+n+1);
        for(int i=2;i<=n;i++){
            if(s[i]-s[i-1]>=a1)pan1[i]=ru1;
            else{ru1++;pan1[i]=ru1;}
            if(s[i]-s[i-1]>=b1)pan2[i]=ru2;
            else{ru2++;pan2[i]=ru2;}
        }
        a[1]=1;b[1]=1;ru1=1;ru2=1;
        for(int i=2;i<=n;i++){
            while(s[i]-s[ru1]>=a1){
                ru1++;
            }if(s[i]-s[ru1]<a1&&ru1>1)ru1--;
            while(s[i]-s[ru2]>=b1){
                ru2++;
            }if(s[i]-s[ru2]<b1&&ru2>1)ru2--;
            if(s[i]-s[ru1]>=a1){
                if(ru1==i-1)a[i]=(a[i-1]+b[i-1])%mo;
                else if(pan2[ru1+1]==pan2[i-1])a[i]=b[ru1+1];
                else a[i]=0;
            }
            else if(pan2[ru1]==pan2[i-1])a[i]=b[ru1];
            else a[i]=0;
            if(s[i]-s[ru2]>=b1){
                if(ru2==i-1)b[i]=(a[i-1]+b[i-1])%mo;
                else if(pan1[ru2+1]==pan1[i-1]){b[i]=a[ru2+1];}
                else b[i]=0;
            }
            else if(pan1[ru2]==pan1[i-1])b[i]=a[ru2];
            else b[i]=0;
        }
        printf("%lld\n",(a[n]+b[n])%mo);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值