[Atcoder2292] Division into Two
题目大意
给定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的更多相关文章
- python from __future__ import division
1.在python2 中导入未来的支持的语言特征中division(精确除法),即from __future__ import division ,当我们在程序中没有导入该特征时,"/&qu ...
- [LeetCode] Evaluate Division 求除法表达式的值
Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...
- 关于分工的思考 (Thoughts on Division of Labor)
Did you ever have the feeling that adding people doesn't help in software development? Did you ever ...
- POJ 3140 Contestants Division 树形DP
Contestants Division Description In the new ACM-ICPC Regional Contest, a special monitoring and su ...
- 暴力枚举 UVA 725 Division
题目传送门 /* 暴力:对于每一个数都判断,是否数字全都使用过一遍 */ #include <cstdio> #include <iostream> #include < ...
- GDC2016【全境封锁(Tom Clancy's The Division)】对为何对应Eye Tracked System,以及各种优点的演讲报告
GDC2016[全境封锁(Tom Clancy's The Division)]对为何对应Eye Tracked System,以及各种优点的演讲报告 原文 4Gamer編集部:松本隆一 http:/ ...
- Leetcode: Evaluate Division
Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...
- 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 ...
- uva 725 Division(暴力模拟)
Division 紫书入门级别的暴力,可我还是写了好长时间 = = [题目链接]uva 725 [题目类型]化简暴力 &题解: 首先要看懂题意,他的意思也就是0~9都只出现一遍,在这2个5位数 ...
随机推荐
- hbase的架构组成+hbase在create报错 -hue - mvn
0.hbase的组件 架构 参考:https://cloud.tencent.com/developer/article/1084209 各个组件的功能 参考:https://zhuanlan.zhi ...
- Django2.2 Mysql数据表一对一映射(字段绑定) --Model拓展学习笔记shen
我:本次继上次的Model继续延伸一下数据表之间的一对一映射关系 访客:有什么用? 我:众所周知,一张数据表的字段太多会显得冗余.杂乱,那么就需要将一张表进行拆分成多个表,这样不会太杂乱,也比较好维护 ...
- CDH安装前系统优化准备
参考: https://www.cnblogs.com/yinzhengjie/p/10367447.html https://www.sysit.cn/blog/post/sysit/CDH6.2. ...
- Oracle中ORA-01113,ORA-01110的简单解决
分析和解决问题: 1.重起数据库: C:\Documents and Settings\Jacken>sqlplus /nologSQL> conn sys/123456 as sysdb ...
- (public丶private丶protected) 的理解
public(公有):公有的类成员可以在任何地方被访问. protected(受保护):受保护的类成员则可以被其自身以及其子类和父类访问. private(私有):私有的类成员则只能被其定义所在的类访 ...
- 2019牛客暑期多校训练营(第五场)G - subsequeue 1 (一题我真的不会的题)
layout: post title: 2019牛客暑期多校训练营(第五场)G - subsequeue 1 (一题我真的不会的题) author: "luowentaoaa" c ...
- py2 json字符串转字典去掉前缀u
def unicode_convert(input): if isinstance(input, dict): return {unicode_convert(key): unicode_conver ...
- 牛客 545A 小A与最大子段和 & CF 660F Bear and Bowling 4
大意: 给定序列$a$, 求选择一个子区间$[l,r]$, 使得$\sum\limits_{i=l}^r(i-l+1)a_i$最大. $n\le2e5, |a_i|\le 1e7$. 记$s[i]=\ ...
- mysql的索引为什么要使用B+树而不是其他树?
总结 1.InnoDB存储引擎的最小存储单元是页,页可以用于存放数据也可以用于存放键值+指针,在B+树中叶子节点存放数据,非叶子节点存放键值+指针. 2.索引组织表通过非叶子节点的二分查找法以及指针确 ...
- 平时工作使用到的idea快捷键或者技巧
平时工作使用到的idea快捷键或者技巧 alt+enter 快速导入包 alt+insert 快速生成setter和getter ctrl+alt+l 格式化代码 /**然后回车 快速生成文档注释 a ...