现在的位置: 首页 > 综合 > 正文

poj 3274 Gold Balanced Lineup

2012年08月19日 ⁄ 综合 ⁄ 共 2806字 ⁄ 字号 评论关闭

介于我太水了啊,题意什么的叙述不清啊。。所以就从二牛那里搞来了一些解释,感觉叙述的不错啊、、

下面是题目大意,对提供者表示感谢。


代码操作如下:
1、先将十进制数转换成二进制数记录保存。

2、然后逐行累加,得出到某只牛时某种特征出现了几次。

3、每行减去第一个数,得出一个序列,若两头牛之间是平衡区间的话,各个特征的增长数是相等的,及减掉第一个数得出来的序列是相等的。

4、寻找距离最远的两个相等的序列,得出答案。

我们可以由样例举例:

转换成二进制特征值为:

数字  特征值    第几头牛

   7          1 1 1                1

   6          0 1 1                2

   7          1 1 1                3

   2          0 1 0                4

   1          1 0 0                5

   4          0 0 1                6

   2          0 1 0                7

按行累加得:

1 1 1

1 2 2

2 3 3

2 4 3

3 4 3

3 4 4

3 5 4

都减去第一列得:

0 0 0

0 1 1

0 1 1

0 2 1

0 1 0

0 1 1

0 2 1

所以说  最大区间是  6-2 = 4.

有一种特殊情况就是当到了某一行出现全都是零的情况,例如:

0 1 2 1 1 0 2

0 0 0 2 1 1 1

0 0 0 0 0 0 0 

我再说两句啊、什么哈希的啊,不会啊。。只能用其他的方法代替了啊、、时间不过有点慢啊、、

Gold Balanced Lineup
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 10759   Accepted: 3194

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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node
{
    int d[30], in;
} cow[101000];
int k;
int cmp1(int x)
{
    for(int i = 0; i < k; i++)
    {
        if(cow[x].d[i] != cow[x-1].d[i])
            return 0;
    }
    return 1;
}
int cmp2(const void *a , const void *b)
{
    struct node *c = (struct node*)a;
    struct node *d1 = (struct node*)b;
    for(int i = 0; i < k; i++)
    {
        if(c->d[i] != d1->d[i])
            return c->d[i]-d1->d[i];
    }
    return c->in - d1->in;
}
int main()
{
    int n, i, j, max, t;
    while(~scanf("%d %d",&n, &k))
    {
        for(i = 0; i < k; i++)
            cow[0].d[i] = 0;
        for(i = 1; i <= n; i++)
        {
            scanf("%d",&t);
            for(j = 0; j < k; j++)
            {
                cow[i].d[j] = t%2;
                t /= 2;
            }
            cow[i].in = i;
            for(j = 0; j < k; j++)
                cow[i].d[j] += cow[i-1].d[j];
            for(j = 1; j < k; j++)
                cow[i].d[j] -= cow[i].d[0];
            cow[i].d[0] = 0;
        }
        qsort(cow , n+1 , sizeof(cow[0]) , cmp2);
        int x = 0, y = 0;
        max = 0;
        for(i = 1; i <= n; i++)
        {
            if(cmp1(i))
                y = i;
            else
            {
                if(max < cow[y].in-cow[x].in)
                    max = cow[y].in-cow[x].in;
                x = i;
                y = i;
            }
        }
        if(max < cow[y].in-cow[x].in)
            max = cow[y].in-cow[x].in;
        printf("%d\n",max);
    }
    return 0;
}

抱歉!评论已关闭.