这题,看到别人的解题报告做出来的,分析:

大概意思就是:

数组sum[i][j]表示从第1到第i头cow属性j的出现次数。

所以题目要求等价为:

求满足

sum[i][0]-sum[j][0]=sum[i][1]-sum[j][1]=.....=sum[i][k-1]-sum[j][k-1] (j<i)

中最大的i-j

将上式变换可得到

sum[i][1]-sum[i][0] = sum[j][1]-sum[j][0]

sum[i][2]-sum[i][0] = sum[j][2]-sum[j][0]

......

sum[i][k-1]-sum[i][0] = sum[j][k-1]-sum[j][0]

令C[i][y]=sum[i][y]-sum[i][0] (0<y<k)

初始条件C[0][0~k-1]=0

所以只需求满足C[i][]==C[j][] 中最大的i-j,其中0<=j<i<=n。

C[i][]==C[j][] 即二维数组C[][]第i行与第j行对应列的值相等,

那么原题就转化为求C数组中 相等且相隔最远的两行的距离i-j

大概意思就是:

数组sum[i][j]表示从第1到第i头cow属性j的出现次数。

所以题目要求等价为:

求满足

sum[i][0]-sum[j][0]=sum[i][1]-sum[j][1]=.....=sum[i][k-1]-sum[j][k-1] (j<i)

中最大的i-j

将上式变换可得到

sum[i][1]-sum[i][0] = sum[j][1]-sum[j][0]

sum[i][2]-sum[i][0] = sum[j][2]-sum[j][0]

......

sum[i][k-1]-sum[i][0] = sum[j][k-1]-sum[j][0]

令C[i][y]=sum[i][y]-sum[i][0] (0<y<k)

初始条件C[0][0~k-1]=0

所以只需求满足C[i][]==C[j][] 中最大的i-j,其中0<=j<i<=n。

C[i][]==C[j][] 即二维数组C[][]第i行与第j行对应列的值相等,

那么原题就转化为求C数组中 相等且相隔最远的两行的距离i-j

以样例为例

7 3
7
6
7
2
1
4
2
 

先把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,这就是所求。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int num[][];
int N,K;
struct hash{
int ind;
hash* next;
};
hash hashtable[];
int gethash(int id){
num[id][]=;
for(int i=;i<K;i++){
num[id][]+=num[id][i]*i;
}
num[id][]=(num[id][]&0x7fffffff)%;
return num[id][];
}
int isEqual(int id1,int id2){
int flag=;
for(int i=;i<K;i++){
if(num[id1][i]!=num[id2][i]){
flag=;
}
}
return flag;
}
int main(){
scanf("%d %d",&N,&K);
for(int j=;j<K;j++){
num[][j]=;
}
memset(hashtable,,sizeof(hash)*);
for(int i=;i<=N;i++){
int t;
scanf("%d",&t);
for(int j=;j<K;j++){
num[i][j]=t%;
t=t>>;
num[i][j]+=num[i-][j];
} } int result=;
for(int i=;i<=N;i++){
for(int j=;j<K;j++){
num[i][j]-=num[i][];
}
int h=gethash(i);
hash *t=(hash*)malloc(sizeof(hash));
t->next=hashtable[h].next;
t->ind=i;
hashtable[h].next=t;
while(t!=NULL){
if(isEqual(t->ind,i)){
int tmp=i-t->ind;
result=result>tmp?result:tmp;
}
t=t->next;
}
}
printf("%d\n",result);
return ;
}
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 13200   Accepted: 3866

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

最新文章

  1. Hadoop学习笔记—1.基本介绍与环境配置
  2. java中集合类中Collection接口中的Map接口的常用方法熟悉
  3. 函数----Beginning Visual C#
  4. TreeMap按照key排序
  5. 当一个activity中按钮过多时怎么办?
  6. 预定义宏_GNUC_ _MSC_VER
  7. emWin(ucGui)数值显示例程 -【worldsing笔记】
  8. bug集合
  9. writeToFile 读写文件问题
  10. Hashtable和HashMap类
  11. OpenGL中的深度、深度缓存、深度测试及保存成图片
  12. 复制JAVABEAN中的属性到另外一个JAVABEAN中
  13. android 初学: 提示No Launcher activity found!
  14. [POJ2104/HDU2665]Kth Number-主席树-可持久化线段树
  15. nodejs01--什么是nodejs,nodejs的基本使用
  16. Memcached的简介和使用
  17. CSS 左右两边底部对齐
  18. sqlserver数据库导出表结构和表数据生成创建表和insert语句
  19. Codeforces Gym 101623A - 动态规划
  20. php get_magic_quotes_gpc()函数使用

热门文章

  1. Python的编码注释# -*- coding:utf-8 -*-
  2. Metesploit使用随笔
  3. IE刷新后,文本框的值不变
  4. EL表达式介绍(1)
  5. s3c6410 Linux 驱动开发环境搭建
  6. HTML5 Canvas 笛卡尔坐标系转换尝试
  7. 小米miui系统怎么关闭文件管理里的热门视频和表情?
  8. 【Excle】文本日期转化为日期格式
  9. 【DB2】报错:-30090 25000 指定的操作对远程执行失败
  10. Yii2.0 下的 load() 方法的使用