题目大意

给定n个不同的整数,求将它们分成两个集合X,Y,并且X集合中任意两个数的差>=A,Y集合中任意两个数的差>=B的方案数。

样例输入

5 3 7

1

3

6

9

12

样例输出

5

解析

不妨设\(A>B\),那么考虑如何动态规划。设\(f[i]\)表示第一个集合最后选择的数是i时的方案数。只用枚举第一个集合前一个选的数是哪一个即可转移。但 这么做是\(O(n^2)\)的。考虑从能够转移的点的性质出发。

对于能够转移到i的j,必须要满足的条件有

  • \(S_i-S_j >= A\)
  • 对于\([j+1,i-1]\)中的数,满足任意两个数\(x,y\)都有\(S_y-S_x>=B\)

可以发现满足条件的j是一段连续位置。因此采用前缀和优化即可。

代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#define int long long
#define N 100002
using namespace std;
const int mod=1000000007;
int n,a,b,i,m[N],sum[N],f[N];
int read()
{
char c=getchar();
int w=0;
while(c<'0'||c>'9') c=getchar();
while(c<='9'&&c>='0'){
w=w*10+c-'0';
c=getchar();
}
return w;
}
signed main()
{
n=read();a=read();b=read();
if(a>b) swap(a,b);
for(i=1;i<=n;i++) m[i]=read();
sort(m+1,m+n+1);
for(i=3;i<=n;i++){
if(m[i]-m[i-2]<a){
puts("0");
return 0;
}
}
int l=0,r=0,ans=0;
sum[0]=f[0]=1;
for(i=1;i<=n;i++){
while(r<i-1&&m[i]-m[r+1]>=b) r++;
if(l<=r) f[i]=(sum[r]-sum[l-1]+mod)%mod;
sum[i]=(sum[i-1]+f[i])%mod;
if(i>1&&m[i]-m[i-1]<a) l=i-1;
}
for(i=n;i>=0;i--){
ans=(ans+f[i])%mod;
if(i<n&&m[i+1]-m[i]<a) break;
}
printf("%lld\n",ans);
return 0;
}

[Atcoder2292] Division into Two的更多相关文章

  1. python from __future__ import division

    1.在python2 中导入未来的支持的语言特征中division(精确除法),即from __future__ import division ,当我们在程序中没有导入该特征时,"/&qu ...

  2. [LeetCode] Evaluate Division 求除法表达式的值

    Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...

  3. 关于分工的思考 (Thoughts on Division of Labor)

    Did you ever have the feeling that adding people doesn't help in software development? Did you ever ...

  4. POJ 3140 Contestants Division 树形DP

    Contestants Division   Description In the new ACM-ICPC Regional Contest, a special monitoring and su ...

  5. 暴力枚举 UVA 725 Division

    题目传送门 /* 暴力:对于每一个数都判断,是否数字全都使用过一遍 */ #include <cstdio> #include <iostream> #include < ...

  6. GDC2016【全境封锁(Tom Clancy's The Division)】对为何对应Eye Tracked System,以及各种优点的演讲报告

    GDC2016[全境封锁(Tom Clancy's The Division)]对为何对应Eye Tracked System,以及各种优点的演讲报告 原文 4Gamer編集部:松本隆一 http:/ ...

  7. Leetcode: Evaluate Division

    Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...

  8. hdu 1034 (preprocess optimization, property of division to avoid if, decreasing order process) 分类: hdoj 2015-06-16 13:32 39人阅读 评论(0) 收藏

    IMO, version 1 better than version 2, version 2 better than version 3. make some preprocess to make ...

  9. uva 725 Division(暴力模拟)

    Division 紫书入门级别的暴力,可我还是写了好长时间 = = [题目链接]uva 725 [题目类型]化简暴力 &题解: 首先要看懂题意,他的意思也就是0~9都只出现一遍,在这2个5位数 ...

随机推荐

  1. 【机器学习】Learning to Rank之Ranking SVM 简介

    Learning to Rank之Ranking SVM 简介 排序一直是信息检索的核心问题之一,Learning to Rank(简称LTR)用机器学习的思想来解决排序问题(关于Learning t ...

  2. 【VS开发】MFC运行时库与debug、release版本之间的配置关系

    参考内容: 前段时间从网上下来一个有意思的代码,用VS2010打开时需要将工程转换为2010的工程,转化后却出现了编译不通过的问题,类似这样的错误:c:\program files\microsoft ...

  3. Docker守护进程

    Docker安装完成之后, 需要确定Docker的守护进程是否已经运行. Docker是使用root 权限运行他的程序,进而可以处理普通用户无法完成的操作(比如挂载文件系统). docker程序是Do ...

  4. powerdesigner去掉网格线

    powerdesigner去掉网格线 去掉网格线

  5. Dubbo消费方服务调用过程源码分析

    参考:dubbo消费方服务调用过程源码分析dubbo基于spring的构建分析Dubbo概述--调用过程dubbo 请求调用过程分析dubbo集群容错机制代码分析1dubbo集群容错策略的代码分析2d ...

  6. 搭建springCloud网关zuul

    一.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www ...

  7. 在Powershell中使用Group-Object和-GroupBy

    使用Group-Object(group)按组统计 PS C:\> Get-Command -Module Microsoft.PowerShell.LocalAccounts | group ...

  8. eclipse 保存web.xml 和 loading description from 问题的解决

    Eclipse 版本为 2019-06 (4.12.0) 发现开启的时候一直有loading description from ***  ,这个loading description 是web项目加载 ...

  9. Java中字符串排序

    package com.fs.test; import java.util.ArrayList; import java.util.Collections; import java.util.List ...

  10. 关于mail mailx 以及sendmail 的理解

    最近在弄邮件告警相关的东西,接触到了mail这一块,但是发送邮件的时间看到网上的用法 yum install mailx sednmail -y 这一块很迷糊 所以决定自己研究下 首先套用官话解释: ...