[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;
}

C语言-光伏MPPT算法:电导增量法扰动观察法+自动全局搜索Plecs最大功率跟踪算法仿真内容概要:本文档主要介绍了一种基于C语言实现的光伏最大功率点跟踪(MPPT)算法,结合电导增量法与扰动观察法,并引入自动全局搜索策略,利用Plecs仿真工具对算法进行建模与仿真验证。文档重点阐述了两种经典MPPT算法的原理、优缺点及其在不同光照和温度条件下的动态响应特性,同时提出一种改进的复合控制策略以提升系统在复杂环境下的跟踪精度与稳定性。通过仿真结果对比分析,验证了所提方法在快速性和准确性方面的优势,适用于光伏发电系统的高效能量转换控制。; 适合人群:具备一定C语言编程基础和电力电子知识背景,从事光伏系统开发、嵌入式控制或新能源技术研发的工程师及高校研究人员;工作年限1-3年的初级至中级研发人员尤为适合。; 使用场景及目标:①掌握电导增量法与扰动观察法在实际光伏系统中的实现机制与切换逻辑;②学习如何在Plecs中搭建MPPT控制系统仿真模型;③实现自动全局搜索以避免传统算法陷入局部峰值问题,提升复杂工况下的最大功率追踪效率;④为光伏逆变器或太阳能充电控制器的算法开发提供技术参考与实现范例。; 阅读建议:建议读者结合文中提供的C语言算法逻辑与Plecs仿真模型同步学习,重点关注算法判断条件、步长调节策略及仿真参数设置。在理解基本原理的基础上,可通过修改光照强度、温度变化曲线等外部扰动因素,进一步测试算法鲁棒性,并尝试将其移植到实际嵌入式平台进行实验验证。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值