题目传送门

题目大意:有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            16          0 1 1            27          1 1 1            32          0 1 0            41          1 0 0            54          0 0 1            62          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<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

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

poj3274 找平衡数列(哈希加一点数学思维)相关推荐

  1. Error 错误: 找不到或无法加载主类

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

  2. 在命令窗口执行java文件时,提示找不到或无法加载主类

    在命令窗口执行java文件时,提示找不到或无法加载主类 以前写java代码的时候,都是在Eclipse或者IDEA等集成开发工具上进行,所以编译和测试代码的时候都是一键执行,其中的原理简单来说,就是先 ...

  3. 各种体+把Σ的上标打到正上方+箭头加东西+矩阵+圆圈+向量各种箭头+头上加一点

    文章目录 最花的花体 各种体 把Σ的上标打到正上方 箭头丧面加东西 latex输入矩阵 圆圈 加向量 头上加一点 我感觉啊csdn的编辑器已经暗自导入这些包了! 符号找不到点这里哦 最花的花体 \us ...

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

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

  5. hadoop错误: 找不到或无法加载主类 org.apache.hadoop.mapreduce.v2.app.MRAppMaster

    错误: 找不到或无法加载主类 org.apache.hadoop.mapreduce.v2.app.MRAppMaster 原创hongxiao2016 最后发布于2019-03-30 21:20:5 ...

  6. 错误: 找不到或无法加载主类 com.wdg.auditproject.auditproject.action.TestClass

    今天在web项目中创建一个测试的类TestClass,然后在里面写主方法,就像这样: package com.wdg.auditproject.action;public class TestClas ...

  7. java报错-找不到或无法加载主类(Error: Could not find or load main class)

    此文首发于我的个人博客:java报错-找不到或无法加载主类(Error Could not find or load main class) - zhang0peter的个人博客 比如说test.ja ...

  8. 计算机中英语GAI缩写,等等英语_英语中“等等”缩写成为etc吗要加一点吗全拼是什么谢谢大家_淘题吧...

    ㈠ 等等 英文怎么写 英语为等待:等待 a 分钟.因为 等待 a 分钟英语 [weit minit]. 美人 [湿 e mnt] 等等:等一下:慢慢走. 等待 a 分钟的用法示例如下: 1.等等,这是 ...

  9. java问题:错误: 找不到或无法加载主类 HelloWorld

    有时候我们需要直接用jdk提供的java命令来执行class文件让软件运行起来,特别是很多初学者,但经常会发现如下提示: D:\java\eclipse-workspace\first\src\fir ...

  10. 找不到或无法加载主类 org.jivesoftware.openfire.starter.ServerStarter

    clean项目后出现 找不到或无法加载主类 org.jivesoftware.openfire.starter.ServerStarter 1. 出现上面的错误时,在Ant中重新build.

最新文章

  1. 关于局域网共享访问问题总结
  2. 9.mybatis动态SQL标签的用法
  3. CnetOS中开关网络服务
  4. 【福利解锁Part1】报名参与腾讯云专场活动,第一波干货内容免费放送
  5. [UE4]更新UI的三种方式
  6. web前端基础(01html基本标签)
  7. ASP.NET 文件后缀名详解
  8. JDK历史所有版本下载地址(附Oracle帐号)
  9. python插值算法_python插值算法
  10. 软件测试之蚂蚁金服面试题,含答案
  11. 鑫迪自助建站系统1.1版网站源码完整源码
  12. 计算机相关期刊,计算机相关杂志
  13. 【20220108】【雷达】毫米波雷达(二)—— 毫米波雷达和激光雷达的区别及优缺点比较
  14. 别再手动比对文件啦,Python 让你轻松实现文件内容以及目录对比!!!
  15. 决策树算法的 MATLAB 实践
  16. 轻量级 Linux 发行版CRUX 发布 3.4 版
  17. 鸿蒙系统的结构图,鸿蒙图形系统整体框架及图像子系统
  18. 两分钟学会用ADC0804采集数据
  19. 电池洗地机进行更换电池时需要注意啥?
  20. 联咏(Novatek)NT98562M/NT98566M成像处理器Datasheet、SDK工具包下载

热门文章

  1. java判断字符串不为空_Java判断字符串是否为空的方法
  2. c语言 入字符要中文状态吗,常见的HTTP状态码深入理解
  3. 二叉树查找结点及父结点_【剑指offer】57 二叉树的下一个结点
  4. matmul torch 详解_python基础教程详解torch.Tensor的4种乘法
  5. 超轻薄笔记本电脑软件测试,一口气测了三款轻薄本 这三个核心问题有答案了...
  6. 解析极限编程--Kent Beck, Cynthia Andres读后感
  7. 用html设计倒计时秒表,Javascript实现秒表倒计时功能
  8. 广义注意力- saliency map 关注图、gaze、Att
  9. 473.火柴拼正方形
  10. 判断数组中的元素是否连续