AGC009C Division into Two
题意
有\(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的更多相关文章
- [AGC009C]Division into 2
题意: 有一个长度为$N$的递增序列$S_i$,要把它分成$X,Y$两组,使得$X$中元素两两之差不小于$A$且$Y$中元素两两之差不小于$B$,求方案数 首先考虑$O\left(n^2\right) ...
- 【AGC009C】Division into Two
[AGC009C]Division into Two 题面 洛谷 题解 首先有一个比较显然的\(n^2\)算法: 设\(f_{i,j}\)表示\(A\)序列当前在第\(i\)个,\(B\)序列当前在第 ...
- 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 ...
随机推荐
- .NET Core 使用ModelBinder去掉所有参数的空格
一.前言 通过各种姿势搜索都没搜到这方面的,唯一找到一个比较符合的,但是只适合简单类型,而且代码还没贴全,心累.. 然后查看官网和源码之后,发现继承并实现 IModelBinder和IModelBin ...
- IP-reputation feature
IP-reputation feature https://blog.norz.at/citrix-netscaler-ip-reputation-feature/ I recently had to ...
- kong网关命令(一)
上次在虚拟机里安装kong网关后,因为版本(1.4)太高,目前Kong Dashboard无法支持, 后续发现Git上有个开源工具Kong admin ui,下载源码并部署到NGINX. 但是发现使用 ...
- C++ STL 之 常用算法
#include <iostream> #include <vector> #include <algorithm> using namespace std; // ...
- 完整的ELK+filebeat+kafka笔记
之前有写过elasticsearch集群和elk集群的博客, 都是基于docker的,使用docker-compose进行编排(K8S暂未掌握) 三台服务器搭建es集群:https://www.cnb ...
- bash基础——grep、基本正则表达式、扩展正则表达式、fgrep
grep grep全称:Globally search a Regular Expression and Print 全局搜索正则表达式 正规表达式本质上是一种"表示方法", 只要 ...
- Linux命令——cat、more、less、head、tail
cat 一次显示整个文件 -n:显示行号 -b :和 -n 相似,只不过对于空白行不编号 -s:当遇到有连续两行以上的空白行,就代换为一行的空白行 -E显示换行符 [root@localhost ~] ...
- Python一些细节
1.python set() dict() 有序问题,不同版本之间的差异,与Java/C++的对比 https://www.cnblogs.com/niuxichuan/p/11608386.html ...
- pandas库介绍之DataFrame基本操作
怎样删除list中空字符? 最简单的方法:new_list = [ x for x in li if x != '' ] 今天是5.1号. 这一部分主要学习pandas中基于前面两种数据结构的基本操作 ...
- Nginx 配置参数中文说明
Nginx配置参数中文详细说明: #定义Nginx运行的用户和用户组 user www www; # #nginx进程数,建议设置为等于CPU总核心数. worker_processes ; # #全 ...