【CF1141F2】Same Sum Blocks
题解:发现可以通过枚举区间将区间和相同的元组记录在一个表中,对于答案来说,在同一个表中的元组的选择才会对答案产生贡献。发现每一个表中都是一个个区间,问题转化成了对于每一个表来说,选择若干个不相交的区间,使得选择出来的区间数量最多,直接贪心即可。
代码如下
#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的更多相关文章
- 【CF1141F1】Same Sum Blocks
题目大意:给定一个 N 个值组成的序列,求序列中区间和相同的不相交区间段数量的最大值. 题解:设 \(dp[i][j]\) 表示到区间 [i,j] 时,与区间 [i,j] 的区间和相同的不相交区间数量 ...
- 【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 ...
随机推荐
- js获取数组中相同元素数量
<script> var array = new Array(1,2,5,1,4,4,2,3,5,1,1,5,5,5,6,7,3,9,9,10); var arr = new Array( ...
- csrf补充
问csrftoken在Django里面是基于什么实现的?------>中间件. 如果是Django表示每次发请求过来的时候,要检验有没有带随机字符串.当在执行视图函数之前,前面还有一道屏障,这个 ...
- redhat7通过yum安装nginx最新版
1.准备yum源 vi /etc/yum.repo.d/nginx.repo [nginx]name=nginx repobaseurl=http://nginx.org/packages/mainl ...
- spec文件中的 %pre %post %preun %postun
转载http://meinit.nl/rpm-spec-prepostpreunpostun-argument-values RPM has 4 parts where (shell) scripts ...
- IWMS后台上传文章,嵌入视频,调用优酷通用代码
<a href="http://player.youku.com/player.php/sid/XODcxNjM3OTYw/v.swf " target="_bla ...
- vue樣式綁定
vue的樣式可以使得class,style不僅可以綁定文本,而且可以綁定數組和對象. 使用對象{} 使用數組 綁定對象 使用computed屬性, 使用內聯樣式.
- BZOJ4205卡牌配对——最大流+建图优化
题目描述 现在有一种卡牌游戏,每张卡牌上有三个属性值:A,B,C.把卡牌分为X,Y两类,分别有n1,n2张. 两张卡牌能够配对,当且仅当,存在至多一项属性值使得两张卡牌该项属性值互质,且两张卡牌类别不 ...
- L - Vases and Flowers HDU - 4614 线段树+二分
题意 给出一排空花瓶 有两种操作 1是 从A花瓶开始放F朵花 如果当前瓶有花就跳过前往下一个 直到花用完或者 瓶子到了最后一个为止 输出 成功放花的第一个和最后一个 如果没有输出 can not. ...
- E - Just a Hook HDU - 1698 线段树区间修改区间和模版题
题意 给出一段初始化全为1的区间 后面可以一段一段更改成 1 或 2 或3 问最后整段区间的和是多少 思路:标准线段树区间和模版题 #include<cstdio> #include& ...
- python爬取豆瓣前25个影片内容的正则表达式练习
通过python正则表达式获取豆瓣top250的第一页的25个影片排名,影片名字,影片连接,导演,主演,上映日期,国家,剧情,评分,评价人数的内容 网页html内容: <ol class=&qu ...