[POJ3274]-Gold Balanced Lineup

本文解析了一道算法题——寻找具有平衡特性的最大牛群区间,并提供了详细的解题思路及实现代码。通过将每头牛的特征转换为二进制形式并利用哈希表优化搜索过程。
Gold Balanced Lineup
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 14483 Accepted: 4196

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
题目描述:
这个题目意思相信有一很大部分人都没看清楚,我的英语也是硬伤,看题目时看了半天一头雾水,弱弱的抱怨一句WTF!,所以默默的去大神的博客去弄懂题目,看了一会,题目终于看清楚了,后来我发现我没看Hint,其实想想没那么难懂,静下心好好看,多揣摩揣摩,题意也就明白了,可能有些童鞋题目遇到我这种问题,我弱弱的说下题目意思吧!剧情就不说了,我说说理解的重点:
每个牛有一个ID,把这个ID用二进制来表示,如果该位为1表示有这种特性,为0表示没有此特性,如果存在一个连续cowID他们每种特性和加起来是相等的,说明该连续cowID是一个平衡区间,要求求得最大的平衡区间。
解题思路:
通过以上描述题意应该明白了吧!
拿这个输入输出分析吧!第一行输入7 代表下面有7个cowID,3代表几种特性。接下来是7个cowID
1 7 ---------->1 1 1
2 6 ---------->1 1 0
3 7 ---------->1 1 1
4 2 ---------->0 1 0
5 1 ---------->0 0 1
6 4 ---------->1 0 0
7 2 ---------->0 1 0
可以清楚的看到3456这几个ID每种特征和相等都等于2,这个特征可以作为我们解题的关键,如果我们将其每种特性都累加起来:
1 7 ---------->1 1 1
2 6 ---------->2 2 1
3 7 ---------->3 3 2
4 2 ---------->3 4 2
5 1 ---------->3 4 3
6 4 ---------->4 4 3
7 2 ---------->4 5 3
然后同时减去第一列的值,
1 7 ---------->0 0 0
2 6 ---------->1 1 0
3 7 ---------->1 1 0
4 2 ---------->1 2 0
5 1 ---------->0 1 0
6 4 ---------->1 1 0
7 2 ---------->1 2 0
我们发现第二行和第六行相等,想想为什么会相等,很容易明白的,因为我们之前分析了3456cowID每种特性和加起来是相等的,那么你加到第六个cowID时相对于第二行来说,是不是每种特性都同时加上了一个random值,这里random值为2,然后你同时都减去最后一种特性,当然第六行每种特性也就等于第二行的每种特性,这样解释应该是比较通熟易懂了。
下面就到了最后的阶段,如何设计算法解题,我是看了一个大神的blog,根据他的思想写的,这里用到了一个对一个数组hash的算法,这个hashcode算法是从Unix下面常用的一个对字符串进行hash处理的方式借鉴过来的,也就是把char* 改为int* ,其余原封不动的搬了过来。
结构体变量cowb[]存储每个cowIDcow[i].b存储的是i cowID编号的二进制。
cnt[].b记录的就是累加后再减去最后一列的得特性值
代码如下
#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
const int maxn = 1000001;
const int prime = 1000003;
const int INF = -1;
struct node
{
	int b[31];//存储每种特性
}cowb[maxn],cnt[maxn];
int n,k;
int HASH[prime];
int hashcode(const int *v,int m)
{
	int p=0;
	for(int i = 1;i <= m;++ i)
		p = ((p<<2)+(v[i]>>4))^(v[i]<<10);
	p=p%prime;
	p = p<0? (p+prime):p;
	return p;
}
void debug_print()
{
	for(int j = 1;j <= k;++ j)
	{
		printf("%d ",cnt[i].b[j]);
	}
	cout << endl;
}
int main()
{
	freopen("data3274.in","r",stdin);
	while(~scanf("%d%d",&n,&k))
	{
		memset(cowb,0,n);
		memset(cnt,0,n);
		memset(HASH,INF,prime);
		HASH[hashcode(cnt[0].b,k)]=0;//初始化
		int res=0;
		for(int i = 1;i <= n;++ i)
		{
			int cow;
			scanf("%d",&cow);
			for(int j = 1;j <= k;++ j)
			{
				int b = cow%2;
				cowb[i].b[j]=b+cowb[i-1].b[j];
				cnt[i].b[j] = cowb[i].b[j]-cowb[i].b[1];
				cow/=2;
			}
			// debug_print();
			int p=hashcode(cnt[i].b,k);//每次获得该cowID处理后的hahs值
			while(HASH[p]!=-1)
			{
				bool flag=true;//判断每个特性是否相同
				for(int j = 1;j <= k;++ j)
					if(cnt[i].b[j]!=cnt[HASH[p]].b[j])
					{
						flag=false;//如果有一个特性值不同就为false
						break;
					}
				if(flag)
				{
					res=max(res,i-HASH[p]);//若果找打了就把res更新
					break;
				}
				//printf("res = %d\n",res);
				p++;
			}
			if(HASH[p]==-1)//没有被赋值的话
				HASH[p]=i;//将其赋值为i cowID
		}
		printf("%d\n",res);
	}
	return 0;
}

源码地址: https://pan.quark.cn/s/d1f41682e390 miyoubiAuto 米游社每日米游币自动化Python脚本(务必使用Python3) 8更新:更换cookie的获取地址 注意:禁止在B站、贴吧、或各大论坛大肆传播! 作者已退游,项目不维护了。 如果有能力的可以pr修复。 小引一波 推荐关注几个非常可爱有趣的女孩! 欢迎B站搜索: @嘉然今天吃什么 @向晚大魔王 @乃琳Queen @贝拉kira 第三方库 食用方法 下载源码 在Global.py中设置米游社Cookie 运行myb.py 本地第一次运行时会自动生产一个文件储存cookie,请勿删除 当前仅支持单个账号! 获取Cookie方法 浏览器无痕模式打开 http://user.mihoyo.com/ ,登录账号 按,打开,找到并点击 按刷新页面,按下图复制 Cookie: How to get mys cookie 当触发时,可尝试按关闭,然后再次刷新页面,最后复制 Cookie。 也可以使用另一种方法: 复制代码 浏览器无痕模式打开 http://user.mihoyo.com/ ,登录账号 按,打开,找到并点击 控制台粘贴代码并运行,获得类似的输出信息 部分即为所需复制的 Cookie,点击确定复制 部署方法--腾讯云函数版(推荐! ) 下载项目源码和压缩包 进入项目文件夹打开命令行执行以下命令 xxxxxxx为通过上面方式或取得米游社cookie 一定要用双引号包裹!! 例如: png 复制返回内容(包括括号) 例如: QQ截图20210505031552.png 登录腾讯云函数官网 选择函数服务-新建-自定义创建 函数名称随意-地区随意-运行环境Python3....
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值