「AT2292」Division into Two

Posted zsbzsb

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了「AT2292」Division into Two相关的知识,希望对你有一定的参考价值。

传送门
Luogu

解题思路

考虑如何 ( ext{DP})
为了方便处理,我们设 (A > B)
(dp[i]) 表示处理完 (1...i) ,并且第 (i) 个数放入关于 (A) 的集合中的方案。
转移就只需要枚举前一个数 (j) 就好了。
但是观察到 (N le 10^5) ,我们就需要考虑优化。
观察到每次 (j) 的取值都是一段连续的区间,所以我们可以前缀和优化一下,就可以做到 (O(n))

细节注意事项

  • 咕咕咕

参考代码

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#define rg register
using namespace std;
template < typename T > inline void read(T& s) {
    s = 0; int f = 0; char c = getchar();
    while (!isdigit(c)) f |= (c == '-'), c = getchar();
    while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
    s = f ? -s : s;
}

typedef long long LL;
const int _ = 100010;
const LL p = 1e9 + 7;

int n; LL dp[_], sum[_], A, B, a[_];

int main() {
    read(n), read(A), read(B); if (A < B) swap(A, B);
    for (rg int i = 1; i <= n; ++i) read(a[i]);
    for (rg int i = 1; i + 2 <= n; ++i)
        if (a[i + 2] - a[i] < B) return puts("0"), 0;
    dp[0] = sum[0] = 1;
    for (rg int l = 0, r = 0, i = 1; i <= n; ++i) {
        while (r < i && a[i] - a[r + 1] >= A) ++r;
        if (l <= r) dp[i] = (sum[r] - (l > 0 ? sum[l - 1] : 0ll) + p) % p;
        sum[i] = (sum[i - 1] + dp[i]) % p;
        if (i > 1 && a[i] - a[i - 1] < B) l = i - 1;
    }
    LL ans = 0;
    for (rg int i = n; ~i; --i) {
        ans = (ans + dp[i]) % p;
        if (i < n && a[i + 1] - a[i] < B) break;
    }
    printf("%lld
", ans);
    return 0;
}

完结撒花 (qwq)

以上是关于「AT2292」Division into Two的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #598 (Div. 3) E. Yet Another Division Into Teams dp

CF1256E Yet Another Division Into Teams(动态规划)

Division and Recursion--find the nearest points in two dimension

algorithm@ Divide two integers without using multiplication, division and mod operator. (Bit Operati

Classify the pixels into two sets (像素点的分类)

[Git] Undo my last commit and split it into two separate ones