【CF1141F1】Same Sum Blocks
题目大意:给定一个 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的更多相关文章
- 【CF1141F2】Same Sum Blocks
题解:发现可以通过枚举区间将区间和相同的元组记录在一个表中,对于答案来说,在同一个表中的元组的选择才会对答案产生贡献.发现每一个表中都是一个个区间,问题转化成了对于每一个表来说,选择若干个不相交的区间 ...
- 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)
[LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...
- 【Leetcode】404. Sum of Left Leaves
404. Sum of Left Leaves [题目]中文版 英文版 /** * Definition for a binary tree node. * struct TreeNode { * ...
- 【LibreOJ】【LOJ】#6220. sum
[题意]对于n个数,找出一些数使得它们的和能被n整除,输出任意一组方案,n<=10^6. [算法]构造/结论 [题解]引用自:http://www.cnblogs.com/Sakits/p/74 ...
- 【LeetCode】Two Sum II - Input array is sorted
[Description] Given an array of integers that is already sorted in ascending order, find two numbers ...
- 【LeetCode】633. Sum of Square Numbers
Difficulty: Easy More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/sum-of-square-n ...
- 【leetcode】907. Sum of Subarray Minimums
题目如下: 解题思路:我的想法对于数组中任意一个元素,找出其左右两边最近的小于自己的元素.例如[1,3,2,4,5,1],元素2左边比自己小的元素是1,那么大于自己的区间就是[3],右边的区间就是[4 ...
- 【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 ...
- 【leetcode】Path Sum
题目简述: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding ...
随机推荐
- jenkins配置SSH远程服务器连接
之前用jenkins做了一个自动发布测试,配置任务的Post Steps时,选择的是执行shell命令.如下图: 这是在本192.168.26.233服务器上测试的,此服务器上运行jenkins,to ...
- AngularJS 中的 factory、 service 和 provider区别,简单易懂
转自:http://blog.csdn.net/ywl570717586/article/details/51306176 初学 AngularJS 时, 肯定会对其提供 factory . serv ...
- 前K个高频元素
给定一个非空的整数数组,返回其中出现频率前 k 高的元素. 示例 1: 输入: nums = [1,1,1,2,2,3], k = 2 输出: [1,2] 示例 2: 输入: nums = [1], ...
- Client将数据读写HDFS流程
HDFS介绍 HDFS(Hadoop Distributed File System )Hadoop分布式文件系统.是根据google发表的论文翻版的. 什么是分布式文件系统 分布式文件系统(Dist ...
- EXAMPLE FOR PEEWEE 多姿势使用 PEEWEE
使用 PEEWEE 断断续续的差不多已经三个年头了,但是没有像这次使用这么多的特性和功能,所以这次一并记录一下,需要注意的地方和一些使用细节,之后使用起来可能会更方便. 因为是使用的 SQLAched ...
- python 钉钉机器人发送消息
import json import requests def sendmessage(message): url = 'https://oapi.dingtalk.com/robot/send?ac ...
- WPF中元素拖拽的两个实例
今天结合之前做过的一些拖拽的例子来对这个方面进行一些总结,这里主要用两个例子来说明在WPF中如何使用拖拽进行操作,元素拖拽是一个常见的操作,第一个拖拽的例子是将ListBox中的子元素拖拽到ListV ...
- vue組件
組件有局部組件和全局組件,全局組件,其它的元素能夠調用. Prop父組件子組件看不大明白.
- 【转】console.log 用法
标签: 转自http://www.cnblogs.com/ctriphire/p/4116207.html 大家都有用过各种类型的浏览器,每种浏览器都有自己的特色,本人拙见,在我用过的浏览器当中,我是 ...
- C#,单元测试
C#,单元测试入门(以下内容可能来自网络) 一.什么叫单元测试(unit testing)? 是指对软件中的最小可测试单元进行检查和验证.对于单元测试中单元的含义,一般来说,要根据实际情况去判定其具体 ...