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

代码如下

#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. (二)类数组对象HTMLCollection

    HTMLCollection 表示 HTML 元素的集合. 下面的几种方式将返回 HTMLCollection对象: html: <body> <ul id="box&qu ...

  2. Codeforces 1154F Shovels Shop

    题目链接:http://codeforces.com/problemset/problem/1154/F 题目大意: 商店有n把铲子,欲购k把,现有m种优惠,每种优惠可使用多次,每种优惠(x, y)表 ...

  3. 关于手机端适配的问题(rem,页面缩放)

    关于手机端适配的问题(rem,页面缩放) 96 进击的小前端 关注 2018.02.02 13:57 字数 320 阅读 19评论 0喜欢 0 相信很多和会和我碰到一样的情况,就是你用rem去写移动端 ...

  4. php5.6.x到php7.0.x特性

    php5.6.x到php7.0.x特性 1.标量类型声明 字符串(string), 整数 (int), 浮点数 (float), 布尔值 (bool),callable,array,self,Clas ...

  5. Linux基础学习笔记1

    MBR分区 主分区: 1-4,一块硬盘最多四个主分区,对主机必须有,主区可以格式化ntfs,存数据: 扩展分区:1-4,一块硬盘最多一个扩展分区,可以没有扩展分区,划分更小的单元,即逻辑分区: 逻辑分 ...

  6. 如何通过stat获取目录或文件的权限的数字形式

    man stat 查看帮助. -c --format=FORMAT use the specified FORMAT instead of the default; output a new line ...

  7. wget 下载网页

    如有转载,不胜荣幸.http://www.cnblogs.com/aaron-agu/ wget --http-user=username --http-passwd=password http:/w ...

  8. python 脚本之 IP地址探测

    #第一种方法#!/usr/bin/env python #_*_ coding:utf8 _*_ #### 该脚本需要使用fping命令 如果没有安装需要提前安装fping #### yum inst ...

  9. BZOJ2428[HAOI2006]均分数据——模拟退火

    题目描述 已知N个正整数:A1.A2.…….An .今要将它们分成M组,使得各组数据的数值和最平均,即各组的均方差最小.均方差公式如下: ,其中σ为均方差,是各组数据和的平均值,xi为第i组数据的数值 ...

  10. shelve 模块

    shelve 模块概述:   shelve是python的自带model.   可以直接通过import shelve来引用.   shelve类似于一个存储持久化对象的持久化字典,即字典文件.   ...