题目大意:给定一个 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. eclipse 部署项目

  2. Keras和tensorflow的区别

    参考: https://blog.csdn.net/zhangbaoanhadoop/article/details/82111056

  3. Java线程的5种状态及切换(透彻讲解)-京东面试

    一.Thread的几个重要方法: 我们先了解一下Thread的几个重要方法. a.start()方法,开始执行该线程:b.stop()方法,强制结束该线程执行:c.join方法,等待该线程结束.d.s ...

  4. centos7根分区扩容(亲测有效)

    root@haojftest:~# df -h 文件系统 容量 已用 可用 已用% 挂载点 /dev/mapper/centos_test2-root 28G 14G 15G % / devtmpfs ...

  5. 微信小程序支付功能

    API:wx.requestPayment() { } https://blog.csdn.net/qishubiao/article/details/80804052

  6. dreamweavercs 和dreamweaver cc的區別

    https://zhidao.baidu.com/question/1541178469432885667.html

  7. Redis 禁用FLUSHALL FLUSHDB KEYS 命令

      (error) ERR unknown command 'keys'问题解决(error) ERR unknown command 'FLUSHDB' 问题解决 背景 FLUSHALL FLUSH ...

  8. Hibernate最佳实战

    1:一对一,一对多,多对多双向管理必设mappedBy ,将关系交给乙方维护,不然的话会在双方都建立关系,比如一对一双向的时候双方都会保存对方的id外键管理 具体在项目中采用双向还是单项看实际情况. ...

  9. no module named 'win32api'问题

    运行scrapy时,报错no module named 'win32api' 解决办法: https://github.com/mhammond/pywin32/releases 下载对于python ...

  10. HUST 1541 解方程

    参考自:https://www.cnblogs.com/ECJTUACM-873284962/p/6394836.html 1541 - Student’s question 时间限制:1秒 内存限制 ...