Gold Balanced Lineup - POJ 3274 哈希

Gold Balanced Lineup
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 11867   Accepted: 3487

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 theK 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 3
7
6
7
2
1
4
2

Sample Output

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.

用哈希和vector去做,我的时间一般,782MS,注意有全为0的情况。另附测试数据。

#include
#include
#include
using namespace std;
struct node
{ int val[35];
}cow[100010];
vector hash[23333];
int sum[35];
int a[35],k;
bool compare(int pos)
{ bool flag=true;
  for(int i=1;i<=k;i++)
   if(a[i]!=cow[pos].val[i])
    flag=false;
  return flag;
}
int solve(int pos)
{ int s=0,i;
  bool flag=false,flag2=true;
  for(i=1;i<=k;i++)
  { s*=49;
    s+=a[i];
    s%=23330;
    if(s<0)
     s+=23330;
  }
  for(i=0;i

1.边界:
注意以下情况:
4 4
1
2
4
8
====和====
4 4
8
1
2
4
如果输出不为4的话小心。
还有:
5 4
3
1
2
4
8
注意一下。这个输出应该也为4.

2.小心输出为零:
1 5
3
这组输出为0,而
1 2
3
输出1.
外加一组:
8 4
1
2
4
8
1
2
4
8
输出应该为8,全为0的情况不能加入vector中



你可能感兴趣的:(POJ)