小Biu的区间和

小Biu的区间和
时间限制: 1 Sec 内存限制: 128 MB

题目描述
小Biu去逛超市,超市有一个长度为n的货架,第i个位置摆放着价值为a[i]的商品,小Biu有很多好朋友,他想给好朋友们买一些礼物,但是小Biu又是一个很细心地人,他想让所有朋友收到的礼物的总和一样,而且送给每个朋友的礼物必须是位置连续的一段商品,小Biu想知道他最多可以给多少个好朋友送出礼物。
输入
第一行两个整数n(1<=n<=1000)。
第二行n个整数,第i个整数为第i个数字a[i],(1<=a[i]<=20)。
输出
输出一个数字表示答案。
样例输入 Copy
5
1 3 4 3 1
样例输出 Copy
3
提示
样例解释:[1,3] [4] [3,1] 可以分为三个不相交而且区间和相等的区间。

20%的数据中,1<=n<=10
50%的数据中,1<=n<=100
100%的数据中,1<=n<=1000

题目意思就是找出连续且相同的子区间和相同的数量最多是多少。
n比较小,可以从依次枚举每个点的前面的点,利用前缀和算出每个区间的和,让后贪心的选取每个能选的和,记录每个和的右区间的端点,之后每次选取的这个和的区间左端点必须在已经选取的右端点之后。

#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#define X first
#define Y second
using namespace std;

typedef long long LL;
typedef pair<int,int> PII;

const int N=1010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;

int n;
int a[N];
int ans;
map<int,int>cnt;
map<int,int>f;

int main()
{
//	ios::sync_with_stdio(false);
//	cin.tie(0);

	scanf("%d",&n);
	
	for(int i=1;i<=n;i++)
	{
		int x;
		scanf("%d",&x);
		a[i]=x+a[i-1];
	}
	
	for(int i=1;i<=n;i++)
	{
		for(int j=0;j<=i-1;j++)
		{
			int t=a[i]-a[j];
			if(f[t]<=j)
			{
				cnt[t]++;
				f[t]=i;
				ans=max(cnt[t],ans);
			}
		}
	}
		
	cout<<ans<<endl;
	


	return 0;
}










发布了43 篇原创文章 · 获赞 1 · 访问量 1573

猜你喜欢

转载自blog.csdn.net/DaNIelLAk/article/details/105161621
biu