POJ 3274 Gold Balanced Lineup

来源:互联网 发布:淘宝上好的外贸店 编辑:程序博客网 时间:2024/05/16 12:38

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 andK.
Lines 2..N+1: Line i+1 contains a single K-bit integer specifying the features present in cowi. 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 37672142

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 530282416137151624316 301234567892345678903456789014567890125678901236789012344 412484 481245 4312481 531 23测试输出:8044401*/


0 0
原创粉丝点击