Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 10711   Accepted: 3182

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

题意:有n个二进制长度为k的特征数(十进制),求最大的连续的牛的数目,使这些牛每个特征的总数相等;

思路:

先把7个十进制特征数转换为二进制,并逆序存放到特征数组feature[ ][ ],得到:

7 à 1 1 1

6 à 0 1 1

7 à 1 1 1

2 à 0 1 0

1 à 1 0 0

4 à 0 0 1

2 à 0 1 0

(行数为cow编号,自上而下从1开始;列数为特征编号,自左到右从0开始)

再求sum数组,逐行累加得,sum数组为

1 1 1

1 2 2

2 3 3

2 4 3

3 4 3

3 4 4

3 5 4

再利用C[i][y]=sum[i][y]-sum[i][0]求C数组,即所有列都减去第一列

注意C数组有第0行,为全0

0 0 0 à 第0行

0 0 0

0 1 1

0 1 1

0 2 1

0 1 0

0 1 1

0 2 1

显然第2行与第6行相等,均为011,且距离最远,距离为6-2=4,这就是所求。

但是最大数据有10W个,即10W行,因此不能直接枚举找最大距离,必须用Hash查找相同行,找到相同行再比较最大距离。

注意C数组的值可能为负数,因此生成key值时要注意保证key为非负数。

 #include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; const int prime = ;
struct node
{
int id;
struct node *next;
}*hash[prime];//邻接表 int sum[][];//从第一头牛到第i头牛,特征j总共出现了sum[i][j]次;
int c[][];//c[i][j] = sum[i][j] - sum[i][0];
int n,k,maxlen; bool check(int x, int y)
{
int i;
for(i = ; i < k; i++)
if(c[x][i] != c[y][i])
return false;
return true;
} void Hash(int ci)//ci代表序列的下标
{
int key = ,i;
//生成关键字
for(i = ; i < k; i++)
key += c[ci][i]*i;//key有可能是负值,要取绝对值,对大素数取余;
key = abs(key)%prime;
if(!hash[key])//出现新的key;
{
hash[key] = new struct node;
hash[key]->id = ci;
hash[key]->next = NULL;
}
//当key发生冲突时
else
{
struct node *p = hash[key];
if(check(p->id,ci))
{
int dis = ci-(p->id);
if(dis > maxlen)
maxlen = dis;//因p->id是从小到大的,所以当遍历有p->id满足时肯定是当前最优的,
return; //可以return; }
else
{
while(p->next)
{
if(check(p->next->id,ci))
{
int dis = ci-(p->next->id);
if(dis > maxlen)
maxlen = dis;
return; }
p = p->next;
}
//当ci序列与其它序列都不相同时就插到该链的最后;
struct node *tmp = new struct node;
tmp->id = ci;
tmp->next = NULL;
p->next = tmp;
}
}
return;
}
int main()
{
int i,j;
while(~scanf("%d %d",&n,&k))
{
for(i = ; i < k; i++)
{
sum[][i] = ;
c[][i] = ;//第0头牛初始化
}
memset(hash,,sizeof(hash));
Hash();//把第0头牛放入哈希表
maxlen = ;
for( i = ; i <= n; i++)
{
int x;
scanf("%d",&x);
for(j = ; j < k; j++)
{
sum[i][j] = sum[i-][j] + x%;
c[i][j] = sum[i][j]-sum[i][];
x = x/;
}
Hash(i);
}
printf("%d\n",maxlen);
}
return ;
}
 

Gold Balanced Lineup(哈希表)的更多相关文章

  1. POJ 3274 Gold Balanced Lineup 哈希,查重 难度:3

    Farmer John's N cows (1 ≤ N ≤ 100,000) share many similarities. In fact, FJ has been able to narrow ...

  2. poj 3274 Gold Balanced Lineup(哈希 )

    题目:http://poj.org/problem?id=3274 #include <iostream> #include<cstdio> #include<cstri ...

  3. 哈希-Gold Balanced Lineup 分类: POJ 哈希 2015-08-07 09:04 2人阅读 评论(0) 收藏

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

  4. POJ 3274:Gold Balanced Lineup 做了两个小时的哈希

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

  5. POJ 3274 Gold Balanced Lineup

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

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

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

  7. 洛谷 P1360 [USACO07MAR]Gold Balanced Lineup G (前缀和+思维)

    P1360 [USACO07MAR]Gold Balanced Lineup G (前缀和+思维) 前言 题目链接 本题作为一道Stl练习题来说,还是非常不错的,解决的思维比较巧妙 算是一道不错的题 ...

  8. 【洛谷】P2880 [USACO07JAN]平衡的阵容Balanced Lineup(st表)

    题目背景 题目描述: 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连 ...

  9. POJ 3264 Balanced Lineup 【ST表 静态RMQ】

    传送门:http://poj.org/problem?id=3264 Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total S ...

随机推荐

  1. FastDFS安装配置手册

    文件服务器分布式系统安装手册 本文档详细的介绍了FastDFS的最小集群安装过程.集群环境如下: tracker:20.2.64.133 .用于调度工作,在访问上起负载均衡的作用. group1: s ...

  2. enter 默认搜索

    onkeydown=" if(event.keyCode==13) Search(); "

  3. 转--Oracle DB 服务器系统时间修改问题与 SCN 关系的深入研究

    论坛里一个朋友说将DB 服务器系统时间往往后修改了3个月(从11年改成10年),启动DB报600的错误. 一. 先做个测试 1.1 关闭DB SQL> shutdown immediate Da ...

  4. C#使用框架,打开新选项卡

    C#使用框架,打开新选项卡: --打开函数 function Open(text, url) {        if ($("#tabs").tabs('exists', text ...

  5. Mac下载并编译Google安卓AOSP项目代码

    Mac下载并编译Google安卓AOSP项目代码 参考 https://source.android.com/source/index.html 这两天用Mac下载安卓AOSP源码,且把遇到的问题记下 ...

  6. Android开发--二维码开发应用(转载!)

    android项目开发 二维码扫描   基于android平台的二维码扫描项目,可以查看结果并且链接网址 工具/原料 zxing eclipse 方法/步骤   首先需要用到google提供的zxin ...

  7. php验证是否为手机端还是PC

    <?php $forasp = strtolower($_SERVER['HTTP_USER_AGENT']); if(strpos($forasp,'mobile')==true) { ech ...

  8. SGU 199 Beautiful People(DP+二分)

    时间限制:0.25s 空间限制:4M 题意: 有n个人,每个人有两个能力值,只有一个人的两个能力都小于另一个的能力值,这两个人才能共存,求能同时共存的最大人数. Solution: 显然这是一个两个关 ...

  9. 『重构--改善既有代码的设计』读书笔记----Self Encapsulate Field

    如果你直接访问一个字段,你就会和这个字段直接的耦合关系变得笨拙.也就是说当这个字段权限更改,或者名称更改之后你的客户端代码都需要做相应的改变,此时你可以为这个字段建立设值和取值函数并且只以这些函数来访 ...

  10. Linux下定时备份数据库

    linux下使用crontab定时备份MYSQL数据库的方法只需按照下面3步做,一切都在你的掌控之下: 第一步:在服务器上配置备份目录代码: mkdir /var/lib/mysqlbackup cd ...