【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 ...
随机推荐
- day 7-21 pymysql模块
一.安装的两种方法 第一种: #安装 pip3 install pymysql 第二种: 二.链接,执行sql,关闭(游标) import pymysql user = input("use ...
- Django的分页和中间件
一.分页 Django的分页器(paginator) view.py from django.shortcuts import render,HttpResponse # Create your vi ...
- 用junit对java代码进行测试,需要注意
1.用@Test注解的方法必须没有返回值,返回值类型无:void 2.用@Test注解的方法必须没有参数.
- 前端传送JSON数据,报Required request body is missing
声明: 后端为Java,采用SSM框架 前端一个JSON.stringify()传来的json字符串,后端一般用@RequestBody标签来定义一个参数接收 但问题在于,当我使用get方式传JSON ...
- 剖析插件 DataTable 自定义列表列get请求如何书写传递的参数
重点代码Demo 已用 斜体标注 后端flask: @task_mgm.route('/taskinfo_editID=<int:num>', methods=['GET', 'POST' ...
- Sql server 系统表
sql server系统表详细说明 SQL Server 用户库中系统表说明 名称 说明 备注 syscolumns 每个表和视图中的每列在表中占一行,存储过程中的每个参数在表中也占一行. sys ...
- ADO工具类
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data; ...
- CodeForces615B-Longtail Hedgehog-dp/图
记忆化数组记录从这个点的最长下降序列,然后乘以这个点的度,就是ans,维护即可. #include <cstdio> #include <cstring> #include & ...
- 【BZOJ3997】【TJOI2015】组合数学 Dilworth定理 DP
题目描述 有一个\(n\times m\)的网格图,其中某些格子有财宝,每次从左上角出发,只能向下或右走.问至少走多少次才能将财宝捡完. 此对此问题变形,假设每个格子中有好多财宝,而每一次经过一个格子 ...
- 【AGC014E】Blue and Red Tree 并查集 启发式合并
题目描述 有一棵\(n\)个点的树,最开始所有边都是蓝边.每次你可以选择一条全是蓝边的路径,删掉其中一条,再把这两个端点之间连一条红边.再给你一棵树,这棵树的所有边都是红边,问你最终能不能把原来的树变 ...