Gold balanced lineup
Time limit:2000 ms |
|
Memory limit:65536 K |
Total submissions:12334 |
|
Accepted:3618 |
Description
Farmer John'sNCows (1 ≤N≤ 100,000) Share Your similarities. In fact, FJ has been able to narrow down the list of features shared by his cows to a list of onlyKDifferent 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 featureI.
Always the sensitive fellow, FJ lined up cows 1 ..NIn a long row and noticed that certain ranges of cows are somewhat "balanced" in terms of the features the exhibit. A contiguous range of cowsI..JIs balanced if each ofKPossible features is exhibited by the same number of cows in the range. fj is curous as to the size of the largest balanced range of cows. See if you can determine it.
Input
Line 1: two space-separated integers,
NAnd
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
Returns some features in binary format, and finds that all features within the longest segment are the same.
First, we use P [I] [J] to store the total number of features of the first ox. If [I, j] in a certain segment matches
P [J] [0]-P [I-1] [0] = P [J] [1] = P [I-1] [1] = .. = P [J] [k-1]-P [I-1] [k-1]; all the difference from 0th to k-1 is the same, it must be P [J] [0... k]-P [I-1] [0... k] corresponds to the interval [I, j];
Then, the transformed P [J] [1]-P [J] [0] = P [I-1] [1]-P [I-1] [0], P [J] [2]-P [J] [0] = P [I-1] [2]-P [I-1] [0]... Always K-1
Therefore, P [I] [J] is converted to P [I] [J] = P [I] [J]-P [I] [0]. if the interval [I, j] is the same, then P [I-1] and P [J] should be exactly the same and use hash to find the longest segment
Note that the interval [1, N] may be the longest, so add 0 to VEC [0 ].
#include <cstdio>#include <cstring>#include <algorithm>#include <vector>using namespace std ;#define mod 999997int p[110000][32] ;vector <int> vec[100000] ;int main(){ int i , j , n , k , a , c[32] , num , flag , l , h , max1 = 0 ; __int64 sum ; scanf("%d %d", &n, &k); for(i = 0 , num = 1; i < k ; i++) { p[0][i] = 0 ; num *= 2 ; } sum = 0; vec[0].push_back(0) ; for(i = 1 ; i <= n ; i++) { scanf("%d", &a); if(a == num-1) max1 = 1 ; j = k-1 ; memset(c,0,sizeof(c)); while(a) { c[j--] = a%2 ; a /= 2 ; } for(j = 0 ; j < k ; j++) { p[i][j] = p[i-1][j] + c[j] ; } } for(i = 1 ; i <= n ; i++) { sum = 0 ; for(j = 1 ; j < k ; j++) { p[i][j] = p[i][j] - p[i][0] ; sum += p[i][j] ; } p[i][0] = 0 ; if(sum < 0) sum += 100000 ; sum %= mod ; num = vec[sum].size(); flag = 0 ; for(j = 0 ; j < num ; j++) { l = vec[sum][j] ; for(h = 0 ; h < k ; h++) if( p[i][h] != p[l][h] ) break; if(h == k) { flag = 1 ; if( max1 < i-l ) max1 = i-l ; } } if(!flag) vec[sum].push_back(i) ; } printf("%d\n", max1);}
Poj3274 -- gold balanced lineup (hash)