题解:发现可以通过枚举区间将区间和相同的元组记录在一个表中,对于答案来说,在同一个表中的元组的选择才会对答案产生贡献。发现每一个表中都是一个个区间,问题转化成了对于每一个表来说,选择若干个不相交的区间,使得选择出来的区间数量最多,直接贪心即可。

代码如下

#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
const int maxn=1501; int n,a[maxn],sum[maxn],ans;
map<int,vector<P>> mp;
vector<P> rec; bool cmp(P a,P b){return a.second<b.second;} 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++)
mp[sum[j]-sum[i-1]].push_back(make_pair(i,j));
auto p=mp.begin();
for(;p!=mp.end();p++){
vector<P> &res=p->second;
sort(res.begin(),res.end(),cmp);
int cnt=0,last=-1;
vector<P> tmp;
for(int i=0;i<res.size();i++)
if(res[i].first>last){
++cnt,last=res[i].second;
tmp.push_back(res[i]);
}
if(ans<cnt)ans=cnt,rec=tmp;
}
printf("%d\n",ans);
for(int i=0;i<rec.size();i++)printf("%d %d\n",rec[i].first,rec[i].second);
} int main(){
solve();
return 0;
}

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

  1. 【CF1141F1】Same Sum Blocks

    题目大意:给定一个 N 个值组成的序列,求序列中区间和相同的不相交区间段数量的最大值. 题解:设 \(dp[i][j]\) 表示到区间 [i,j] 时,与区间 [i,j] 的区间和相同的不相交区间数量 ...

  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. Day 5-3 多态与多态性

    多态与多态性 鸭子类型 多态与多态性 多态:一类事物有多种形态.比如,动物有多种形态,人,狗,猪,豹子.水也有多种形态,冰,雪,水蒸气. #多态:同一类事物的多种形态 import abc class ...

  2. @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})

    @EnableAutoConfiguration 作用:Spring Boot会自动根据你jar包的依赖来自动配置项目. 例如当你项目下面有HSQLDB的依赖时,Spring Boot会创建默认的内存 ...

  3. Java8 flatMap的sample

    外国人写得, 很不错 http://www.java67.com/2016/03/how-to-use-flatmap-in-java-8-stream.html package test; impo ...

  4. mysql “Too many connections” 解决办法

    今天生产服务器上的MySQL出现了一个不算太陌生的错误“Too many connections”.平常碰到这个问题,我基本上是修改/etc/my.cnf的max_connections参数,然后重启 ...

  5. Delphi 工具条按钮上的下拉菜单

    制作步骤: 1.添加一个 TImageList: ImageList1, 然后载入些图标; 2.添加两个 TPopupMenu: PopupMenu1.PopupMenu2, 并分别添加些菜单项; 3 ...

  6. 二、core abp 数据库迁移

    一.数据库迁移-ABP(库) 1.配置链接数据库:  贴以下代码: { "ConnectionStrings": { "Default": "Serv ...

  7. Ajax 调用的WCF

    支持ajax 跨域调用的WCF搭建 1.新建一个"ASP.NET空Web应用程序"项目. 2.新建一个“WCF服务(支持ajax)”. 3.修改WCFAjaxService.svc ...

  8. 51nod2383

    2383 高维部分和 1 秒 131,072 KB 80 分 5 级题   输入一个长度为n的数组a[i],下标从0开始(0到n-1)保证n是2的整数次幂,对于每个i (0 <= i < ...

  9. poj-1724(bfs+优先队列)

    题意:有向图,给你m条边,每条边有两个权值,路径长和通过这条路径的花费,问你在不超过k花费的前提下,最短的路径从1走到n 解题思路:因为边数很少,我们可以直接用暴力每条边的方式来找最小的路径长,也就是 ...

  10. HTML5获取地理位置信息

    <!DOCTYPE html> <html> <head> <title>Location</title> <meta charset ...