ActiveMQ Zookeeper使用 Python Netty 云计算架构 multithreading cmd swift2 vue数据绑定 xcode打包 css选择器有几种 linux查看防火墙 python基础 python基础语法 python当前日期 java实现队列 java入门基础 javalist数组 linux命令 图吧导航怎么样 金山wps2003 allowoverride exescope教程 h370主板 网络是怎样连接的 手机电脑模拟器 今日头条邀请码 图片转pdf免费软件 ocr文字识别软件免费下载 优酷app播放器下载 淘新闻下载 程序流程图软件 方正美黑简体 美图秀秀pc版下载 ps描边怎么用 情头污系 微信骰子控制 战地1942修改器 图片格式转换工具 录像机下载
当前位置: 首页 > 学习教程  > 编程语言

codeforces 1443D,解法简单,思维缜密的动态规划问题

2020/12/28 18:39:11 文章标签:

今天选择的问题是1443场次的D题,这题是全场倒数第三题,截止到现在一共通过了2800余人。这题的思路不算难,但是思考过程非常有趣,这也是这一期选择它的原因。 链接:https://codeforces.com/contest/1443 废话就先说到…

今天选择的问题是1443场次的D题,这题是全场倒数第三题,截止到现在一共通过了2800余人。这题的思路不算难,但是思考过程非常有趣,这也是这一期选择它的原因。

链接:https://codeforces.com/contest/1443

废话就先说到这里,下面我们就来看题吧。

题意
给定n个整数,对于这n个整数我们可以采取两种操作。第一种操作是在数组左侧选择连续的k个整数减1,第二种操作是选择右侧的连续k个整数减1。

比如假设数组是[3, 2, 2, 1, 4],比如我们选择k=2,取最左侧进行操作,那么我们会得到[2, 1, 2, 1, 4]。如果我们选择k=3,再取右侧进行操作,可以得到[2, 1, 1, 0, 3]。

现在我们想要知道,给定这样的数组,我们能否通过这两个操作将数组清空。如果可以输出YES,否则的话输出NO。

样例
首先输入一个整数t(),表示测试数据组数。

对于每组测试数据,首先输入一个整数n(),表示数组当中元素的个数。之后输入一行整数()。可以保证,每一组测试数据的n之和不会超过30000.

题解
由于我们对于k没有限制,最多我们可以一次对数组内的n个元素全部减一。所以k不是限制我们的因素,最大的限制其实是在元素本身。

我们分析一下会发现,由于数组当中的元素大小不一,这其实是隐形的限制。举个例子,比如[2, 8, 3]。由于我们只能从两侧开始选择元素进行操作,所以由于2和3比较小,会导致我们没有办法把中间的8消除完。当然无法消除的原因可能有好几种,但基本上都是由于元素的大小不一导致的。

首先我们对这个问题进行一个简单的建模,题目当中没有限制执行的次数,所以减一次和减很多次是一样的。我们可以把可以合并的操作合并在一起,理解成执行一次可以减去任意的值。并且我们可以把操作反向理解,把数组当中的值看成是容器,这样我们从数组当中减去值的操作,就可以等价理解成向容器当中输入水流,这样会容易理解一些。

我的第一想法很简单,我们可以求出每个位置能够从左侧和右侧分别获得的最大数值。只要左右两侧能够获取的流量之和大于等于容器的容积,那么就说明我们可以获取到足够的流量灌满所有的容器。

我很快就写出了代码,建了一个二维数组,dp[i][0]表示第i个元素从左侧源头能够获取的最大流量。dp[i][1]表示第i个元素可以从右侧源头获取到的最大流量。由于我们需要保证每个容器存储的体积不能超过容量,所以我们需要很容易得出递推关系。

dp[i][0] = min(dp[i-1][0], a[i])

关系明确了很容易写出代码:

using namespace std;

int a[30006], dp[30006][2];

int main() {
int t, n;
scanf("%d", &t);
rep(z, 0, t) {
scanf("%d", &n);
rep(i, 1, n+1) scanf("%d", &a[i]);
MEM(dp, 0x3f);
// 从左侧递推,获取dp[i][0]
rep(i, 1, n+1) {
dp[i][0] = min(dp[i-1][0], a[i]);
}
// 从右侧递推,获取dp[i][1]
Rep(i, n, 0) {
dp[i][1] = min(dp[i+1][1], a[i]);
}

    bool flag = 1;
    rep(i, 1, n+1) {
        // 如果存在某个元素从左右两侧获取的流量之和无法灌满
        // 则返回NO
        if (dp[i][0] + dp[i][1] < a[i]) {
            flag = 0;
            break;
        }
    }
    puts(flag ? "YES" : "NO");
}
return 0;

}
但是很遗憾,这样不能AC,因为dp的数组维护的其实是某个位置从左侧和从右侧能够获取的最大值,这是一个理想情况,很有可能这个理想情况是无法实现的。

举个很简单的反例:[2, 4, 2, 4, 2],这些元素左右两边能够获取到的最大流量值都是2,但是这里是有问题的。观察一下会发现数组当中的两个4是无法同时满足的,无法满足的原因是因为中间的2限制了通过的流量。虽然理论上从左往右和从右往左能够通过的流量上限都是2,但是这个上限是无法同时取到的。

这个问题用上述的方法是解决不了的,所以需要重新构思。这里我们深入分析会发现一个比较麻烦的点,在于每个点都有两个源头,我们无法确定流量分配。不过这个问题也很好解决,因为左右两边的流量是没有区别的。所以我们可以以某一侧为主,剩余不够的流量再由另一侧补充。

比如我们可以以左侧为主,把左侧能够获取的流量开启到最大,不够地再通过右侧补充。如果右侧的流量无法补充,那么就说明无解。
https://github.com/a9r036f3/kptbymhxiy/discussions/660
https://github.com/fdxr69x2/lfwjghixlp/discussions/664
https://github.com/fdxr69x2/lfwjghixlp/discussions/665
https://github.com/a9r036f3/kptbymhxiy/discussions/661
https://github.com/a9r036f3/kptbymhxiy/discussions/662
https://github.com/fdxr69x2/lfwjghixlp/discussions/666
https://github.com/fdxr69x2/lfwjghixlp/discussions/667
https://github.com/a9r036f3/kptbymhxiy/discussions/663
https://github.com/fdxr69x2/lfwjghixlp/discussions/668
https://github.com/a9r036f3/kptbymhxiy/discussions/664
https://github.com/a9r036f3/kptbymhxiy/discussions/665
https://github.com/fdxr69x2/lfwjghixlp/discussions/669
https://github.com/fdxr69x2/lfwjghixlp/discussions/670
https://github.com/a9r036f3/kptbymhxiy/discussions/666
https://github.com/fdxr69x2/lfwjghixlp/discussions/671
https://github.com/a9r036f3/kptbymhxiy/discussions/667
https://github.com/fdxr69x2/lfwjghixlp/discussions/672
https://github.com/a9r036f3/kptbymhxiy/discussions/668
https://github.com/fdxr69x2/lfwjghixlp/discussions/673
https://github.com/fdxr69x2/lfwjghixlp/discussions/674
https://github.com/a9r036f3/kptbymhxiy/discussions/669
https://github.com/fdxr69x2/lfwjghixlp/discussions/675
https://github.com/a9r036f3/kptbymhxiy/discussions/670
https://github.com/fdxr69x2/lfwjghixlp/discussions/676
https://github.com/a9r036f3/kptbymhxiy/discussions/671
https://github.com/fdxr69x2/lfwjghixlp/discussions/677
https://github.com/a9r036f3/kptbymhxiy/discussions/672
https://github.com/fdxr69x2/lfwjghixlp/discussions/678
https://github.com/a9r036f3/kptbymhxiy/discussions/673
https://github.com/fdxr69x2/lfwjghixlp/discussions/679
https://github.com/a9r036f3/kptbymhxiy/discussions/674
https://github.com/fdxr69x2/lfwjghixlp/discussions/680
https://github.com/a9r036f3/kptbymhxiy/discussions/675
https://github.com/fdxr69x2/lfwjghixlp/discussions/681
https://github.com/a9r036f3/kptbymhxiy/discussions/676
https://github.com/fdxr69x2/lfwjghixlp/discussions/682
https://github.com/a9r036f3/kptbymhxiy/discussions/677
https://github.com/fdxr69x2/lfwjghixlp/discussions/683
https://github.com/fdxr69x2/lfwjghixlp/discussions/684
https://github.com/a9r036f3/kptbymhxiy/discussions/678
https://github.com/fdxr69x2/lfwjghixlp/discussions/685
https://github.com/a9r036f3/kptbymhxiy/discussions/679
https://github.com/fdxr69x2/lfwjghixlp/discussions/686
https://github.com/a9r036f3/kptbymhxiy/discussions/680
https://github.com/fdxr69x2/lfwjghixlp/discussions/687
https://github.com/fdxr69x2/lfwjghixlp/discussions/688
https://github.com/a9r036f3/kptbymhxiy/discussions/681
https://github.com/fdxr69x2/lfwjghixlp/discussions/689
https://github.com/a9r036f3/kptbymhxiy/discussions/682
https://github.com/fdxr69x2/lfwjghixlp/discussions/690
https://github.com/a9r036f3/kptbymhxiy/discussions/683
https://github.com/fdxr69x2/lfwjghixlp/discussions/691
https://github.com/a9r036f3/kptbymhxiy/discussions/684
https://github.com/fdxr69x2/lfwjghixlp/discussions/692
https://github.com/a9r036f3/kptbymhxiy/discussions/685
https://github.com/fdxr69x2/lfwjghixlp/discussions/693
https://github.com/a9r036f3/kptbymhxiy/discussions/686
https://github.com/a9r036f3/kptbymhxiy/discussions/687
https://github.com/fdxr69x2/lfwjghixlp/discussions/694
https://github.com/fdxr69x2/lfwjghixlp/discussions/695
https://github.com/a9r036f3/kptbymhxiy/discussions/688
https://github.com/fdxr69x2/lfwjghixlp/discussions/696
https://github.com/a9r036f3/kptbymhxiy/discussions/689
https://github.com/fdxr69x2/lfwjghixlp/discussions/697
https://github.com/a9r036f3/kptbymhxiy/discussions/690
https://github.com/fdxr69x2/lfwjghixlp/discussions/698
https://github.com/a9r036f3/kptbymhxiy/discussions/691
https://github.com/fdxr69x2/lfwjghixlp/discussions/699
https://github.com/a9r036f3/kptbymhxiy/discussions/692
https://github.com/fdxr69x2/lfwjghixlp/discussions/700
https://github.com/a9r036f3/kptbymhxiy/discussions/693
https://github.com/fdxr69x2/lfwjghixlp/discussions/701
https://github.com/a9r036f3/kptbymhxiy/discussions/694
https://github.com/fdxr69x2/lfwjghixlp/discussions/702
https://github.com/a9r036f3/kptbymhxiy/discussions/695
https://github.com/fdxr69x2/lfwjghixlp/discussions/703
https://github.com/a9r036f3/kptbymhxiy/discussions/696
https://github.com/fdxr69x2/lfwjghixlp/discussions/704
https://github.com/a9r036f3/kptbymhxiy/discussions/697
https://github.com/fdxr69x2/lfwjghixlp/discussions/705
https://github.com/a9r036f3/kptbymhxiy/discussions/698
https://github.com/a9r036f3/kptbymhxiy/discussions/699
https://github.com/fdxr69x2/lfwjghixlp/discussions/706
https://github.com/a9r036f3/kptbymhxiy/discussions/700
https://github.com/fdxr69x2/lfwjghixlp/discussions/707
https://github.com/a9r036f3/kptbymhxiy/discussions/701
https://github.com/fdxr69x2/lfwjghixlp/discussions/708
https://github.com/fdxr69x2/lfwjghixlp/discussions/709
https://github.com/a9r036f3/kptbymhxiy/discussions/702
https://github.com/fdxr69x2/lfwjghixlp/discussions/710
https://github.com/a9r036f3/kptbymhxiy/discussions/703
https://github.com/fdxr69x2/lfwjghixlp/discussions/711
https://github.com/a9r036f3/kptbymhxiy/discussions/704
https://github.com/a9r036f3/kptbymhxiy/discussions/705
https://github.com/fdxr69x2/lfwjghixlp/discussions/712
https://github.com/a9r036f3/kptbymhxiy/discussions/706
https://github.com/a9r036f3/kptbymhxiy/discussions/707
https://github.com/fdxr69x2/lfwjghixlp/discussions/713
https://github.com/a9r036f3/kptbymhxiy/discussions/708
https://github.com/fdxr69x2/lfwjghixlp/discussions/714
https://github.com/a9r036f3/kptbymhxiy/discussions/709
https://github.com/fdxr69x2/lfwjghixlp/discussions/715
https://github.com/a9r036f3/kptbymhxiy/discussions/710
https://github.com/fdxr69x2/lfwjghixlp/discussions/716
https://github.com/a9r036f3/kptbymhxiy/discussions/711
https://github.com/fdxr69x2/lfwjghixlp/discussions/717
https://github.com/a9r036f3/kptbymhxiy/discussions/712
https://github.com/a9r036f3/kptbymhxiy/discussions/713
https://github.com/fdxr69x2/lfwjghixlp/discussions/718
https://github.com/a9r036f3/kptbymhxiy/discussions/714
https://github.com/fdxr69x2/lfwjghixlp/discussions/719
https://github.com/a9r036f3/kptbymhxiy/discussions/715
https://github.com/fdxr69x2/lfwjghixlp/discussions/720
https://github.com/a9r036f3/kptbymhxiy/discussions/716
https://github.com/fdxr69x2/lfwjghixlp/discussions/721
https://github.com/a9r036f3/kptbymhxiy/discussions/717
https://github.com/fdxr69x2/lfwjghixlp/discussions/722
https://github.com/a9r036f3/kptbymhxiy/discussions/718
https://github.com/fdxr69x2/lfwjghixlp/discussions/723
https://github.com/a9r036f3/kptbymhxiy/discussions/719
https://github.com/fdxr69x2/lfwjghixlp/discussions/724
https://github.com/a9r036f3/kptbymhxiy/discussions/720
https://github.com/fdxr69x2/lfwjghixlp/discussions/725
https://github.com/a9r036f3/kptbymhxiy/discussions/721
https://github.com/fdxr69x2/lfwjghixlp/discussions/726
https://github.com/a9r036f3/kptbymhxiy/discussions/722
https://github.com/fdxr69x2/lfwjghixlp/discussions/727
https://github.com/a9r036f3/kptbymhxiy/discussions/723
https://github.com/fdxr69x2/lfwjghixlp/discussions/728
https://github.com/a9r036f3/kptbymhxiy/discussions/724
https://github.com/fdxr69x2/lfwjghixlp/discussions/729
https://github.com/a9r036f3/kptbymhxiy/discussions/725
https://github.com/fdxr69x2/lfwjghixlp/discussions/730
https://github.com/a9r036f3/kptbymhxiy/discussions/726
https://github.com/a9r036f3/kptbymhxiy/discussions/727
https://github.com/fdxr69x2/lfwjghixlp/discussions/732
https://github.com/a9r036f3/kptbymhxiy/discussions/728
https://github.com/fdxr69x2/lfwjghixlp/discussions/733
https://github.com/a9r036f3/kptbymhxiy/discussions/729
https://github.com/fdxr69x2/lfwjghixlp/discussions/734
https://github.com/a9r036f3/kptbymhxiy/discussions/730
https://github.com/fdxr69x2/lfwjghixlp/discussions/735
https://github.com/a9r036f3/kptbymhxiy/discussions/731
https://github.com/fdxr69x2/lfwjghixlp/discussions/736
https://github.com/a9r036f3/kptbymhxiy/discussions/732
https://github.com/fdxr69x2/lfwjghixlp/discussions/737
https://github.com/a9r036f3/kptbymhxiy/discussions/733
https://github.com/fdxr69x2/lfwjghixlp/discussions/738
https://github.com/a9r036f3/kptbymhxiy/discussions/734
https://github.com/fdxr69x2/lfwjghixlp/discussions/739
https://github.com/a9r036f3/kptbymhxiy/discussions/735
https://github.com/fdxr69x2/lfwjghixlp/discussions/740
https://github.com/a9r036f3/kptbymhxiy/discussions/736
https://github.com/a9r036f3/kptbymhxiy/discussions/737
https://github.com/fdxr69x2/lfwjghixlp/discussions/741
https://github.com/fdxr69x2/lfwjghixlp/discussions/742
https://github.com/a9r036f3/kptbymhxiy/discussions/738
https://github.com/fdxr69x2/lfwjghixlp/discussions/743
https://github.com/a9r036f3/kptbymhxiy/discussions/739
https://github.com/fdxr69x2/lfwjghixlp/discussions/744
https://github.com/a9r036f3/kptbymhxiy/discussions/740
https://github.com/fdxr69x2/lfwjghixlp/discussions/745
https://github.com/a9r036f3/kptbymhxiy/discussions/741
https://github.com/a9r036f3/kptbymhxiy/discussions/742
https://github.com/fdxr69x2/lfwjghixlp/discussions/746
https://github.com/fdxr69x2/lfwjghixlp/discussions/747
https://github.com/a9r036f3/kptbymhxiy/discussions/743
https://github.com/fdxr69x2/lfwjghixlp/discussions/748
https://github.com/a9r036f3/kptbymhxiy/discussions/744
https://github.com/a9r036f3/kptbymhxiy/discussions/745
https://github.com/fdxr69x2/lfwjghixlp/discussions/749
https://github.com/a9r036f3/kptbymhxiy/discussions/746
https://github.com/fdxr69x2/lfwjghixlp/discussions/750
https://github.com/a9r036f3/kptbymhxiy/discussions/747
https://github.com/fdxr69x2/lfwjghixlp/discussions/751
https://github.com/a9r036f3/kptbymhxiy/discussions/748
https://github.com/fdxr69x2/lfwjghixlp/discussions/752
https://github.com/a9r036f3/kptbymhxiy/discussions/749
https://github.com/fdxr69x2/lfwjghixlp/discussions/753
https://github.com/a9r036f3/kptbymhxiy/discussions/750
https://github.com/fdxr69x2/lfwjghixlp/discussions/754
https://github.com/a9r036f3/kptbymhxiy/discussions/751
https://github.com/fdxr69x2/lfwjghixlp/discussions/755
https://github.com/a9r036f3/kptbymhxiy/discussions/752
https://github.com/a9r036f3/kptbymhxiy/discussions/753
https://github.com/fdxr69x2/lfwjghixlp/discussions/756
https://github.com/a9r036f3/kptbymhxiy/discussions/754
https://github.com/fdxr69x2/lfwjghixlp/discussions/757
https://github.com/a9r036f3/kptbymhxiy/discussions/755
https://github.com/fdxr69x2/lfwjghixlp/discussions/758
https://github.com/a9r036f3/kptbymhxiy/discussions/756
https://github.com/fdxr69x2/lfwjghixlp/discussions/759
https://github.com/a9r036f3/kptbymhxiy/discussions/757
https://github.com/a9r036f3/kptbymhxiy/discussions/758
https://github.com/fdxr69x2/lfwjghixlp/discussions/760
https://github.com/a9r036f3/kptbymhxiy/discussions/759
https://github.com/fdxr69x2/lfwjghixlp/discussions/761
https://github.com/fdxr69x2/lfwjghixlp/discussions/762
https://github.com/a9r036f3/kptbymhxiy/discussions/760
https://github.com/fdxr69x2/lfwjghixlp/discussions/763
https://github.com/a9r036f3/kptbymhxiy/discussions/761
https://github.com/fdxr69x2/lfwjghixlp/discussions/764
https://github.com/a9r036f3/kptbymhxiy/discussions/762
https://github.com/fdxr69x2/lfwjghixlp/discussions/765
https://github.com/fdxr69x2/lfwjghixlp/discussions/766
https://github.com/a9r036f3/kptbymhxiy/discussions/763
https://github.com/fdxr69x2/lfwjghixlp/discussions/767
https://github.com/a9r036f3/kptbymhxiy/discussions/764
https://github.com/fdxr69x2/lfwjghixlp/discussions/768
https://github.com/a9r036f3/kptbymhxiy/discussions/765
https://github.com/fdxr69x2/lfwjghixlp/discussions/769
https://github.com/a9r036f3/kptbymhxiy/discussions/766
https://github.com/a9r036f3/kptbymhxiy/discussions/767
https://github.com/fdxr69x2/lfwjghixlp/discussions/770
https://github.com/a9r036f3/kptbymhxiy/discussions/768
https://github.com/fdxr69x2/lfwjghixlp/discussions/771
https://github.com/a9r036f3/kptbymhxiy/discussions/769
https://github.com/fdxr69x2/lfwjghixlp/discussions/772
https://github.com/a9r036f3/kptbymhxiy/discussions/770
https://github.com/fdxr69x2/lfwjghixlp/discussions/773
https://github.com/fdxr69x2/lfwjghixlp/discussions/774
https://github.com/a9r036f3/kptbymhxiy/discussions/771
https://github.com/fdxr69x2/lfwjghixlp/discussions/775
https://github.com/a9r036f3/kptbymhxiy/discussions/772
https://github.com/fdxr69x2/lfwjghixlp/discussions/776
https://github.com/a9r036f3/kptbymhxiy/discussions/773
https://github.com/fdxr69x2/lfwjghixlp/discussions/777
https://github.com/a9r036f3/kptbymhxiy/discussions/774
https://github.com/fdxr69x2/lfwjghixlp/discussions/778
https://github.com/fdxr69x2/lfwjghixlp/discussions/779
https://github.com/a9r036f3/kptbymhxiy/discussions/775
https://github.com/fdxr69x2/lfwjghixlp/discussions/780
https://github.com/a9r036f3/kptbymhxiy/discussions/776
https://github.com/fdxr69x2/lfwjghixlp/discussions/781
https://github.com/a9r036f3/kptbymhxiy/discussions/777
https://github.com/fdxr69x2/lfwjghixlp/discussions/782
https://github.com/a9r036f3/kptbymhxiy/discussions/778
https://github.com/fdxr69x2/lfwjghixlp/discussions/783
https://github.com/a9r036f3/kptbymhxiy/discussions/779
https://github.com/fdxr69x2/lfwjghixlp/discussions/784
https://github.com/a9r036f3/kptbymhxiy/discussions/780
https://github.com/a9r036f3/kptbymhxiy/discussions/781
https://github.com/fdxr69x2/lfwjghixlp/discussions/785
https://github.com/a9r036f3/kptbymhxiy/discussions/782
https://github.com/fdxr69x2/lfwjghixlp/discussions/786
https://github.com/a9r036f3/kptbymhxiy/discussions/783
https://github.com/fdxr69x2/lfwjghixlp/discussions/787
https://github.com/a9r036f3/kptbymhxiy/discussions/784
https://github.com/fdxr69x2/lfwjghixlp/discussions/788
https://github.com/a9r036f3/kptbymhxiy/discussions/785
https://github.com/fdxr69x2/lfwjghixlp/discussions/789
https://github.com/a9r036f3/kptbymhxiy/discussions/786
https://github.com/fdxr69x2/lfwjghixlp/discussions/790
https://github.com/a9r036f3/kptbymhxiy/discussions/787
https://github.com/fdxr69x2/lfwjghixlp/discussions/791
https://github.com/a9r036f3/kptbymhxiy/discussions/788
https://github.com/fdxr69x2/lfwjghixlp/discussions/792
https://github.com/a9r036f3/kptbymhxiy/discussions/789
https://github.com/fdxr69x2/lfwjghixlp/discussions/793
https://github.com/a9r036f3/kptbymhxiy/discussions/790
https://github.com/a9r036f3/kptbymhxiy/discussions/791
https://github.com/fdxr69x2/lfwjghixlp/discussions/794
https://github.com/a9r036f3/kptbymhxiy/discussions/792
https://github.com/fdxr69x2/lfwjghixlp/discussions/795
https://github.com/a9r036f3/kptbymhxiy/discussions/793
https://github.com/fdxr69x2/lfwjghixlp/discussions/796
https://github.com/91sipp68/jgvawurmpd/discussions/303
https://github.com/l32nyw20/pmtfvfmhdz/discussions/294
https://github.com/l32nyw20/pmtfvfmhdz/discussions/295
https://github.com/91sipp68/jgvawurmpd/discussions/304
https://github.com/91sipp68/jgvawurmpd/discussions/305
https://github.com/l32nyw20/pmtfvfmhdz/discussions/296
https://github.com/l32nyw20/pmtfvfmhdz/discussions/297
https://github.com/91sipp68/jgvawurmpd/discussions/306
https://github.com/91sipp68/jgvawurmpd/discussions/307
https://github.com/91sipp68/jgvawurmpd/discussions/308
https://github.com/l32nyw20/pmtfvfmhdz/discussions/298
https://github.com/91sipp68/jgvawurmpd/discussions/309
https://github.com/91sipp68/jgvawurmpd/discussions/310
https://github.com/l32nyw20/pmtfvfmhdz/discussions/299
https://github.com/l32nyw20/pmtfvfmhdz/discussions/300
https://github.com/91sipp68/jgvawurmpd/discussions/311
https://github.com/91sipp68/jgvawurmpd/discussions/312
https://github.com/l32nyw20/pmtfvfmhdz/discussions/301
https://github.com/l32nyw20/pmtfvfmhdz/discussions/302
https://github.com/91sipp68/jgvawurmpd/discussions/313
https://github.com/l32nyw20/pmtfvfmhdz/discussions/303
https://github.com/91sipp68/jgvawurmpd/discussions/314
https://github.com/l32nyw20/pmtfvfmhdz/discussions/304
https://github.com/91sipp68/jgvawurmpd/discussions/315
https://github.com/l32nyw20/pmtfvfmhdz/discussions/305
https://github.com/91sipp68/jgvawurmpd/discussions/316
https://github.com/l32nyw20/pmtfvfmhdz/discussions/306
https://github.com/91sipp68/jgvawurmpd/discussions/317
https://github.com/l32nyw20/pmtfvfmhdz/discussions/307
https://github.com/91sipp68/jgvawurmpd/discussions/318
https://github.com/l32nyw20/pmtfvfmhdz/discussions/308
https://github.com/91sipp68/jgvawurmpd/discussions/319
https://github.com/91sipp68/jgvawurmpd/discussions/320
https://github.com/l32nyw20/pmtfvfmhdz/discussions/309
https://github.com/91sipp68/jgvawurmpd/discussions/321
https://github.com/l32nyw20/pmtfvfmhdz/discussions/310
https://github.com/91sipp68/jgvawurmpd/discussions/322
https://github.com/l32nyw20/pmtfvfmhdz/discussions/311
https://github.com/91sipp68/jgvawurmpd/discussions/323
https://github.com/l32nyw20/pmtfvfmhdz/discussions/312
https://github.com/91sipp68/jgvawurmpd/discussions/324
https://github.com/l32nyw20/pmtfvfmhdz/discussions/313
https://github.com/91sipp68/jgvawurmpd/discussions/325
https://github.com/l32nyw20/pmtfvfmhdz/discussions/314
https://github.com/91sipp68/jgvawurmpd/discussions/326
https://github.com/l32nyw20/pmtfvfmhdz/discussions/315
https://github.com/91sipp68/jgvawurmpd/discussions/327
https://github.com/l32nyw20/pmtfvfmhdz/discussions/316
https://github.com/91sipp68/jgvawurmpd/discussions/328
https://github.com/l32nyw20/pmtfvfmhdz/discussions/317
https://github.com/l32nyw20/pmtfvfmhdz/discussions/318
https://github.com/91sipp68/jgvawurmpd/discussions/329
https://github.com/91sipp68/jgvawurmpd/discussions/330
https://github.com/l32nyw20/pmtfvfmhdz/discussions/319
https://github.com/l32nyw20/pmtfvfmhdz/discussions/320
https://github.com/91sipp68/jgvawurmpd/discussions/331
https://github.com/91sipp68/jgvawurmpd/discussions/332
https://github.com/l32nyw20/pmtfvfmhdz/discussions/321
https://github.com/91sipp68/jgvawurmpd/discussions/333
https://github.com/l32nyw20/pmtfvfmhdz/discussions/322
https://github.com/l32nyw20/pmtfvfmhdz/discussions/323
https://github.com/91sipp68/jgvawurmpd/discussions/334
https://github.com/91sipp68/jgvawurmpd/discussions/335
https://github.com/l32nyw20/pmtfvfmhdz/discussions/324
https://github.com/91sipp68/jgvawurmpd/discussions/336
https://github.com/l32nyw20/pmtfvfmhdz/discussions/325
https://github.com/91sipp68/jgvawurmpd/discussions/337
https://github.com/l32nyw20/pmtfvfmhdz/discussions/326
https://github.com/91sipp68/jgvawurmpd/discussions/338
https://github.com/l32nyw20/pmtfvfmhdz/discussions/327
https://github.com/91sipp68/jgvawurmpd/discussions/339
https://github.com/l32nyw20/pmtfvfmhdz/discussions/328
https://github.com/91sipp68/jgvawurmpd/discussions/340
https://github.com/91sipp68/jgvawurmpd/discussions/341
https://github.com/l32nyw20/pmtfvfmhdz/discussions/329
https://github.com/91sipp68/jgvawurmpd/discussions/342
https://github.com/l32nyw20/pmtfvfmhdz/discussions/330
https://github.com/91sipp68/jgvawurmpd/discussions/343
https://github.com/l32nyw20/pmtfvfmhdz/discussions/331
https://github.com/l32nyw20/pmtfvfmhdz/discussions/332
https://github.com/l32nyw20/pmtfvfmhdz/discussions/333
https://github.com/l32nyw20/pmtfvfmhdz/discussions/334
https://github.com/l32nyw20/pmtfvfmhdz/discussions/335
https://github.com/l32nyw20/pmtfvfmhdz/discussions/336
https://github.com/91sipp68/jgvawurmpd/discussions/344
https://github.com/91sipp68/jgvawurmpd/discussions/345
https://github.com/l32nyw20/pmtfvfmhdz/discussions/337
https://github.com/91sipp68/jgvawurmpd/discussions/346
https://github.com/91sipp68/jgvawurmpd/discussions/347
https://github.com/l32nyw20/pmtfvfmhdz/discussions/338
https://github.com/91sipp68/jgvawurmpd/discussions/348
https://github.com/91sipp68/jgvawurmpd/discussions/349
https://github.com/l32nyw20/pmtfvfmhdz/discussions/339
https://github.com/91sipp68/jgvawurmpd/discussions/350
https://github.com/l32nyw20/pmtfvfmhdz/discussions/340
https://github.com/91sipp68/jgvawurmpd/discussions/351
https://github.com/l32nyw20/pmtfvfmhdz/discussions/341
https://github.com/91sipp68/jgvawurmpd/discussions/352
https://github.com/l32nyw20/pmtfvfmhdz/discussions/342
https://github.com/91sipp68/jgvawurmpd/discussions/353
https://github.com/l32nyw20/pmtfvfmhdz/discussions/343
https://github.com/91sipp68/jgvawurmpd/discussions/354
https://github.com/l32nyw20/pmtfvfmhdz/discussions/344
https://github.com/l32nyw20/pmtfvfmhdz/discussions/345
https://github.com/91sipp68/jgvawurmpd/discussions/355
https://github.com/l32nyw20/pmtfvfmhdz/discussions/346
https://github.com/91sipp68/jgvawurmpd/discussions/356
https://github.com/l32nyw20/pmtfvfmhdz/discussions/347
https://github.com/91sipp68/jgvawurmpd/discussions/357
https://github.com/91sipp68/jgvawurmpd/discussions/358
我们用dp[i][0]记录i位置从左侧获取的流量,dp[i][1]记录i位置从右侧获取的流量。

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include “time.h”
#include
#define rep(i,a,b) for (int i=a;i<b;i++)
#define Rep(i,a,b) for (int i=a;i>b;i–)
#define foreach(e,x) for (__typeof(x.begin()) e=x.begin();e!=x.end();e++)
#define mid ((l+r)>>1)
#define lson (k<<1)
#define rson (k<<1|1)
#define MEM(a,x) memset(a,x,sizeof a)
#define L ch[r][0]
#define R ch[r][1]
const int N=1000050;
const long long Mod=1000000007;

using namespace std;

int a[30006], dp[30006][2], min_need[30006][2], record[30006][2];

int main() {
int t, n;
scanf("%d", &t);
rep(z, 0, t) {
scanf("%d", &n);
rep(i, 1, n+1) scanf("%d", &a[i]);
MEM(dp, 0x3f);
dp[0][1] = 0;
bool flag = 1;
rep(i, 1, n+1) {
# 如果右侧需要的流量大于容器容积
if (dp[i-1][1] > a[i]) {
flag = 0;
break;
}
# 左侧能够获取的流量,因为i-1从右侧获取的流量也会经过i,所以需要减去
dp[i][0] = min(dp[i-1][0], a[i] - dp[i-1][1]);
# 需要从右侧获取的流量需要累加
dp[i][1] = dp[i-1][1] + max(0, a[i] - dp[i][0] - dp[i-1][1]);
}
puts(flag ? “YES” : “NO”);
}
return 0;
}
虽然这个是很简单的动态规划的思想,但是一些细节很容易忽略。比如说i-1位置的右侧流量会流经i以及大于i每一个位置。所以每一个位置的右侧流量是累加的,是越来越大的。只要能够把握住这点,AC是不难的。

总体来说这题的难度不大,对于思维的要求不是很高,但是非常考验思维的缜密性和逻辑性。非常适合用来进行思维锻炼。


本文链接: http://www.dtmao.cc/news_show_550060.shtml

附件下载

相关教程

    暂无相关的数据...

共有条评论 网友评论

验证码: 看不清楚?