[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/16a53f4bd595 小天才电话手表刷机教程 — 基础篇 我们将为您简单的介绍小天才电话手表新机型的简单刷机以及玩法,如adb工具的使用,magisk的刷入等等。 我们会确保您看完此教程后能够对Android系统有一个最基本的认识,以及能够成功通过magisk root您的手表,并安装您需要的第三方软件。 ADB Android Debug Bridge,简称,在android developer的adb文档中是这么描述它的: 是一种多功能命令行工具,可让您与设备进行通信。 该命令有助于各种设备操作,例如安装和调试应用程序。 提供对 Unix shell 的访问,您可以使用它在设备上运行各种命令。 它是一个客户端-服务器程序。 这听起来有些难以理解,因为您也没有必要去理解它,如果您对本文中的任何关键名词产生疑惑或兴趣,您都可以在搜索引擎中去搜索它,当然,我们会对其进行简单的解释:是一款在命令行中运行的,用于对Android设备进行调试的工具,并拥有比一般用户以及程序更高的权限,所以,我们可以使用它对Android设备进行最基本的调试操作。 而在小天才电话手表上启用它,您只需要这么做: - 打开拨号盘; - 输入; - 点按打开adb调试选项。 其次是电脑上的Android SDK Platform-Tools的安装,此工具是 Android SDK 的组件。 它包括与 Android 平台交互的工具,主要由和构成,如果您接触过Android开发,必然会使用到它,因为它包含在Android Studio等IDE中,当然,您可以独立下载,在下方选择对应的版本即可: - Download SDK Platform...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值