zoukankan      html  css  js  c++  java
  • POJ 3274 Gold Balanced Lineup(Hash)

    Gold Balanced Lineup

    Time Limit: 2000MS

     

    Memory Limit: 65536K

    Total Submissions: 7983

     

    Accepted: 2357

    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 onlyK 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

    Source

    USACO 2007 March Gold

     解题报告:这道题看了好长时间还是没有看懂,没有想到这个题为什么和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]==sum[j][k-1]-sum[j][0]

    C[i][l]=sum[i][l]-sum[i][0] (0<l<k)。

    所以只需求满足C[i]==C[j] 中最大的i-j。

    代码如下:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    const int MAX = 100007;
    int hash[MAX], sum[MAX][35], binary[MAX][35], c[MAX][35];
    int SearchHash(int *v, int n)//这是网上淘来的,自己写的hash提交时就错!
    {
    int i, p=0;
    for(i=0; i<n; i++)
    {
    p=((p<<2)+(v[i]>>4))^(v[i]<<10);
    }
    p = p % MAX;
    if (p <0 ) p = p + MAX;
    return p;
    }
    int main()
    {
    int N, K, i, j, a, p;
    while (scanf("%d%d", &N, &K) != EOF)
    {
    memset(hash, -1, sizeof(hash));
    memset(sum, 0, sizeof(sum));
    memset(binary, 0, sizeof(binary));
    memset(c, 0, sizeof(c));
    int ans = 0;
    hash[SearchHash(c[0], K)] = 0;
    for (i = 1; i <= N; ++i)
    {
    scanf("%d", &a);
    for (j = 0; j < K; ++j)
    {
    binary[i][j] = a & 1;//相当于binary[i][j] = a % 2;
    a = a >> 1;//相当于a = a / 2;
    sum[i][j] = sum[i - 1][j] + binary[i][j];//求各自的和
    c[i][j] = sum[i][j] - sum[i][0];
    }
    p = SearchHash(c[i], K);
    if (hash[p] != -1)
    {
    for (j = 1; j < K; ++j)
    {
    if (c[i][j] != c[hash[p]][j])
    {
    break;
    }
    }
    if (j == K)
    {
    if (i - hash[p] > ans)
    {
    ans = i - hash[p];
    }
    }
    }
    if (hash[p] == -1)
    {
    hash[p] = i;
    }
    }
    printf("%d\n", ans);
    }
    }



  • 相关阅读:
    第一篇:理论篇
    day 3:注释,缩进
    HTML的报告
    SAP DIALOG屏幕新增搜索帮助
    财务凭证科目替代(未写完)
    表维护生成器本地转请求包
    web安全之SQL注入
    Ubuntu16.04 下安装Sublime Text 3
    ubuntu16.04 下安装配置python3.6
    Ubuntu 16.04 下安装 PyCharm
  • 原文地址:https://www.cnblogs.com/lidaojian/p/2410423.html
Copyright © 2011-2022 走看看