Gold Balanced Lineup
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 16870   Accepted: 4774

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

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

问题链接:POJ3274 Gold Balanced Lineup

问题描述:(略)

问题分析:占个位置,不解释。

程序说明:(略)

参考链接:(略)

题记:(略)

AC的C++语言程序如下:

/* POJ3274 Gold Balanced Lineup */#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>using namespace std;const int MOD = 99991;
const int N = 100000;
const int K = 30;
int sum[N + 1][K];      // 各头牛的属性和
int c[N + 1][K];            // 各头牛与第1头的属性差
int hashv[N * 10];
int n, k;int hash_key(int cc[])
{int key = 0;for(int i = 1; i < k; i++)key =(key % MOD + cc[i]) << 2;key = abs(key) % MOD;return key;
}int main()
{int ans = 0;memset(sum, 0, sizeof(sum));memset(hashv, -1, sizeof(hashv));hashv[0] = 0;scanf("%d%d",&n,&k);for(int i = 1; i <=n; i++) {int a;scanf("%d", &a);for(int j = 0; j < k; j++) {sum[i][j] = sum[i - 1][j] + (a & 1);c[i][j] = sum[i][j] - sum[i][0];a >>= 1;}int key = hash_key(c[i]);while(hashv[key] != -1) {int j;for(j = 0; j < k; j++)if(c[i][j] != c[hashv[key]][j])break;if(j == k && ans < (i - hashv[key])) {ans = i - hashv[key];break;}key++;}if(hashv[key] == -1)hashv[key] = i;}printf("%d\n",ans);return 0;
}

POJ3274 Gold Balanced Lineup【Hash函数】相关推荐

  1. bzoj 1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列

    1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 821   ...

  2. 1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列

    1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 510   ...

  3. Gold Balanced Lineup - poj 3274 (hash)

    这题,看到别人的解题报告做出来的,分析: 大概意思就是: 数组sum[i][j]表示从第1到第i头cow属性j的出现次数. 所以题目要求等价为: 求满足 sum[i][0]-sum[j][0]=sum ...

  4. 【POJ 3274】Gold Balanced Lineup (stl map )设计hash表,处理碰撞

    题目链接 题目链接 http://poj.org/problem?id=3274 题意 输入每头牛的特征的10进制,若i~j头牛中每个数位的特征相等则满足要求,求所有满足要求的j-i的最大值. 解题思 ...

  5. BZOJ1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列

    n<=100000个数表示每头牛在K<=30种物品的选取情况,该数在二进制下某位为0表示不选1表示选,求一个最大的区间使区间内选择每种物品的牛一样多. 数学转化,把不同状态间单变量的关系通 ...

  6. POJ 3274 Gold Balanced Lineup(哈希)

    题目链接 很难想.会哈希,但是想不出.需要一个转化,本来是求某一段上的二进制上每一位的1数目相等,转化为找两段相等的,换元可推出公式.最后注意特判.. 1 #include <iostream& ...

  7. POJ3274Gold Balanced Lineup(哈希)

    Gold Balanced Lineup Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10360   Accepted: ...

  8. Hash函数的安全性

    我们为了保证消息的完整性,引进了散列函数,那么散列函数会对安全正造成什么影响呢?这是需要好好研究一番的问题. 三个概念: 1.如果y<>x,且h(x)=h(y),则称为碰撞. 2.对于给定 ...

  9. Hash 函数资源链接汇总

    Hash 链接: [1]General Purpose Hash Function Algorithms:http://www.partow.net/programming/hashfunctions ...

最新文章

  1. 中小企业低成本快速建站的秘诀——模板建站
  2. 【风控建模】风控分类模型种类(决策、排序)比较与模型评估体系(ROC/gini/KS/lift)
  3. [改善Java代码]使用valueOf前必须进行校验
  4. 【SPFA】Party(jzoj 1328)
  5. 怎么用python自制计算公式_如何使用Python和Numpy计算r平方?
  6. 堆叠自编码器中的微调解释_25种深刻漫画中的编码解释
  7. 多媒体会议系统中的延迟
  8. java俄罗斯方块算法_【俄罗斯方块java】分享一个Java写的俄罗斯方块源码 算法简单(300行) 注释详细!...
  9. PMP课程笔记:第8章 项目质量管理
  10. 2020-09-21
  11. 手把手教你使用stata做竞争风险模型
  12. 感觉中国程序员前景一片灰暗,是这样吗?
  13. 如何将一个应用添加开机启动项
  14. Archiving 时遇到的错误
  15. Chrome 开发者工具官方中文文档
  16. C#程序崩溃捕获与自动重启方法(简洁有效)
  17. 通达OA web页面与精灵显示内容更新后不一致的问题
  18. 深圳新生儿出生入户办理流程 - 父深圳公司集体户 母内地户口
  19. js中的函数防抖和函数节流
  20. 中国移动计算机类行测题目,2019中国移动春招考试行测题库:行测常识判断模拟题(十三)...

热门文章

  1. C# WPD PortableDeviceApiLib获取便携设备列表
  2. yaml语法三大规则
  3. mysql 5.5 主从_Mysql5.5常用命令及主从配置
  4. tomcat的服务器目录在哪个文件夹,Tomcat目录结构详细介绍
  5. windows10安装更新很慢ndows,Windows 10最近更新可能会使电脑速度变慢 但有解决办法...
  6. Clion配置Ros环境
  7. Sqoop导入hive中null是空字符串还是‘null‘的问题(关注)
  8. LeetCode69. x 的平方根(二分查找)
  9. 动态添加和删除 ListView 项
  10. 至少清楚知道兼容IE8 ie9 ;持续更新