poj 3274 Gold Balanced Lineup

来源:互联网 发布:尹美莱 知乎 编辑:程序博客网 时间:2024/05/16 13:45
Gold Balanced Lineup
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 14633 Accepted: 4246

Description

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

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

Always the sensitive fellow, FJ lined up cows 1..N in a long row and noticed that certain ranges of cows are somewhat "balanced" in terms of the features the exhibit. A contiguous range of cows i..j is balanced if each of the K possible features is 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 is 1 if the cow exhibits feature #1, and the most-significant bit is 1 if the cow exhibits feature #K.

Output

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

Sample Input

7 37672142

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

以下转载
/*

说实在话对于英文渣渣的我读题真的很难。

下面是题目大意,对提供者表示感谢。


代码操作如下:
1、先将十进制数转换成二进制数记录保存。

2、然后逐行累加,得出到某只牛时某种特征出现了几次。

3、每行减去第一个数,得出一个序列,若两头牛之间是平衡区间的话,各个特征的增长数是相等的,及减掉第一个数得出来的序列是相等的。

4、寻找距离最远的两个相等的序列,得出答案。

我们可以由样例举例:

转换成二进制特征值为:

数字  特征值    第几头牛

   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

按行累加得:

1 1 1

1 2 2

2 3 3

2 4 3

3 4 3

3 4 4

3 5 4

都减去第一列得:

0 0 0

0 1 1

0 1 1

0 2 1

0 1 0

0 1 1

0 2 1

所以说  最大区间是  6-2 = 4.

有一种特殊情况就是当到了某一行出现全都是零的情况,例如:

0 1 2 1 1 0 2

0 0 0 2 1 1 1

0 0 0 0 0 0 0 

我们需要在所有序列之前加一行0以方便比较。

*/

为什么要全部下加呢?
因为你想啊!在一段区间如果特征出现次数相同就会从上(这里指在这个区间的上)到下(这里指在这个区间的下)一行上两两的差值是相同的,如例题
2 3 3到3 4 4是不是两两差值相同,,然后我们其实就是再找差值相同的两行,怎么找方便呢?
当然是他们有相同的值我们查找起来方便了,正好利用了他们的差值相同,我们就寻找差值就行了?是吧!!


这里我还要说明一下为什么前面要加上额外的一行0了
大家可以亲手试验一下当转码为

1 0 0
0 1 0
0 0 1
肯定长度是3了
你如果不在前面加一行0就会出现
1 0 0
1 1 0
1 1 1

0 -1 -1
0 0 -1
0 0 0
你会发现,用这种方法竟然没有匹配的,,,,,,


<pre name="code" class="cpp">#include <string.h>#include <stdio.h>#include <iostream>#include <algorithm>#define M 100005#define N 35using namespace std;int num[M];struct node{    int min1;    int num1[N];}bin[M];int n,m;int cmp2 ( node x, node y )//Qsort的比较函数{    for( int i = 0; i < m; i++ )    {        if ( x.num1[i] != y.num1[i] )        {            return x.num1[i] < y.num1[i];        }    }    return x.min1 < y.min1;}void Binary(){    int i, j;    int BIN_size = m;    for ( i = 0; i <= n ; i++ )    {        j = 0;        while ( num[i] != 0 )        {            bin[i].num1[j++] = num[i]%2;            num[i] = num[i]/2;        }        while ( j <= BIN_size )        {            bin[i].num1[j++] = 0;        }    }}void Accu (){    int i, j;    for ( i = 1 ; i <= n ; i++ )    {        for ( j = 0; j < m ; j++ )        {            bin[i].num1[j] = bin[i-1].num1[j] + bin[i].num1[j];        }    }}void Sub (){    int i, j;    for ( i = 0 ; i <= n; i++ )    {        for ( j = 1; j < m ; j++ )        {            bin[i].num1[j] = bin[i].num1[j] - bin[i].num1[0];        }        bin[i].num1[0] = 0;    }}int compace ( int j ,int k){    for ( int i = 0;i < m; i++ )    {        if( bin[j].num1[i] != bin[j-k].num1[i] )            return 0;    }    return 1;}int main(){    int i, j;    while ( ~scanf ( "%d %d", &n, &m ) )    {        num[0] = 0;        bin[0].min1 = 0;        for ( i = 1; i <= n ; i++ )        {            scanf ( "%d", &num[i] );            bin[i].min1 = i;        }        /*Transfer number into a binary*/        Binary ();          /*Accumulation of number*/          Accu ();          /*The data were subtracted*/          Sub ();          /*The test data*///        for ( i = 0;i <= n ; i++ )//        {//            for ( j = 0; j < m ; j++ )//            {//                printf("%d ",bin[i].num1[j]);//            }//            printf("\n");//        }               /*Order sort*/          sort(bin,bin+n+1,cmp2);                              int max1 = 0;          j = 1;          for ( i = 1;i <= n; i++ )          {              if ( compace(i, j) )              {                  if ( max1 < bin[i].min1 - bin[i-j].min1 )                  {                      max1 = bin[i].min1 - bin[i-j].min1;                  }                  i = i-1;                  j = j+1;              }              else              {                  j = 1;              }          }          printf ( "%d\n", max1 );    }}



代码菜鸟,如有错误,请多包涵!!!
0 0