poj3274--Gold Balanced Lineup(hash)

Gold Balanced Lineup

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 12334   Accepted: 3618

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

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

题目大意,按二进制给出一些特征,求最长的一段内的所有特征相同

首先用p[i][j]存储从第一个牛到底i头牛,关于第j种特征的总数,如果某一段中的[i,j]符合,那么

p[j][0] - p[i-1][0] = p[j][1] = p[i-1][1] = 。。 =  p[j][k-1] - p[i-1][k-1];从第0到k-1所有的差都相同,一定是p[j][0...k] - p[i-1][0...k] 才对应的是区间[i,j];

那么,转化后的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] 。。。。一直k-1

所以最后是p[i][j]转化为p[i][j] = p[i][j] - p[i][0] ;那么如果区间[i,j]相同,那么p[i-1]和p[j]应该完全相同,使用hash找到最长的一段

注意,存在可能区间[1,n]是最长的区间,所以在vec[0] 中加入0

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std ;
#define mod 999997
int 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);
}
时间: 2024-10-25 22:28:35

poj3274--Gold Balanced Lineup(hash)的相关文章

POJ 3474 Gold Balanced Lineup Hash

题意一开始不是很明确, 后来发现是每一种特征出现的次数相同 这样一来就变成简单hash问题了,如果把每个特征看看做是一个(n+1)进制数的话,对奶牛序列求一下前缀和,如果i - j这一段每一种特征出现的次数相同的话,把i - 1点和j点的每一位减去所有位中的最小值之后,必然相等,所以hash判断一下就好. #include <cstdio> #include <cstring> #include <iostream> #include <map> #incl

poj3274(Gold Balanced Lineup)

题目地址:Gold Balanced Lineup 题目大意: 一个农场有N个奶牛,每个奶牛都有不同的特征,聪明的农夫给奶牛 feature ID.代表奶牛所具有的特征.将feature ID 写成为K位的二进制的数,其中有1的位置代表奶牛具有此特征,0代表没有此特征.从i->j 使这个区间的奶牛所有特征的个数是相等的.其中最大的区间差就是题图所求的. 解题思路: 解题思路: 经典题,不转化问题很难做,先根据官方的方法转化问题,把“求最远的两行间各个特征出现次数相等”转化为“求最远的相同两行”,

poj 3274 Gold Balanced Lineup, 拉链式hash

sum[i][j] 表示从第1到第i头cow属性j的出现次数 所以题目要求等价为: 求满足 sum[i][0]-sum[j][0]=sum[i][1]-sum[j][1]=.....=sum[i][k-1]-sum[j][k-1] (j<i) 中最大的i-j 将上式变换可得到 sum[i][1]-sum[i][0] = sum[j][1]-sum[j][0] sum[i][2]-sum[i][0] = sum[j][2]-sum[j][0] ...... sum[i][k-1]-sum[i][0

poj 3274 -- Gold Balanced Lineup

Gold Balanced Lineup Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12110   Accepted: 3553 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

bzoj1702[Usaco2007 Mar]Gold Balanced Lineup 平衡的队列*

bzoj1702[Usaco2007 Mar]Gold Balanced Lineup 平衡的队列 题意: N头牛,一共K种特色.每头牛有多种特色.[i,j]段被称为balanced当且仅当K种特色在[i,j]内拥有次数相同.求最大的[i,j]段长度.n≤100000,k≤30. 题解: 得到式子:a[i][l]-a[j][l]=a[i][l-1]-a[j][l-1],l在2..k之间,移项得a[i][l]-a[i][l-1]=a[j][l]-a[j][l-1],l在2..k之间,故可以定义一个

1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列

1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 510  Solved: 196[Submit][Status][Discuss] Description Farmer John's N cows (1 <= N <= 100,000) share many similarities. In fact, FJ has been able to narrow

Gold Balanced Lineup - poj 3274 (hash)

这题,看到别人的解题报告做出来的,分析: 大概意思就是: 数组sum[i][j]表示从第1到第i头cow属性j的出现次数. 所以题目要求等价为: 求满足 sum[i][0]-sum[j][0]=sum[i][1]-sum[j][1]=.....=sum[i][k-1]-sum[j][k-1] (j<i) 中最大的i-j 将上式变换可得到 sum[i][1]-sum[i][0] = sum[j][1]-sum[j][0] sum[i][2]-sum[i][0] = sum[j][2]-sum[j]

bzoj 1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列【hash】

我%&&--&()&%????? 双模hashWA,unsigned long longAC,而且必须判断hash出来的数不能为0???? 我可能学了假的hash 这个题求个前缀和,然后目标是找到距离当前位置最远,且能使这两个数组差分后2-k位相同 hash把差分后数组的2到k位压起来即可,用map存这个hash值最早出现的位置 但是我还是不明白为啥hash值不能为0啊?? #include<iostream> #include<cstdio> #i

【POJ 3274】Gold Balanced Lineup (stl map )设计hash表,处理碰撞

题目链接 题目链接 http://poj.org/problem?id=3274 题意 输入每头牛的特征的10进制,若i~j头牛中每个数位的特征相等则满足要求,求所有满足要求的j-i的最大值. 解题思路 抽屉原理,用前缀和处理每个数位即可. 直接暴力的话复杂度太大了,所以需要取巧的办法. 直接暴力求解是sum[i][p] - sum[j][p] == sum[i][0] - sum[j][0].其中i表示第i头牛,j表示第j头牛,p表示第p个特征,i > j. 取巧的办法:sum[i][p] -