题目大意:给定一个 N 个值组成的序列,求序列中区间和相同的不相交区间段数量的最大值。

题解:设 \(dp[i][j]\) 表示到区间 [i,j] 时,与区间 [i,j] 的区间和相同的不相交区间数量是多少。可以枚举之前的区间进行状态转移,时间复杂度为 \(O(n^4)\)。

代码如下

#include <bits/stdc++.h>
using namespace std;
const int maxn=51; int n,ans,x,y,a[maxn],sum[maxn],dp[maxn][maxn];
struct node{int l,r;}pre[maxn][maxn]; void dfs(int i,int j){
if(!i)return;
dfs(pre[i][j].l,pre[i][j].r);
printf("%d %d\n",i,j);
} void solve(){
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&a[i]),sum[i]=sum[i-1]+a[i];
for(int i=1;i<=n;i++)
for(int j=i;j<=n;j++){
dp[i][j]=0;
int ch=sum[j]-sum[i-1];
for(int l=1;l<i;l++)
for(int r=l;r<i;r++)if(ch==sum[r]-sum[l-1])
if(dp[l][r]>dp[i][j]){
dp[i][j]=dp[l][r];
pre[i][j]=node{l,r};
}
++dp[i][j];
if(dp[i][j]>ans)ans=dp[i][j],x=i,y=j;
}
printf("%d\n",ans);
dfs(x,y);
} int main(){
solve();
return 0;
}

【CF1141F1】Same Sum Blocks的更多相关文章

  1. 【CF1141F2】Same Sum Blocks

    题解:发现可以通过枚举区间将区间和相同的元组记录在一个表中,对于答案来说,在同一个表中的元组的选择才会对答案产生贡献.发现每一个表中都是一个个区间,问题转化成了对于每一个表来说,选择若干个不相交的区间 ...

  2. 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)

    [LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...

  3. 【Leetcode】404. Sum of Left Leaves

    404. Sum of Left Leaves [题目]中文版  英文版 /** * Definition for a binary tree node. * struct TreeNode { * ...

  4. 【LibreOJ】【LOJ】#6220. sum

    [题意]对于n个数,找出一些数使得它们的和能被n整除,输出任意一组方案,n<=10^6. [算法]构造/结论 [题解]引用自:http://www.cnblogs.com/Sakits/p/74 ...

  5. 【LeetCode】Two Sum II - Input array is sorted

    [Description] Given an array of integers that is already sorted in ascending order, find two numbers ...

  6. 【LeetCode】633. Sum of Square Numbers

    Difficulty: Easy  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/sum-of-square-n ...

  7. 【leetcode】907. Sum of Subarray Minimums

    题目如下: 解题思路:我的想法对于数组中任意一个元素,找出其左右两边最近的小于自己的元素.例如[1,3,2,4,5,1],元素2左边比自己小的元素是1,那么大于自己的区间就是[3],右边的区间就是[4 ...

  8. 【leetcode】Path Sum IV

    If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...

  9. 【leetcode】Path Sum

    题目简述: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding ...

随机推荐

  1. switch-case和if-else可互换时

    当switch-case和if-else可互换时,应当优先采用switch-case.因为switch-case的效率更高(详情下回分解).

  2. shell自定义输入输出 read+echo

    自定义格式输入.输出(244)  输出:echo -e 解释转义字符 -n  回车不换行 \n   新的一行,等同于回车 \t 制表符 \r 回车 \b 回退 baskspace 删除键 演示\n \ ...

  3. No module named 'ConfigParser'

    系统: CentOS-6.4-x86_64 Python : Python 3.4.5 和 Python 3.5.2 安装 MySQL-python ,结果出错: ImportError: No mo ...

  4. elasticsearch索引合并

    参考地址:http://cwiki.apachecn.org/display/Elasticsearch/Reindex+API 1.首先插入准备数据,创建两个索引. (1).PUT  http:// ...

  5. 三、zookeeper安装

    一.简介 二.下载解压: #wget http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.12/zookeeper-3.4.12.tar ...

  6. 小米Note3 MIUI9可以用的XPosed框架

    资源来自论坛里的:http://www.miui.com/thread-6449305-1-1.html 首先需要刷入TWRP,资源在这个帖子里:http://www.miui.com/thread- ...

  7. github上传时出现error: src refspec master does not match any解决办法22

    1 error:src refspec master does not match any这个问题,我之前也遇到过,这次又遇到了只是时间间隔比较长了,为了防止以后再遇到类似问题,还是把这个方法简单记录 ...

  8. name设置id的方式 解决多个单选域冲突现象 同时有利于从动态网页取值

  9. 如何在虚拟机下配置centOS7

    链接地址:https://baijiahao.baidu.com/s?id=1597320700700593557&wfr=spider&for=pc

  10. 简单介绍一下在CentOS上安装Docker。

    简单介绍一下在CentOS上安装Docker. 前置条件: 64-bit 系统 kernel 3.10+ 1.检查内核版本,返回的值大于3.10即可. $ uname -r 2.使用 sudo 或 r ...