BZOJ 4511: [Usaco2016 Jan]Subsequences Summing to Sevens

4511: [Usaco2016 Jan]Subsequences Summing to Sevens

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 197  Solved: 149
[Submit][Status][Discuss]

Description

Farmer John's N cows are standing in a row, as they have a tendency to do from time to time. Each cow is labeled with a distinct integer ID number so FJ can tell them apart. FJ would like to take a photo of a contiguous group of cows but, due to a traumatic childhood incident involving the numbers 1…6, he only wants to take a picture of a group of cows if their IDs add up to a multiple of 7.
Please help FJ determine the size of the largest group he can photograph.
The first line of input contains N(1≤N≤50,000). The next N lines each contain the N integer IDs of the cows (all are in the range 0…1,000,000
).
给定长度为N的数列A,求A最长的连续子序列,满足子序列中元素的和是7的倍数。输出最大的长度。如果不存在满
足条件的子序列,输出0。1 ≤ N ≤ 50000, 0 ≤ A_i ≤ 10 ^ 6

Input

Please output the number of cows in the largest consecutive group whose IDs sum to a multiple of 7. If no such group exists, output 0.

Output

You may want to note that the sum of the IDs of a large group of cows might be too large to fit into a standard 32-bit integer. If you are summing up large groups of IDs, you may therefore want to use a larger integer data type, like a 64-bit "long long" in C/C++.

Sample Input

7
3
5
1
6
2
14
10

Sample Output

5
In this example, 5+1+6+2+14 = 28.

刷水有益健康

维护前缀和就行了

BJ处理了一下连续0的情况

搞的rank超低


#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<complex>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<map>
#include<set>
using namespace std;

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch<='9'&&ch>='0'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
	return x*f;
}
void print(int x)
{if(x<0)putchar('-'),x=-x;if(x>=10)print(x/10);putchar(x%10+'0');}

const int N=50010;

int con[N],sum[N],last[7];

int main()
{
	int n=read();
	register int i,x,ans=0;
	for(i=1;i<7;++i)last[i]=n;
	for(i=1;i<=n;++i)
	{
		con[i]=con[i-1];sum[i]=sum[i-1];
		x=read();
		if(x){x%=7;con[i]++;(sum[i]+=x)%=7;}
		last[sum[i]]=min(last[sum[i]],i);
		if(con[i]-con[last[sum[i]]]>0)ans=max(ans,i-last[sum[i]]);
	}
	print(ans);puts("");return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值