Codeforces Round #547 (Div. 3)F1. Same Sum Blocks (Easy)

time limit per test

2 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.

The picture corresponds to the first example. Blue boxes illustrate blocks.

Write a program to find such a set of blocks.

Input

The first line contains integer nn (1≤n≤501≤n≤50) — the length of the given array. The second line contains the sequence of elements a[1],a[2],…,a[n]a[1],a[2],…,a[n] (−105≤ai≤105−105≤ai≤105).

Output

In the first line print the integer kk (1≤k≤n1≤k≤n). The following kk lines should contain blocks, one per line. In each line print a pair of indices li,rili,ri (1≤li≤ri≤n1≤li≤ri≤n) — the bounds of the ii-th block. You can print blocks in any order. If there are multiple answers, print any of them.

Examples

input

Copy

7
4 1 2 2 1 5 3

output

Copy

3
7 7
2 3
4 5

input

Copy

11
-5 -4 -3 -2 -1 0 1 2 3 4 5

output

Copy

2
3 4
1 1

input

Copy

4
1 1 1 1

output

Copy

4
4 4
1 1
2 2
3 3

看了别的大佬写的

#include<bits/stdc++.h>

using namespace std ;

int a[55];

int main()
{
	int n;
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	map<int,int> mmp,ma;
	map<int,int>::iterator it;
    //找个数最多的区间和值
	for (int i=1;i<=n;i++)//i表示所找区间的右端点
	{
		int sum = 0;
		for (int j=i;j>=1;j--)
		{
			sum += a[j];
			if (mmp[sum]<j)//如果相同的sum而右端点更小则更新,因为这才能保证区间最多
			{
				mmp[sum] = i;
				ma[sum]++;
			}
		}
	}
	int ans = -1;
	for (it=ma.begin();it!=ma.end();it++)
	{
		if (it->second>ma[ans])
		{
			ans = it->first;
		}
	}
	printf("%d\n",ma[ans]);
	int r = 0;
    //和前面一样,只不过这是遍历区间
	for (int i=1;i<=n;i++)
	{
		int sum = 0;
		for (int j=i;j>r;j--)
		{
			sum += a[j];
			if (sum==ans)
			{
				printf("%d %d\n",j,i);
				r = i;
			}
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40912854/article/details/89366323