Codeforces Round #547 (Div. 3)F2. Same Sum Blocks (Hard)
时间:2022-07-25
本文章向大家介绍Codeforces Round #547 (Div. 3)F2. Same Sum Blocks (Hard),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
F2. Same Sum Blocks (Hard)
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
This problem is given in two editions, which differ exclusively in the constraints on the number nn.
You are given an array of integers a[1],a[2],…,a[n].a[1],a[2],…,a[n]. A block is a sequence of contiguous (consecutive) elements a[l],a[l+1],…,a[r]a[l],a[l+1],…,a[r] (1≤l≤r≤n1≤l≤r≤n). Thus, a block is defined by a pair of indices (l,r)(l,r).
Find a set of blocks (l1,r1),(l2,r2),…,(lk,rk)(l1,r1),(l2,r2),…,(lk,rk) such that:
- They do not intersect (i.e. they are disjoint). Formally, for each pair of blocks (li,ri)(li,ri) and (lj,rj(lj,rj) where i≠ji≠j either ri<ljri<lj or rj<lirj<li.
- For each block the sum of its elements is the same. Formally, a[l1]+a[l1+1]+⋯+a[r1]=a[l2]+a[l2+1]+⋯+a[r2]=a[l1]+a[l1+1]+⋯+a[r1]=a[l2]+a[l2+1]+⋯+a[r2]= ⋯=⋯= a[lk]+a[lk+1]+⋯+a[rk].a[lk]+a[lk+1]+⋯+a[rk].
- The number of the blocks in the set is maximum. Formally, there does not exist a set of blocks (l′1,r′1),(l′2,r′2),…,(l′k′,r′k′)(l1′,r1′),(l2′,r2′),…,(lk′′,rk′′)satisfying the above two requirements with k′>kk′>k.
题意:在一个给定序列中找到最大数目的不交叉子段和相同
思路:数据范围很小1500,可以用o(n^2)复杂度预处理出所有子段和,存入vector中,对于一个特定的sum相同的各种子段
采用贪心策略,[l,r]子段,取有边界尽量小的,因为要数目最多嘛
// luogu-judger-enable-o2
#include<bits/stdc++.h>
#include<unordered_set>
#define rg register ll
#define inf 2147483647
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
#define ll long long
#define maxn 200005
const double eps = 1e-8;
using namespace std;
inline ll read()
{
char ch = getchar(); ll s = 0, w = 1;
while (ch < 48 || ch>57) { if (ch == '-')w = -1; ch = getchar(); }
while (ch >= 48 && ch <= 57) { s = (s << 1) + (s << 3) + (ch ^ 48); ch = getchar(); }
return s * w;
}
inline void write(ll x)
{
if (x < 0)putchar('-'), x = -x;
if (x > 9)write(x / 10);
putchar(x % 10 + 48);
}
inline bool cmp(const pair<ll,ll>&a,const pair<ll,ll>&b)
{
return a.second<b.second;
}
int main()
{
ll n=read();
vector<ll>a(n+5);
map<ll,vector<pair<ll,ll>>>p;
for(rg i=1;i<=n;i++)a[i]=read();
for(rg i=1;i<=n;i++)
{
ll sum=0;
for(rg j=i;j<=n;j++)
{
sum+=a[j];
p[sum].push_back(make_pair(i,j));
}
}
ll ans=0;
vector<pair<ll,ll>>res;
for(auto it:p)
{
vector<pair<ll,ll>>now=it.second;
sort(now.begin(),now.end(),cmp);
vector<pair<ll,ll>>temp;
ll cur=0,r=-1;
for(auto itt:now)
{
if(itt.first>r)
{
cur++;
temp.push_back(itt);
r=itt.second;
}
}
if(cur>ans)
{
ans=cur;
res=temp;
}
}
cout<<ans<<endl;
for(auto it:res)
{
cout<<it.first<<" "<<it.second<<endl;
}
return 0;
}
- JavaScript Window - 浏览器对象模型
- 纯CSS实现的圆角折叠菜单特效代码
- MySQL和Oracle中的半连接测试总结(一)(r10笔记第31天)
- 【Go 语言社区】关于select和channel数组的配合使用--转
- Java基础-day09-代码题-对象;类;封装
- MySQL replace into的使用细则(r10笔记第48天)
- Win10下用Anaconda安装TensorFlow
- 【Go 语言社区】跨域问题解决方案:jsonP客户端和服务器代码
- 图;代码轻松理解,代理
- 巧用闪回数据库来查看历史数据 (r10笔记第47天)
- 【Go 语言社区】Golang内存分配
- 小白也能懂的手写体识别
- 【Go 语言社区】浅析javascript的间隔调用和延时调用
- 说说JSON和JSONP,也许你会豁然开朗-转
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法