[bzoj4511][Usaco2016 Jan]Subsequences Summing to Sevens

13 篇文章 0 订阅

4511: [Usaco2016 Jan]Subsequences Summing to Sevens

Time Limit: 10 Sec   Memory Limit: 128 MB
[ 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.

HINT

Source

膜数分类,7要特判
#include<iostream>
#include<cstdio>
int a[50005],b[10][2],n,ans=0;
int main(){
	scanf("%d", &n);
	for( int i = 1,x; i <= n; i++ ){
		scanf("%d", &a[i]); a[i] += a[i-1];
		a[i] %= 7; x = a[i];
		if( !b[x][0] ) b[x][0] = i;
		b[x][1] = i;
	}
	if( b[0][1] ) ans = std::max(1,b[0][1]-b[0][0]);
	for( int i = 1; i < 7; i++ ) ans = std::max(ans,b[i][1]-b[i][0]);
	printf("%d", ans);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值