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 ...
随机推荐
- (二)XML基础(2)
三.解析 服务端解析 JDK: DOM SAX JAXB java and xml Binding 开源(一般都是用开源的) ...
- Java Web 修改请求参数
方法一.继承 HttpServletRequestWrapper , 实现自定义 request 1.除了修改的参数,其他 Header 等参数不变, 等同于修改了请求参数 2.实质是另一个请求 /* ...
- Spring Boot Redis 分布式缓存的使用
一.pom 依赖 <!-- 分布式缓存 --> <dependency> <groupId>org.springframework.boot</groupId ...
- jquery 获取滚动条高度
获取浏览器显示区域的高度 : $(window).height(); 获取浏览器显示区域的宽度 :$(window).width(); 获取页面的文档高度 :$(document).height(); ...
- SQL 不同服务器数据库操作
https://www.cnblogs.com/lusunqing/p/3660190.html --创建远程链接服务器 execute sys.sp_addlinkedserver @server= ...
- c#基础知识梳理(五)
上期回顾 - https://www.cnblogs.com/liu-jinxin/p/10831189.html 一.运算符重载 您可以重定义或重载 C# 中内置的运算符.因此,程序员也可以使用用户 ...
- Nginx与负载均衡
Nginx,首先是一款轻量级的Web服务器,其特点是占有内存少,并发能力强,大厂用户有:百度.新浪.网易.腾讯等.其次,它是一款反向代理服务器:第三,它还是一款电子邮件(IMAP/POP3)代理服务器 ...
- docker系列之六容器数据卷
docker之容器数据卷 一.容器数据卷 docker容器运行的时候,会产生一系列的文件,那么我们希望容器产生的数据能提供给其他的容器使用,也就是说怎么实现容器间的数据的共享呢?这就需要用到我们所提到 ...
- K2 BPM_【解决方案】从流程梳理到落地,K2为企业打造流程管理闭环_全业务流程管理专家
很多企业在进行流程管理体系建设的过程中,往往急于求成,还没有理清楚要“做什么”和“怎么做”,就开始大刀阔斧地进行改革优化.管理目标.建设标准的不统一,使得体系建设之间内容重复.要求冲突等现象层出不穷. ...
- moment——日期格式化常用示例
<template> <div id="app" style="text-align: center;"> <h1>{{ m ...