传送门easy

传送门hard

切水题的感觉真好

看到数据范围这么小,所以暴力枚举所有的可能,然后用map+vector存下每种值的区间,然后贪心去选

代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<map>
using namespace std;
void read(int &x) {
char ch; bool ok;
for(ok=0,ch=getchar(); !isdigit(ch); ch=getchar()) if(ch=='-') ok=1;
for(x=0; isdigit(ch); x=x*10+ch-'0',ch=getchar()); if(ok) x=-x;
}
#define rg register
const int maxn=1.5e3+10;
map<int,vector<pair<int,int> > >mp;
int n,a[maxn],s[maxn],w[maxn*maxn],tot,ans,l[maxn],r[maxn],ll[maxn],rr[maxn];
bool solve(int k)
{
int sum=0,las=0;sort(mp[k].begin(),mp[k].end());int now=mp[k].size();
for(rg int i=0;i<now;i++)if(las<mp[k][i].second)las=mp[k][i].first,ll[++sum]=mp[k][i].second,rr[sum]=las;
if(sum>ans)memcpy(l,ll,sizeof l),memcpy(r,rr,sizeof r),ans=sum;
return 0;
}
int main()
{
read(n);
for(rg int i=1;i<=n;i++)read(a[i]),s[i]=s[i-1]+a[i];
for(rg int i=1;i<=n;i++)
for(rg int j=i;j<=n;j++)
w[++tot]=s[j]-s[i-1],mp[w[tot]].push_back(make_pair(j,i));
sort(w+1,w+tot+1);tot=unique(w+1,w+tot+1)-w-1;
for(rg int i=1;i<=tot;i++)if(solve(w[i]))break;
printf("%d\n",ans);
for(rg int i=1;i<=ans;i++)printf("%d %d\n",l[i],r[i]);
}

CF1141F Same Sum Blocks(easy/hard)的更多相关文章

  1. Codeforces Round #547 (Div. 3) F1/2. Same Sum Blocks (Easy/Hard) (贪心,模拟)

    题意:有一长度为\(n\)的数组,求最多的区间和相同的不相交的区间的个数. 题解:我们可以先求一个前缀和,然后第一层循环遍历区间的右端点,第二层循环枚举左端点,用前缀和来\(O(1)\)求出区间和,\ ...

  2. 1. Two Sum【easy】

    1. Two Sum[easy] Given an array of integers, return indices of the two numbers such that they add up ...

  3. Into Blocks (easy version)

    G1 - Into Blocks (easy version) 参考:Codeforces Round #584 - Dasha Code Championship - Elimination Rou ...

  4. F2. Same Sum Blocks (Hard) 解析(思維、前綴和、貪心)

    Codeforce 1141 F2. Same Sum Blocks (Hard) 解析(思維.前綴和.貪心) 今天我們來看看CF1141F2(Hard) 題目連結 題目 給你一個數列\(a\),要你 ...

  5. 【CF1141F2】Same Sum Blocks

    题解:发现可以通过枚举区间将区间和相同的元组记录在一个表中,对于答案来说,在同一个表中的元组的选择才会对答案产生贡献.发现每一个表中都是一个个区间,问题转化成了对于每一个表来说,选择若干个不相交的区间 ...

  6. 【CF1141F1】Same Sum Blocks

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

  7. [LeetCode] 437. Path Sum III_ Easy tag: DFS

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  8. leetcode 之 two sum (easy)c++

    1.数组的长度 length() .容器vector长度  size() .容器vector vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库. ...

  9. 1. Two Sum [Array] [Easy]

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

随机推荐

  1. 开始使用Python

    1. 开始使用Python 1.1 print使用str()函数显示对象,而交互式解释器调用repr()函数来显示对象. 1.2 在解释器中_表示最后一个表达式的值. 1.3 >>用来重定 ...

  2. C# nunit 单元测试

    1. 引包 nunit.framework.dll

  3. SpringMVC的API和Spring的官方说明文档的地址。

    SpringMVC的API和Spring的官方说明文档的地址. 1.SpringMVC的API的URL: http://docs.spring.io/spring/docs/current/javad ...

  4. c语言学习的第13天2

    #include <stdio.h> #include <malloc.h> void f(int **q) { *q=(int *)malloc(sizeof(int)); ...

  5. spawning cl.exe

    可能很多人在安装VC 6.0后有过点击“Compile”或者“Build”后被出现的“Compiling... ,Error spawning cl.exe”错误提示给郁闷过.很多人的选择是重装,实际 ...

  6. [干货]兼容HTML5的Placeholder属性-更新版v0.10102013

    HTML5对Web Form做了许多增强,比如input新增的type类型.Form Validation等.Placeholder是HTML5新增的另一个属性,当input或者textarea设置了 ...

  7. hdu-5793 A Boring Question(二项式定理)

    题目链接: A Boring Question Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java ...

  8. [Selenium] 处理表格(python + java)

    python : https://www.cnblogs.com/yan-xiang/p/6819168.html 操作内容:获取table总行数.总列数.获取某单元格的text值,删除一行[如果每行 ...

  9. ACM学习历程—HDU 1272 小希的迷宫(并查集)

    Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就 ...

  10. HP SiteScope安装

    下载地址以及安装方法见 http://www.jianshu.com/p/fce30e333578 数据库连接URL:jdbc:mysql://mysql_ip:mysql_port/database ...