题目传送门

题目大意:有n只牛,每只牛有k个属性,接下来n个数字,每个数字的二进制位上的1和0分别表示某种属性的有或者无,然后一个特殊数列就是,一个区间内所有牛的各种属性的总和相等(有e种1属性  e种2属性and so on),问你这排牛的最长的特殊数列长度是多少。

思路:看上去像dp,但思路走不通,然后看网上大佬的思路,仿佛推开新世界的大门。

数组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][l]=sum[i][l]-sum[i][0] (0<l<k)。 所以只需求满足C[i]==C[j] 中最大的i-j。

举样例来说明一下:

   x           属性           牛

   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

按行累加得sum[i]:

1 1 1

1 2 2

2 3 3

2 4 3

3 4 3

3 4 4

3 5 4

都减去第一列得c[i]:

0 0 0

0 1 1

0 1 1

0 2 1

0 1 0

0 1 1

0 2 1

所以说  最大区间是  6-2 = 4

这道题最主要是还是让我理解了哈希的用处,虽然怎么用还再摸索中,但也慢慢的推开了哈希的大门了吧,还有这个题意的转化也非常的重要。

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
const double PI=acos(-1.0);
int fact[10]= {1,1,2,6,24,120,720,5040,40320,362880};
const int maxn = 100005;
const int MAX = 100005;
const int mod = 1000000;
int hash[MAX*10];//hash表储存下标
int next[MAX*10];//next作为邻接表
int sum[MAX][35];//第 1 头牛到第 i 头的对应属性的和
int c[MAX][35];//存放每头牛属性 j与第一个属性的差
int n,k;
int gethash(int *cc){
int key=0;
for(int i=1;i<=k;i++){
key=(key)%mod+cc[i]%mod*cc[i]%mod;//网上看的哈希函数
key%=mod;
}
return abs(key);//key可能是负数
}
bool compare(int id,int x){
for(int i=1;i<=k;i++){
if(c[id][i]!=c[x][i])return false;
}
return true;
}
int main(){
cin>>n>>k;
memset(hash,-1,sizeof(hash));
hash[0]=0;//这两行很重要 因为有可能整个序列都是平衡的
next[0]=-1;//而序列表达是i-j 所以有一部分的牛的哈希可能是0
int maxlen=0;
for(int i=1;i<=n;i++){
int x;
scanf("%d",&x);
for(int j=1;j<=k;j++){
sum[i][j]+=sum[i-1][j]+x%2;
c[i][j]=sum[i][j]-sum[i][1];
x/=2;
}
int key=gethash(c[i]);
for(int j=hash[key];j!=-1;j=next[j]){
if(compare(i,j)){
maxlen=max(i-j,maxlen);
}
}
next[i]=hash[key];//邻接表
hash[key]=i;
}
cout<<maxlen<<endl;
}
old Balanced Lineup
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 16727   Accepted: 4736

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

poj3274 找平衡数列(哈希加一点数学思维)的更多相关文章

  1. 跟Microsoft.AspNet.Identity学习哈希加盐法

    什么是哈希加盐法? 废话少说:对于MD5这种加密算法,同样的密码每次加密后得到的密文是一样的,所以黑客可以利用已知的密码库(彩虹库)对目标数据库密文进行对比进行攻击. 怎样解决:哈希加盐法,以下是网上 ...

  2. 解决Eclipse中“诡异”的错误:找不到或无法加载主类

    记录下来遇到的(问题,解决方法),是更有效的解决问题的方式.(原谅我领悟的太晚与懒,从此用更有意义的方法,做一个更有意义的人) 因为遇到了多次,参考同一个方法,原文连接:https://blog.cs ...

  3. java环境变量---找不到或无法加载主类

    默认安装在C:\ProgramFiles\Java\jdk1.7.0目录下 环境变量配置为 PATH=.;%JAVA_HOME%\bin CLASSPATH=.;%JAVA_HOME%\lib\dt. ...

  4. IDEA 错误:找不到或无法加载主类

    下的java核心编程的源码,只有java文件,没有idea或者eclipse的项目结构信息. 分别用eclipse和idea打开了一遍,方便学习调试. 项目文件夹:E:\学习资料\Java\语法\ja ...

  5. Eclipse无法编译,提示错误“找不到或者无法加载主类”解决方法

    jar包问题: 1.项目的Java Build Path中的Libraries中有个jar包的Source attachment指为了一个不可用的jar包, 解决办法是:将这个不可用的jar包remo ...

  6. Eclipse 无法编译,提示“错误: 找不到或无法加载主类”

    jar包问题: 1.项目的Java Build Path中的Libraries中有个jar包的Source attachment指为了一个不可用的jar包, 解决办法是:将这个不可用的jar包remo ...

  7. eclipse 下找不到或无法加载主类的解决办法

    有时候 Eclipse 会发神经,好端端的 project 就这么编译不了了,连 Hello World 都会报“找不到或无法加载主类”的错误,我已经遇到好几次了,以前是懒得深究就直接重建projec ...

  8. java HelloWorld 提示“错误: 找不到或无法加载主类 HelloWorld“解决方案

    在检查环境变量等前提工作准确无误后,注意要配好CLASSPATH,仍然报“错误: 找不到或无法加载主类 HelloWorld“. 本人工程目录:mygs-maven/src/main/java/hel ...

  9. maven project中,在main方法上右键Run as Java Application时,提示错误:找不到或无法加载主类XXX.XXXX.XXX

    新建了一个maven project项目,经过一大堆的修改操作之后,突然发现在main方法上右键运行时,竟然提示:错误:找不到或无法加载主类xxx.xxx.xxx可能原因1.eclipse出问题了,在 ...

随机推荐

  1. 如何取消WIN7的共享密码

    如何取消WIN7的共享密码 把你的Guest帐号的密码设为空.如何设置呢? 1.右键“计算机”-“管理”-“本地用户和组”-“用户”-右键帐号“Guest”-“设置密码”,然后直接点击确定,不予设置密 ...

  2. elasticsearch2.x优化小结(单节点)

    最近es一直卡顿,甚至宕机,用bigdesk看了,才晓得,es一直用的默认配置(可以看出我有多懒,先前数据量小,es足以应付,现在数据量上去后就不行了). 这里总结三方面: 1.提升jvm内存 vi ...

  3. Luogu 3205 [HNOI2010]合唱队

    初赛滚粗的我含着眼泪写代码…… 设$f_{l, r, 0/1}$表示$[l, r]$的区间的队伍排列好,且最后一个插进去的在左边$(0)$/右边$(1)$的方案数,那么有初态$f_{i, i, 0} ...

  4. HDU 5038 Grade (水题,坑题)

    题意:给 n 个数,输出众数,但是如果所有的频率都相同但数不同输出 Bad Mushroom. 析:直接记录个数直接暴力就,就是要注意只有一种频率的时候. 代码如下: #pragma comment( ...

  5. js/jq基础(日常整理记录)-2-一个简单的js方法实现集合的非引用拷贝

    一.一个简单的js方法实现集合拷贝 做web项目的时候,少不了和js中的数组,集合等对象接触,那么你肯定会发现,在js中存在一个怪异的现象就是数组和集合的拷贝都是地址复制,并不是简单的数据的拷贝. 举 ...

  6. C#/Python/MATLAB操作PostgreSQL数据库

    PostgreSQL数据库是一个功能非常强大的开源数据库,支持多种SQL特性,非常好用.此外由于结合PostGIS可以实现空间数据库功能,故非常适合GIS领域的使用.本文旨在介绍C#.Python.M ...

  7. C++新标准:列表初始化

    一.列表初始化意义 C++新标准为vector提供了一种新的初始化方式:列表初始化.适用于知道多个成员具体值的情况. 二.列表初始化用法 /*1.空vector<int>*/ vector ...

  8. Bugly集成指南

    官网: https://bugly.qq.com/v2/,用QQ扫码登录即可 1.创建应用,获取APPID 2.自动集成 2.1 在Module的build.gradle文件中添加依赖和属性配置: d ...

  9. 搭建TensorFlow

    网上有许多在线安装TensorFlow框架的,我试了好多,结果安装时间长先不说,还总是出现一些问题,然后我就想着离线安装,成功了,与大家分享! (1)首先,需要下载离线安装的TensorFlow包,可 ...

  10. Wannafly挑战赛28B(DP,思维,字符串)

    #include<bits/stdc++.h>using namespace std;int n;int nxt[3][100007];char buff[100007];const ch ...