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
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
Sample Input
7 3 7 6 7 2 1 4 2
Sample Output
4
Hint
这题一开始用的是暴力O(N^2)的方法,N才10W竟然超时了,看来系数太大。
我使用一个features数组,features[i][j] 记录了从第一头牛到第i头牛feature j出现的总次数。
这样cow编号a和b之间feature i总数为 features[b][i] - features[a-1][i]。
我们要找到距离最远的a和b,使得features[b][i] - features[a-1][i] = features[b][j] - features[a-1][j] (1<=i<=j<=k)。
我只想到这一层,然后就双重循环遍历。
其实我们转换一下上面的等式可以得到 features[b][i] - features[b][j] = features[a-1][i] - features[a-1][j] (1<=i<=j<=k)。
features[b][i] - features[b][j] 描述的是单个元素,这个等式意味着 我们可以利用 features[b][i] - features[b][j]条件 作为key对 cows进行hash。
如果a和b满足条件,那么他们必然会hash到一个桶中。这样我们线性扫描的时候hash当前元素得到对应的桶,我们只需要对桶中元素进行验证即可,不需要对之前每一个进行验证。 这样10W个元素,如果桶大小为1W,理想情况下每个桶有10个元素,对每个元素最多则只需要进行10次比较,复杂度为O(10*N)。
对每个元素i,我们用features[i][j] - features[i][1]作为第j个feature的增量。一共会得到k-1个增量。
对多个数字进行hash,这里使用常见的求和取余作为key。
虽是初级hash题目,但是这个转换的思想很精妙。从中学到了新的思路,并体现出hash的灵活和强大。
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <list>
#include <set>
#include <time.h>
#include <string.h>
#include <string>
#include <iostream>
#include <map>
using namespace std;
#define PRIME 27659;
map<int,vector<int> > _map;
int features[100100][31];
int n,k;
bool valid(int begin, int end){
for(int i=2;i<=k;++i)
if( features[end][i]-features[begin][i] != features[end][i-1]-features[begin][i-1])
return false;
return true;
}
int main(){
scanf("%d %d",&n,&k);
int t;
int res = 0;
_map[0].push_back(0);
for(int i=1;i<=n;++i){
scanf("%d",&t);
int sum=0;
for(int j=1;j<=k;++j){
features[i][j] = features[i-1][j] + (t&1);
sum += features[i][j]-features[i][1];
t >>= 1;
}
int key = sum%PRIME;
vector<int> tmp = _map[key];
for(int j=0;j<tmp.size();++j){
if(valid(tmp[j],i))
res = max(res,i-tmp[j]);
}
_map[key].push_back(i);
}
return 0;
}