POJ Gold Balanced Lineup 3274 Hash

Source: Internet
Author: User
Tags hash


Gold Balanced Lineup

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 14640 Accepted: 4249

Description

Farmer John ' s N cows (1≤n≤100,000) share many similarities. In fact, FJ have been able to narrow down the list of features gkfx by he cows to a list of Onlyk different features (1 ≤K≤30). For example, cows exhibiting feature #1 might has spots, cows exhibiting feature #2 might prefer C to Pascal, and so on.

FJ has even devised a concise-to-describe each cow in terms of their "feature ID", a single k-bit integer whose binary r Epresentation tells us the set of features exhibited by the cow. As an example, suppose a cow have feature ID = 13. Since written in binary was 1101, this means we cow exhibits features 1, 3, and 4 (reading right to left), but not feat Ure 2. More generally, we find a 1 in the 2^ (i-1) place if a cow exhibits featurei.

Always the sensitive fellow, FJ lined up cows 1..N in a long row and noticed that certain ranges of cows is somewhat "bal Anced "In terms of the features the exhibit. A contiguous range of cowsi. J is balanced if each of the K possible features are exhibited by the same number of cows in the range. FJ is curious as to the size of the largest balanced range of cows. See if you can determine it.

Input line 1:two space-separated integers, N and K.
Lines 2.. N+1:line i+1 contains a single k-bit integer specifying the features present in Cow I. The least-significant bit of this integer are 1 if the Cow Exhibits feature #1, and the most-significant bit is 1 if the CO W exhibits feature # K.

Output Line 1: A single integer Giving the size of the largest contiguous balanced group of cows.

Sample Input

7 3
7
6
7
2
1
4
2

Sample Output

4

Hint in the range from cow #3 to cow #6 (of size 4), each feature appears in exactly 2 cows in this range

Source Usaco March Gold


Let's ask for an interval in which each of these features is the same.

7 1 1) 1 1

6 0 1) 1 2

7 1 1) 1 3

2 0 1) 0 4

1 1 0) 0 5

4 0 0) 1 6

2 0 1) 0 7


Cumulative by line:

1 1 1

1 2 2

2 3 3

2 4 3

3 4 3

3 4 4

3 5 4

are subtracted from the first column:

0 0 0

0 1 1

0 1 1

0 2 1

0 1 0

0 1 1

0 2 1 So, the maximum interval is 6-2 = 4.

by row, only each feature in this interval is equal, the two ends of the interval are subtracted from the right column, so we find the furthest equal.

record with a hash, and then compare

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <vector> #include <
Algorithm> using namespace std;
#define PRIME 501 Vector<int >ha[505];
int c[100001][35],ma=0,tot,sum[100001][35],n,m;
    int cmp (int x,int y)//compare {int t=0;
    while (C[x][t]==c[y][t] && t<m) t++;
    if (t==m) return 1;
return 0;
    } int main () {int i,j,l;
    Long long X;
    scanf ("%d%d", &n,&m);
    memset (sum,0,sizeof (sum));
    Memset (C,0,sizeof (c));
        for (i=1;i<=n;i++) {scanf ("%lld", &x);
            for (j=0;j<m;j++)//Calculate feature binary {l=x%2;
        x=x/2;//in addition to 2 sum[i][j]=sum[i-1][j]+l;//by row accumulation  }} for (i=1;i<=n;i++) {int su=0;
        tot=0;
            for (j=0;j<m;j++) {c[i][j]=sum[i][j]-sum[i][0];//minus the right column if (!c[i][j]) tot++;
        Su= (Su+c[i][j])%prime;
        } su=abs (SU); if (Tot==m && I>ma) ma=i;//If all 0 of the situation, directly find out ha[su].push_back (i);
        } for (i=0;i<prime;i++)//Compare {int ll=ha[i].size ();
                if (ll>1) for (int. k=0;k<ll-1;k++) {for (j=k+1;j<ll;j++) {
                if (CMP (Ha[i][k],ha[i][j]) && ha[i][j]-ha[i][k]>ma) {ma=ha[i][j]-ha[i][k];
    }}}} printf ("%d\n", MA);
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.