前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Codeforces Round #547 (Div. 3)F2. Same Sum Blocks (Hard)

Codeforces Round #547 (Div. 3)F2. Same Sum Blocks (Hard)

作者头像
glm233
发布2020-09-28 10:39:54
3620
发布2020-09-28 10:39:54
举报

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]子段,取有边界尽量小的,因为要数目最多嘛

代码语言:javascript
复制
// 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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/08/09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档