博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj3274 找平衡数列(哈希加一点数学思维)
阅读量:5331 次
发布时间:2019-06-14

本文共 3968 字,大约阅读时间需要 13 分钟。

题目大意:有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

举样例来说明一下:

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 11 2 22 3 32 4 33 4 33 4 43 5 4都减去第一列得c[i]:0 0 00 1 10 1 10 2 10 1 00 1 10 2 1所以说  最大区间是  6-2 = 4

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

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#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
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<
<
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 37672142

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

转载于:https://www.cnblogs.com/mountaink/p/9536723.html

你可能感兴趣的文章
LAMP、LNMP实战之四搭建mysql(持续更新)
查看>>
iOS 开发者必知的 75 个工具(译文)
查看>>
rabbitmq
查看>>
原型学习
查看>>
编程数学-中括号
查看>>
缓存-System.Web.Caching.Cache
查看>>
关于迭代器
查看>>
c++命名空间
查看>>
Excel文件按照指定模板导入数据(用jxl.jar包)
查看>>
Hive基本操作与案例
查看>>
React的使用与JSX的转换
查看>>
HDU 5056 Boring count
查看>>
HDU 6134 Battlestation Operational(莫比乌斯反演)
查看>>
Android开发规范——命名
查看>>
ssdb binlog机制 存疑
查看>>
Vue 2.0 组件库总结
查看>>
HDU5033 Building(单调栈)
查看>>
Kafka 安装配置 及 简单实验记录
查看>>
想成为程序猿?28个程序员专供在线学习网站(转)
查看>>
font-style: oblique文字斜体,display:inline-block显示间隙
查看>>