Gold Balanced Lineup - POJ 3274 哈希

本文介绍了一种算法挑战,目标是在 Farmer John 的一群牛中找出最长的连续子集,使得每一种特征在这段连续子集内出现的次数相同。文章详细解释了如何通过计算累积和并利用哈希表来高效解决这一问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Gold Balanced Lineup
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 11867 Accepted: 3487

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

题意:让牛的能力加起来,问你让这些能力的数目相同的最长连续序列有多长。

思路:

数字  特征值    第几头牛

   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.

用哈希和vector去做,我的时间一般,782MS,注意有全为0的情况。另附测试数据。

#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
struct node
{ int val[35];
}cow[100010];
vector<int> hash[23333];
int sum[35];
int a[35],k;
bool compare(int pos)
{ bool flag=true;
  for(int i=1;i<=k;i++)
   if(a[i]!=cow[pos].val[i])
    flag=false;
  return flag;
}
int solve(int pos)
{ int s=0,i;
  bool flag=false,flag2=true;
  for(i=1;i<=k;i++)
  { s*=49;
    s+=a[i];
    s%=23330;
    if(s<0)
     s+=23330;
  }
  for(i=0;i<hash[s].size();i++)
  { flag=compare(hash[s][i]);
    if(flag)
     return hash[s][i];}

  for(i=1;i<=k;i++)
   if(a[i]!=0)
   flag2=false;
  if(flag2)
   return 0;
  hash[s].push_back(pos);
  return pos;
}
int main()
{ int n,i,j,m,pos,ans=0;
  scanf("%d%d",&n,&k);
  for(i=1;i<=n;i++)
  { scanf("%d",&m);
    j=1;
    while(m)
    { if(m%2==1)
       sum[j]+=1;
      j++;
      m/=2;
    }
    for(j=1;j<=k;j++)
     cow[i].val[j]=a[j]=sum[j]-sum[1];
    j=solve(i);
    if(ans<i-j)
     ans=i-j;
  }
  printf("%d\n",ans);
}

1.边界:
注意以下情况:
4 4
1
2
4
8
====和====
4 4
8
1
2
4
如果输出不为4的话小心。
还有:
5 4
3
1
2
4
8
注意一下。这个输出应该也为4.

2.小心输出为零:
1 5
3
这组输出为0,而
1 2
3
输出1.
外加一组:
8 4
1
2
4
8
1
2
4
8
输出应该为8,全为0的情况不能加入vector中



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值