POJ 3274 Gold Balanced Lineup

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 featurei.

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 cowsi..j is balanced if each of theK 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

/*
    题目大意:
            农夫的N头牛有很多相同之处。但他将所有牛的不同特性归纳为了K种。农夫用“特性标识符”描述每一头奶牛,输入N和K的长度,给定N个特征值。找出在N头牛中特性出现次数一样的区间最大长度。
   
    思路:
             由样例看,将给定的7个特征值转换为2进制存在结构体中,得到这样的一个序列:
数字   特征值    编号

 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
再按行累加得到
数字   特征值    编号

 7    1 1 1     1
 6    1 2 2     2
 7    2 3 3     3
 2    2 4 3     4
 1    3 4 3     5
 4    3 4 4     6
 2    3 5 4     7
每一行再减去第一列
数字   特征值    编号

 7    0 0 0     1
 6    0 1 1     2
 7    0 1 1     3
 2    0 2 1     4
 1    0 1 0     5
 4    0 1 1     6
 2    0 2 1     7
此时发现最远的相同序列为2和6 ,则最终结果为4.

        不过最后深深提醒,这个题坑的比较狠。当特征值和全部为0时,也就是从0到这个特征值的编号,而0是在输入中恰好没有的,所以遇到特征值全都为0的就要判断一下这个是不是最长区间,要不然根本不可能正确。还有,当仅有一头牛的时候,当特征值序列处理之后全部为一个值时,那就是1,否则就是0.

        代码思路:
                将给定的N个值转换为2进制存在结构体中,输入的同时作加和减去第一位处理。然后排序,判断特征值每一位的和是否相同,相同的话依次比较,更新Max值。
*/
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
#include <cstdio>
#include <map>
#include <set>
const int INF = 0x3f3f3f3f;

using namespace std;

struct node
{
    int ans[30];
    int w,sum;
} s[100100];
int n,m;
bool cmp(node x,node y)
{
    if(x.sum == y.sum)
        return x.w < y.w;
    return x.sum < y.sum;
}
int main()
{
    int ant;
    while(~scanf("%d %d",&n,&m))
    {
        for(int i=0,j=0; i<n; i++,j=0)
        {
            scanf("%d",&ant);
            memset(s[i].ans,0,sizeof(s[i].ans));
            while(ant)
            {
                s[i].ans[j++] = ant%2;
                ant/=2;
            }
            s[i].w = i+1;
            s[i].sum=0;
            if(i > 0)
            {
                for(int l=0; l<m; l++)
                    s[i].ans[l]+=s[i-1].ans[l];
                for(int l=1; l<m; l++)
                {
                    s[i-1].ans[l]=abs(s[i-1].ans[l]-s[i-1].ans[0]);
                    s[i-1].sum+=s[i-1].ans[l];
                }
                s[i-1].ans[0] = 0;
            }
            if(i == n-1)
            {
                for(int j=1; j<m; j++)
                {
                    s[i].ans[j]=abs(s[i].ans[j]-s[i].ans[0]);
                    s[i].sum+=s[i].ans[j];
                }
                s[i].ans[0] = 0;
            }
        }
        sort(s,s+n,cmp);
        int Max = 0;
        if(n==1)
        {
            int F = true;
            for(int i=0;i<m;i++)
            {
                if(s[0].ans[0] != s[0].ans[i])
                {
                    F=false;break;
                }
            }
            if(F)
                printf("1\n");
            else
                printf("0\n");
            continue;
        }
        for(int i=0; i<n; i++)
        {
            for(int j=i+1; j<n; j++)
            {
                if(s[i].sum == s[j].sum)
                {
                    bool F = true;
                    for(int k=0; k<m; k++)
                    {
                        if(s[i].ans[k] != s[j].ans[k])
                        {
                            F = false;
                            break;
                        }
                    }
                    if(F)
                        Max = max(Max,abs(s[j].w-s[i].w));
                }
                else if(s[i].sum == 0)
                     Max = max(Max,s[i].w);
                else
                    break;
            }
        }
        printf("%d\n",Max);
    }
    return 0;
}
/*
测试数据:
11 5
30
28
24
16
1
3
7
15
16
24
31
6 30
123456789
234567890
345678901
456789012
567890123
678901234
4 4
1
2
4
8
4 4
8
1
2
4
5 4
3
1
2
4
8
1 5
3
1 2
3
测试输出:
8
0
4
4
4
0
1
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值