题意

有\(n\)个严格升序的数,请你分成两个集合\(A\)和\(B\),其中一个集合任意两数之差不小于\(x\),另一集合任意两数之差不小于\(y\)。

问方案数,集合可以为空。

$n \le 10^5 $

传送门

思路

又是一道神仙\(dp\)

设\(dp_i\)表示当前\(B\)集合的最后一个数是\(a_i\)的方案数。

如果暴力转移就是:$$dp_i=\sum_{j<i & a_i-a_j\ge y}dp_j$$

并且满足区间\([j+1,i-1]\)能够放在\(A\)集合中

可以发现,满足条件的\(j\)是一个区间,因此前缀和优化,最后把答案累加起来就好了。

代码十分简短

#include <bits/stdc++.h>
const int N=100005,mu=1000000007;
int n,s[N],dp[N],l=0,r=0;
long long x,y,a[N];
int main(){
scanf("%d%lld%lld",&n,&x,&y);
for (int i=1;i<=n;i++) scanf("%lld",&a[i]);
if (x>y) std::swap(x,y);
for (int i=1;i+2<=n;i++)
if (a[i+2]-a[i]<x){
puts("0");return 0;
}
dp[0]=s[0]=1;
for (int i=1;i<=n;i++){
while (a[i]-a[r+1]>=y && r<i-1) r++;
if (l<=r){
if (l) dp[i]=(s[r]-s[l-1]+mu)%mu;
else dp[i]=s[r];
}
if (a[i]-a[i-1]<x) l=i-1;
s[i]=(s[i-1]+dp[i])%mu;
}
int ans=0;
for (int i=n;i>=0;i--){
ans=(ans+dp[i])%mu;
if (a[i+1]-a[i]<x && i<n) break;
}
printf("%d",ans);
}

后记

我好菜啊。以后写\(Atcoder \space dp\)的时候都可以加上思路的第一和最后一句了

AGC009C Division into Two的更多相关文章

  1. [AGC009C]Division into 2

    题意: 有一个长度为$N$的递增序列$S_i$,要把它分成$X,Y$两组,使得$X$中元素两两之差不小于$A$且$Y$中元素两两之差不小于$B$,求方案数 首先考虑$O\left(n^2\right) ...

  2. 【AGC009C】Division into Two

    [AGC009C]Division into Two 题面 洛谷 题解 首先有一个比较显然的\(n^2\)算法: 设\(f_{i,j}\)表示\(A\)序列当前在第\(i\)个,\(B\)序列当前在第 ...

  3. python from __future__ import division

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

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

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

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

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

  6. POJ 3140 Contestants Division 树形DP

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

  7. 暴力枚举 UVA 725 Division

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

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

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

  9. Leetcode: Evaluate Division

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

随机推荐

  1. MongoDB知识小结

    一.术语 RDBMS MongoDB 数据库 数据库 表格 集合 行 文档 列 字段 表联合 嵌套文档 主键 主键 (MongoDB 提供了 key 为 _id ) 数据库 数据库名可以是满足以下条件 ...

  2. GNU,GPL与自由软件

    GNU 是 Richard Stallman(理查德·斯托曼)创建的一个项目,not unix GPL(General Public License),GNU通用公共许可证.书面上的协议 自由软件与开 ...

  3. js的数据类型、函数、流程控制及变量的四种声明方式

    运算符 基本运算符 加 + 减 - 乘 * 除 / 取余 % 自增 ++ eg: 1++ 或 ++1 自减 -- eg: 1-- 或 --1 注:++或--写在前面表示优先级最高,先进行自增或者自减 ...

  4. 结对编程作业(python实现)

    一.Github项目地址:https://github.com/asswecanfat/git_place/tree/master/oper_make 二.PSP2.1表格: PSP2.1 Perso ...

  5. YII2 实现dropDownList 联动事件

    一.视图中 <div class="main-form"> <?php $form = ActiveForm::begin(); ?> <?= $fo ...

  6. redis集群1

    redis-trib.rb命令详解   redis-trib.rb是官方提供的Redis Cluster的管理工具,无需额外下载,默认位于源码包的src目录下,但因该工具是用ruby开发的,所以需要准 ...

  7. 操作系统 (OS)

    1. 操作系统(Operation System,OS) 操作系统作为接口的示意图 没有安装操作系统的计算机,通常被称为 裸机 如果想在 裸机 上运行自己所编写的程序,就必须用机器语言书写程序 如果计 ...

  8. CentOS7安装Oracle11g数据库

    1.关闭防火墙systemctl stop firewalled servicesystemctl disable firewalled service 2.关闭selinuxvim /etc/sel ...

  9. TLS1.3&TLS1.2形式化分析(二)

    1.下面是TLS1.2和TLS1.3握手协议过程 ,明显的可以看出存在不同 . 我们先说TLS1.2的握手过程明显是比TLS1.3的握手过多了一次.在TLS1.3中舍弃了之前RSA的协商过程,然后基于 ...

  10. Django中使用xadmin作为后台管理页面

    xadmin后台管理 安装:luffy虚拟环境下 # >: pip install https://codeload.github.com/sshwsfc/xadmin/zip/django2 ...