博客
关于我
[牛客练习赛69] D. 火柴排队 多维dp+逆元+递推求排列组合
阅读量:334 次
发布时间:2019-03-04

本文共 1983 字,大约阅读时间需要 6 分钟。

??????????????????????????????

????

???????n???a?????????k??????????????d??????????????????????a_i < a_j???????a_i' < a_j'????

???????????????????????????????????????????????????????????

??????????????????????????dp[i][j][k]??i??????j????????k???i?????????

????????

  • ????????dp[i][j][0] = dp[i-1][j][0] + dp[i-1][j][1] * (a[i-1] + d ? a[i])
  • ???????dp[i][j][1] = dp[i-1][j-1][0] + dp[i-1][j-1][1]
  • ??????

    ???????????????????????????

    ????

    #include 
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    using namespace std;typedef long long ll;const double PI = acos(-1.0);const double eps = 1e-6;const ll mod = 998244353;const int inf = 0x3f3f3f3f;const int maxn = 5000 + 10;void get_comb(int n) { c[0] = 1; for (int i = 1; i <= n; ++i) { c[i] = (n - i + 1) * c[i - 1] % mod * inv(i) % mod; }}ll inv(ll a) { return pow(a, mod - 2, mod);}void main() { ll n, d; scanf("%lld %lld", &n, &d); get_comb(n); vector
    a(n + 1); for (int i = 1; i <= n; ++i) { scanf("%lld", &a[i]); } sort(a + 1, a + n + 1); dp[1][1][1] = 1; dp[1][0][0] = 1; for (int i = 2; i <= n; ++i) { for (int j = 0; j <= i; ++j) { if ((i - 1) & 1) { dp[i & 1][j][0] = (dp[i - 1 & 1][j][0] + dp[i - 1 & 1][j][1] * (a[i - 1] + d <= a[i])) % mod; dp[i & 1][j][1] = (dp[i - 1 & 1][j - 1][1] + dp[i - 1 & 1][j - 1][0]) % mod; } else { dp[i & 1][j][0] = (dp[i - 1][j][0] + dp[i - 1][j][1] * (a[i - 1] + d <= a[i])) % mod; dp[i & 1][j][1] = (dp[i - 1][j - 1][1] + dp[i - 1][j - 1][0]) % mod; } } } for (int i = 1; i <= n; ++i) { ll ans = (dp[n & 1][i][0] + dp[n & 1][i][1]) % mod; ans = ans * pow(c[i], mod - 2, mod) % mod; ans = (ans + mod) % mod; printf("%lld\n", ans); }}

    ????

  • ??????????????????????????
  • ????????dp[1][0][0]?dp[1][1][1]????1?
  • ??????????????????????dp??
  • ????????k????????????????????????
  • ????????O(n^2)???????????????n=5000????

    转载地址:http://yqmh.baihongyu.com/

    你可能感兴趣的文章
    Qt笔记——获取位置信息的相关函数
    查看>>
    POJ 2484 A Funny Game(神题!)
    查看>>
    POJ 2486 树形dp
    查看>>
    POJ 2488:A Knight&#39;s Journey
    查看>>
    SpringBoot为什么易学难精?
    查看>>
    poj 2545 Hamming Problem
    查看>>
    poj 2723
    查看>>
    poj 2763 Housewife Wind
    查看>>
    Qt笔记——模型/视图MVD 文件目录浏览器软件
    查看>>
    POJ 2892 Tunnel Warfare(树状数组+二分)
    查看>>
    poj 2965 The Pilots Brothers' refrigerator-1
    查看>>
    poj 3026( Borg Maze BFS + Prim)
    查看>>
    POJ 3041 - 最大二分匹配
    查看>>
    POJ 3041 Asteroids(二分匹配模板题)
    查看>>
    Qt笔记——标准文件对话框QFileDialog
    查看>>
    poj 3083 Children of the Candy Corn
    查看>>
    POJ 3083 Children of the Candy Corn 解题报告
    查看>>
    POJ 3253 Fence Repair C++ STL multiset 可解 (同51nod 1117 聪明的木匠)
    查看>>
    Qt笔记——控件总结
    查看>>
    poj 3262 Protecting the Flowers 贪心
    查看>>