题目大意:给定一个 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. java的强制类型转换

    java强制类型转换  详细连接https://www.cnblogs.com/kuangwong/p/6198862.html 在Java项目的实际开发和应用中,常常需要用到将对象转为String这 ...

  2. 老男孩python学习自修【第三天】列表用法

    列表的使用: list.append(value) 向列表增加元素 list.insert(index, value) 向列表指定元素插入元素 list.extend(newlist) 用新的列表扩展 ...

  3. Python2.7从入门到精通

    快速入门 1.程序输出print语句 (1)使用print语句可查看对象的值:在交互式解释器使用对象本身则输出此对象的字符串表示: (2)使用print语句调用str()显示对象:在交互式解释器使用对 ...

  4. MySQL的FIND_IN_SET()函数

    今天在做项目时,看到了一个从没见过的MySQL函数——FIND_IN_SET(),顿时就产生了浓郁的兴趣,然后就搜了搜,翻了翻. 语法:FIND_IN_SET(str,strlist) 定义: 1. ...

  5. Tembin

    1:组织机构和用户之间是多对一的关系,一个组织结构可以有多个成员,一个成员只能属于一个组织机构. 2:app里面的邀请成员:是邀请发送短信通知用户注册tembin账户,当用户去注册的时候下面就会显示所 ...

  6. Spring4 MVC Hibernate4 maven集成

    http://www.cnblogs.com/leiOOlei/p/3727859.html

  7. NetScope脱机(localhost)使用[转】

    https://blog.csdn.net/jiwu999/article/details/79626773 方法: step1:git clone https://github.com/ethere ...

  8. React Router 4.0 基本使用

    路由的概念,起初来源于服务端,就是当浏览器访问一个网站的不同页面时,服务端能够正确的返回页面的内容.当访问首页时,它能返回首页的内容,访问关于我们页面时,返回关于我们的内容.可以看到路由就是一种控制和 ...

  9. Qt QLineEdit

    //lineEdit显示文字 QLineEdit *lineEdit = new QLineEdit(widget); lineEdit->setObjectName(QString()); l ...

  10. 第四十九天 mysql 索引 元类

    一 昨日回顾 视图 触发器 事务 什么是事务 逻辑上的一组操作 要么都成功 要么都失败 如何使用 start transaction 开启事务 mysql 默认一条sql就是一个事务 pymysql默 ...