[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位数 ...
随机推荐
- [转帖]influxdb和boltDB简介——MVCC+B+树,Go写成,Bolt类似于LMDB,这个被认为是在现代kye/value存储中最好的,influxdb后端存储有LevelDB换成了BoltDB
influxdb和boltDB简介——MVCC+B+树,Go写成,Bolt类似于LMDB,这个被认为是在现代kye/value存储中最好的,influxdb后端存储有LevelDB换成了BoltDB ...
- 常用PostgreSQL HA(高可用)工具收集
PostgreSQL HA Collect: 1.pgpool 2.Pacemaker + Corosync 3.ecox 4.Patroni: A Template for PostgreSQL H ...
- Oracle-DDL 2- 视图&索引
DDL-数据定义语句: 二.视图 --视图(view),本身不保存数据,保存的是一个查询语句--对视图的操作等同于对查询语句中源数据的操作--视图占用存储空间较小,可以快速的对特定数据进行访问和操作- ...
- Hive怎么使用远程连接
HIVE的连接模式== 本地连接模式 直接启动hive命令 HIVE的远程连接 这里要启动HIVE的服务 thirft进行编写 hiveserver2 —- > 前台启动 后台启动 前台启动 h ...
- Mysql workbench 字段类型(转载)
转载自:https://blog.csdn.net/j_h_xie/article/details/52924521 项目初始,在使用workbench建表时,字段中有PK,NN,UQ,BIN,UN, ...
- 检测Python程序本身是否已经在运行
为runner.py实现一个函数,检测是否有其他的runner.py进程在正在执行? 除主要用到os模块,还用到了第三方模块psutil
- mysql行(记录)的详细的操作
目录 一 介绍 二 插入(增加)数据INSERT 三 更新(修改)数据UPDATE 四 删除数据DELETE 五 查询数据SELECT(重点) 六 权限管理 阅读目录 一 介绍 MySQL数据操作: ...
- 使用Python基于TensorFlow的CIFAR-10分类训练
TensorFlow Models GitHub:https://github.com/tensorflow/models Document:https://github.com/jikexueyua ...
- idea 设置自动生成注释
idea新建类注释规则 /** @ProjectName: ${PROJECT_NAME} @Package: ${PACKAGE_NAME} @ClassName: ${NAME} @Descrip ...
- Skills CodeForces - 613B (双指针)
大意: $n$门课, 第$i$门分数$a_i$, 可以增加共$m$分, 求$cnt_{mx}*cf+mi*cm$的最大值 $cnt_{mx}$为满分的科目数, $mi$为最低分, $cf$, $cm$ ...