缩点DP

缩点DP

对于任何一个DP的过程,我们都可以把他看作是对DAG图遍历一次拓扑排序,针对于一条路径 x → … → y x \to \ldots \to y xy,中间的状态转移都是状态继承,我们就直接可以把中间的点去掉,把这个路径压缩为 x → y x \to y xy,压缩之后,不改变答案的结果,即中间的点对结果没有任何帮助,仅仅是一个传递状态的作用,我们就可以压掉这些点。在DP中叫做路径压缩,也叫缩点。

此方法针对于DP状态转移十分稀疏(大多数都是状态继承),并且节点数很多的点。

缩点的方法有很多,例如动态缩点、数论缩点等等。

数论缩点

P1052

此题就可以数论缩点,针对于一条最短路径,其某一个落脚点一定是一个石头的后9个点之内(包括石头正好10个),缩点之后,必须让这些点和路径依旧存在,才能保证问题的等价行。

根据数论的知识,我们选择的下界可以是72(1-10任意相邻两个都能到达的下界),2520(1-10的 l c m lcm lcm,即最小公倍数下界)。

#include <bits/stdc++.h>

using namespace std;

#define FR freopen("in.txt","r",stdin)
#define FW freopen("out1.txt","w",stdout)

typedef long long ll;

ll stones[105];
bool dstones[10000];

int main()
{
    ll L;
    cin >> L;
    int S,T,M;
    cin >> S >> T >> M;

    if(S == T)
    {
        int cnt = 0;

        while(M--)
        {
            int loc;
            cin >> loc;

            if(loc % S == 0) cnt++;
        }

        cout << cnt;
        return 0;
    }

    for(int i = 1; i<=M; i++) cin >> stones[i];

    sort(stones+1,stones + M + 1);

    stones[0] = 0;
    stones[M+1] = L;

    int p = 0;
    for(int i = 0; i<=M; i++)
    {
        p += min(72ll,stones[i+1] - stones[i]);
        dstones[p] = true;
    }
    dstones[p] = false;
    int dp[10000];
    for(int i = 0; i<10000; i++) dp[i] = 1000;
    dp[0] = 0;
    for(int i = 0; i<=p; i++)
    {
        if(dstones[i]) dp[i]++;
        for(int step = S; step<=T; step++)
        {
            dp[i + step] = min(dp[i + step],dp[i]);
        }
    }
    int ans = 1000;
    for(int i = p; i<=p+10; i++)
    {
        ans = min(ans,dp[i]);
    }

    cout << ans;
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在 Android 中,可以使用 View 的布局参数来实现视图的伸效果。常用的布局参数有 LinearLayout.LayoutParams 和 RelativeLayout.LayoutParams。下面介绍两种常见的视图伸方式。 1. LinearLayout 中的权重(weight) LinearLayout 中的权重是一种常见的视图伸方式。可以通过设置子视图的权重来控制它们在 LinearLayout 中的占比。具体使用方法如下: 1)设置 LinearLayout 的方向为水平或垂直方向。 2)设置子视图的宽度或高度为 0dp。 3)设置子视图的权重(weight),权重越大,占比越大。 示例代码: ``` <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="View 1"/> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:text="View 2"/> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="View 3"/> </LinearLayout> ``` 上述代码中,三个 TextView 的占比分别为 1:2:1,即中间的 TextView 占比最大。 2. RelativeLayout 中的规则属性 RelativeLayout 中常用的视图伸方式是通过设置视图之间的相对位置关系来实现。可以使用 RelativeLayout.LayoutParams 中的规则属性来设置视图之间的相对位置关系。具体使用方法如下: 1)设置 RelativeLayout 的布局参数为 RelativeLayout.LayoutParams。 2)通过 addRule() 方法设置视图之间的相对位置关系,比如设置某个视图在另一个视图的下方或右侧等。 示例代码: ``` <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="View 1"/> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="View 2" android:layout_below="@id/textView1"/> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="View 3" android:layout_below="@id/textView1" android:layout_toRightOf="@id/textView2"/> </RelativeLayout> ``` 上述代码中,textView2 在 textView1 的下方,textView3 在 textView1 的下方且在 textView2 的右侧。通过设置不同的规则属性,可以实现更复杂的布局效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值