[TOJ 5065] the longest continuous subsequence and the longest toj5065

Source: Internet
Author: User

[TOJ 5065] the longest continuous subsequence and the longest toj5065

Description

Given a series of non-negative integers, find the longest continuous subsequence so that it is a multiple of 7.

Input

The first positive integer is N (1 <=n <= 50000), followed by N rows, each row has a non-negative integer, and All integers are not greater than 10 ^ 6.

Output

If a continuous subsequence exists and the subsequence is a multiple of 7, the length of the subsequence is output. If the subsequence does not exist, 0 is output.

Sample input

7
3
5
1
6
2
14
10

Sample output

5

#include<iostream> #include<cstring>#include<algorithm>using namespace std;int main(){    int n,i,j,a[50001],right[9],left[9],maxx=0,len;    cin>>n;    memset(right,-1,sizeof(right));    memset(left,-1,sizeof(left));    a[0]=0;    left[0]=0;    for(i=1;i<n;i++)    {        scanf("%d",&a[i]);        a[i]=(a[i-1]+a[i])%7;        if(left[a[i]]==-1)        left[a[i]]=i;    }    for(i=n-1;i>=0;i--)    {        if(right[a[i]]==-1)            right[a[i]]=i;    }    for(i=0;i<=8;i++)    {        for(j=0;j<=8;j++)        {            if(right[i]>=0&&left[j]>=0&&i==j)            {                len=right[i]-left[j];                maxx=max(len,maxx);            }        }    }    cout<<maxx<<endl;    return 0;}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.