POJ 3274 Gold Balanced Lineup

解题思路:hash

统计到当前位置为止,各位上出现1的个数,相对于最小次数的差值

比较出现相同差值序列的最长距离即可

欢迎指教
   
     
#include < iostream >
using namespace std;
#define MAXN 100001
#define PRIME 50101
int feature[MAXN][ 30 ],next[MAXN],first[PRIME];
int main()
{
int i,j,N,K,F,p,_min, _max = 0 ;
long long t,t2;
int bit[ 30 ];
for (i = 0 ,j = 1 ;i < 30 ;i ++ ,j *= 2 )bit[i] = j;
for (i = 0 ;i < MAXN;i ++ )next[i] =- 1 ;
for (i = 0 ;i < PRIME;i ++ )first[i] =- 1 ;
scanf(
" %d %d " , & N, & K);
for (i = 0 ;i < N;i ++ )
{
scanf(
" %d " , & F);
for (_min = MAXN,j = 0 ;j < K;j ++ )
{
if (i > 0 )feature[i][j] = feature[i - 1 ][j];
if (F & bit[j])feature[i][j] ++ ;
if (_min > feature[i][j])_min = feature[i][j];
}
for (t = j = 0 ;j < K;j ++ ){feature[i][j] -= _min;t2 = feature[i][j];t += (t2 * t2);}
if (t == 0 && _max < (i + 1 ))_max = i + 1 ;
t
%= PRIME;
if ( - 1 == (p = first[t])){first[t] = i; continue ;}
while ( 1 )
{
for (j = 0 ;j < K;j ++ ) if (feature[p][j] != feature[i][j]) break ;
if (j == K && ((i - p) > _max))_max = i - p;
if (next[p] ==- 1 ){next[p] = i; break ;}
p
= next[p];
}
}
printf(
" %d\n " ,_max);
return 0 ;
}

 

你可能感兴趣的:(poj)