Skip to content

泉州一中信息学Blog

信息学奥赛技术分享博客

  • 登录
  • 小学oj
  • 中学oj
  • 测试页面1
  • Toggle search form

21 Days Training // Day 14

Posted on 2025年8月8日2025年8月8日 By c2022zyh 21 Days Training // Day 14无评论

T1. brick

2 Conclusions :

  1. The height of the first and the last brick is 0.
  2. For brick i and i+1, abs(h[i+1]-h[i]) cannot be greater than 1.

Proof :
For statement 1, since the region of every operation [L, R], L>=2, R<=N-1, So the height of brick 1 and N do not change.

For statement 2, suppose that we want to change the height of i. So we need to select a region [L, R], and the height of the brick in this region is equal.
To maximize abs(h[i+1]-h[i]), we need to select region [i-1, i+1], or the height of i+1 will also change.
However, after this operation, the region [i-1, i+1] do not satisfy the condition so it cannot be selected again. Also, any other regions that includes [i-1, i+1] cannot be selected.
To make this region be able to be chose, we need th=o change the height of i-1, i+1, but now the abs(h[i+1]-h[i]) will change to 0.

To sum up, abs(h[i+1]-h[i]) cannot be greater than 2.

So, using these 2 conclusions, we can first compute the planss count of a region, and use the multiple principle to get the final result.

In detail, suppose we know the region [L, R] are all -1 except leftmost brick and rightmost brick.
For brick L+1, its height can be h[L], h[L]+1, h[L]-1 .
So the plans count of this point is dp[L+1][h[L]]+dp[L+1][h[L]+1]+dp[L+1][h[L]-1] .
Here, dp[pos][h] shows the plan count when we know the height of the region [L, pos] and the height of pos is h.
Based on this formula, we can get the target value : dp[R][h[R]] .
Specially, when h is 0, current lowest value, dp[pos][h]=dp[pos-1][h]+dp[pos-1][h+1] , and when h is current highest value, dp[pos][h]=dp[pos-1][h]+dp[pos-1][h-1] .

Complexity : O(n^2) .

Code :

#include <bits/stdc++.h>
using namespace std;
const long long MOD=1000000007;

int main() {
    freopen("brick.in", "r", stdin);
    freopen("brick.out", "w", stdout);
    int N;cin >> N;
    int A[N];
    for (int i=0;i<N;i++) cin >> A[i];
    if (N==1) {
        cout <<(A[0]==0||A[0]==-1?"1":"0");
        exit(0);
    }
    if (N==2) {
        cout <<((A[0]==0||A[0]==-1)&&(A[1]==0||A[1]==-1)?"1":"0");
        exit(0);
    }
    A[0] = A[N-1] = 0;
    vector<pair<int, int> > seg;
    int Lx=-1;
    for (int i=0;i<N;i++) {
        if (A[i]==-1) {
            if (Lx==-1) Lx=i;
        }
        else {
            if (Lx!=-1) {
                seg.push_back(make_pair(Lx, i-1));
                Lx=-1;
            }
        }
    }
    if (seg.empty()) {
        for (int i=1;i<N;i++) {
            if (abs(A[i]-A[i-1])>1) {
                cout << 0;
                exit(0);
            }
        }
    }
    long long px=1;
    for (int i=0;i<seg.size();i++) {
        int L=seg[i].first-1, R=seg[i].second+1;
        if (abs(A[R]-A[L])>R-L) {
            cout << 0;
            exit(0);
        }
        // P_+ + P_0 + P_- = R-L
        // P_+ - P_-       = A[R]-A[L]
        long long dp[R-L+1][A[L]+R-L+1];
        dp[0][A[L]]=1;
        for (int i=1;i<=R-L;i++) {
            if (i==1) {
                if (A[L]>0) dp[i][A[L]-1]=1;
                dp[i][A[L]] = dp[i][A[L]+1] = 1;
            }
            else {
                for (int j=A[L]-i;j<=A[L]+i;j++) {
                    if (j<0) continue;
                    if (j==A[L]-i||j==A[L]+i) dp[i][j]=1;
                    else if (j==0||j==A[L]-i+1) dp[i][j] = dp[i-1][j] + dp[i-1][j+1];
                    else if (j==A[L]+i-1) dp[i][j] = dp[i-1][j] + dp[i-1][j-1];
                    else dp[i][j] = dp[i-1][j-1] + dp[i-1][j] + dp[i-1][j+1];
                    dp[i][j] %= MOD;
                }
            }
        }
        px*=dp[R-L][A[R]];
        px%=MOD;
    }
    cout << px;
    return 0;
}

Such a joke :


The case kills me.

Pages: 1 2 3 4 5 6
训练日志

文章导航

Previous Post: 中山纪念中学 Day14
Next Post: 中山纪念中学 Day15

发表回复 取消回复

要发表评论,您必须先登录。

2025年 12月
一 二 三 四 五 六 日
1234567
891011121314
15161718192021
22232425262728
293031  
« 8月    

2024常州 Class Classic OI Problems Contest cqr的长乐集训2023 CZYZ LOC New Game NOI NOIP Password Protected PM_PK Preview Problems Retrospect Selfmade Qusetion STL The end Training Uneasy Problem 蒟蒻 通报

  • 训练日志
  • 链表
  • 入门
  • 模拟
  • dfs序
  • 并查集
  • spfa
  • 最小割
  • 矩阵树定理
  • 仙人掌
  • BSGS
  • 凸包
  • 回文自动机
  • 递推与动归
  • 堆
  • 莫队算法
  • ST表
  • Treap
  • 树套树
  • 可持久化线段树
  • 初赛
  • 搜索
  • 贪心
  • 深度优先搜索
  • 欧拉图
  • dijkstra
  • 费用流
  • 哈夫曼树
  • kruskual
  • 置换
  • 旋转卡壳
  • KMP
  • 区间动归
  • STL
  • 链表
  • 可并堆
  • sply
  • 主席树
  • 可持久化字典树
  • 算法
  • 动态规划
  • 构造
  • 广度优先搜索
  • 最短路
  • floyd
  • 最大流
  • 虚树
  • prim
  • 筛法
  • 半平面交
  • 字典树
  • 背包动归
  • 基础数据结构
  • 分块
  • 线段树
  • 替罪羊树
  • K-DTree
  • 图论
  • 二分法
  • 迭代搜索
  • 拓扑排序
  • 有上下界网络流
  • 生成树
  • 快速幂
  • 后缀数组
  • 树形动归
  • 哈希表
  • 中级数据结构
  • 平衡树
  • 可持久化数据结构
  • 数据结构
  • 三分法
  • 启发式搜索
  • 图的连通
  • 点分治
  • 博弈论
  • AC自动机
  • 状压动归
  • 单调栈
  • 树状数组
  • 高级数据结构
  • OI资料
  • 数学
  • 高精度
  • 差分约束
  • 树上倍增
  • 素数测试
  • 后缀自动机
  • 数位动归
  • 单调队列
  • 新闻
  • 几何
  • 随机化
  • 二分图染色
  • 树链剖分
  • 欧拉函数
  • manacher
  • 斜率优化
  • 离线处理
  • 信息学奥赛学长风采
  • 字符串
  • 二分图匹配
  • prufer编码
  • 卡特兰数
  • 密码学
  • 决策单调
  • 赛后总结
  • 其他
  • 2-SAT
  • 最近公共祖先
  • 矩阵乘法
  • 记忆化搜索
  • 网络流
  • Link cut tree
  • 排列组合
  • 树
  • 高斯消元
  • 乘法逆元
  • 容斥原理
  • 调和级数
  • 概率与期望
  • 模线性方程组
  • 莫比乌斯反演
  • 快速傅里叶变换
  • 扩展欧几里德
  • 最大公约数与最小公倍数

近期文章

  • 中山纪念中学 Day21
  • 中山集训8.15 LAST DAY+集训小结
  • GDNOJ – DAY 18
  • 中山8.14
  • 2025暑假中山集训Day20——8.14

近期评论

归档

  • 2025年8月
  • 2025年7月
  • 2025年2月
  • 2025年1月
  • 2024年11月
  • 2024年10月
  • 2024年9月
  • 2024年8月
  • 2024年7月
  • 2024年3月
  • 2024年2月
  • 2024年1月
  • 2023年12月
  • 2023年11月
  • 2023年10月
  • 2023年9月
  • 2023年8月
  • 2023年7月
  • 2023年3月
  • 2023年2月
  • 2023年1月
  • 2022年12月

Copyright © 2025 泉州一中信息学Blog.

Powered by PressBook WordPress theme