三部曲一(数据结构)-1022-Gold Balanced Lineup
Gold Balanced Lineup
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 3 Accepted Submission(s) : 3
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 Kdifferent 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.
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.
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <cmath>
#include <stdlib.h>
#define maxn 100010
#define seed 999983
using namespace std; int n,k;
int feature[maxn][],sum[maxn][],c[maxn][],maxlen=; struct Hash
{
int id,next;
} h[]; bool compair(int a,int b)
{
int i;
for(i=; i<k; i++)
{
if(c[a][i]!=c[b][i])
return false;
}
return true;
} int count_key(int index)
{
int i,key=;
//Hash公式
for(i=; i<k; i++)
key=((key<<)+(c[index][i]>>))^(c[index][i]<<);
key=key%seed;
if(key<)
key=key+seed;
return key;
} //int count_key(int index)
//{
// int i,key=0;
// for(i=0; i<k; i++)
// {
// key+=abs(c[index][i]);
// key%=seed;
// }
// return key;
//} void InsertAndCheck(int index)
{
int i,key,last=;
key=count_key(index);
// cout<<"key="<<key<<endl;
// cout<<key<<endl;
if(h[key].id==-)
{
h[key].id=index;
}
else
{
bool found=false;
for(i=key; i!=-; i=h[i].next)
{
// cout<<"run "<<index<<' '<<i<<endl;
last=i;
if(!found&&compair(index,h[i].id))
{
if(index-h[i].id>maxlen)
{
maxlen=abs(h[i].id-index);
found=true;
}
}
}
for(i=last; h[i].id!=-; i=(i+)%seed);
h[last].next=i;
h[i].id=index;
}
} int main()
{
// freopen("in.txt","r",stdin);
memset(h,-,sizeof(h));
scanf("%d%d",&n,&k);
int i,j,tmp;
n++;
for(i=; i<maxn; i++)
h[i].next=-;
InsertAndCheck();
for(i=; i<=n; i++)
{
scanf("%d",&tmp);
for(j=; j<k; j++)
{
feature[i][j]=tmp%;
sum[i][j]=sum[i-][j]+feature[i][j];
c[i][j]=sum[i][j]-sum[i][];
tmp>>=;
}
InsertAndCheck(i);
}
//// for(i=0;i<=n;i++)
//// {
//// for(j=0;j<k;j++)
//// cout<<c[i][j]<<' ';
//// cout<<endl;
//// }
// for(i=0;i<6;i++)
// cout<<h[i].id<<' '<<h[i].next<<endl;
printf("%d\n",maxlen);
return ;
}
三部曲一(数据结构)-1022-Gold Balanced Lineup的更多相关文章
- 洛谷 P1360 [USACO07MAR]Gold Balanced Lineup G (前缀和+思维)
P1360 [USACO07MAR]Gold Balanced Lineup G (前缀和+思维) 前言 题目链接 本题作为一道Stl练习题来说,还是非常不错的,解决的思维比较巧妙 算是一道不错的题 ...
- POJ 3274 Gold Balanced Lineup
Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10924 Accepted: 3244 ...
- 哈希-Gold Balanced Lineup 分类: POJ 哈希 2015-08-07 09:04 2人阅读 评论(0) 收藏
Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13215 Accepted: 3873 ...
- 1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列
1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 510 S ...
- POJ 3274:Gold Balanced Lineup 做了两个小时的哈希
Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13540 Accepted: ...
- Gold Balanced Lineup POJ - 3274
Description Farmer John's N cows (1 ≤ N ≤ 100,000) share many similarities. In fact, FJ has been abl ...
- POJ 3274 Gold Balanced Lineup 哈希,查重 难度:3
Farmer John's N cows (1 ≤ N ≤ 100,000) share many similarities. In fact, FJ has been able to narrow ...
- Gold Balanced Lineup(哈希表)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10711 Accepted: 3182 Description Farm ...
- bzoj 1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列——map+hash+转换
Description N(1<=N<=100000)头牛,一共K(1<=K<=30)种特色, 每头牛有多种特色,用二进制01表示它的特色ID.比如特色ID为13(1101), ...
- poj3274 Gold Balanced Lineup(HASH)
Description Farmer John's N cows (1 ≤ N ≤ 100,000) share many similarities. In fact, FJ has been abl ...
随机推荐
- python-判断系统平台
1.windows 2.linux 总结 python提供了sys,os及platform等个模块读取平台信息,客官可以根据自己的喜好选择使用
- JAVA基础知识之NIO.2——Path,Paths,Files
NIO.2 JDK7对NIO进行了重大改进,主要包含以下两方面 新增Path接口,Paths工具类,Files工具类. 这些接口和工具类对NIO中的功能进行了高度封装,大大简化了文件系统的IO编程. ...
- [问题2014A12] 解答
[问题2014A12] 解答 将问题转换成几何的语言: 设 \(\varphi,\psi\) 是 \(n\) 维线性空间 \(V\) 上的线性变换, 满足 \(\varphi\psi=\psi\va ...
- ZIP等
file命令可以查看文件的类型 tar类型 .targzip类型 .gz bzip2类型 .bz2zip类型 .zip 如果一个压缩文件由tar命令解压的前提,2个条件1.这个文件必须是由tar打包之 ...
- C# 蓝牙编程
C#进行蓝牙编程 本节我们给大家用源码的形式给大家介绍如何用C#调用蓝牙.下面的源码是基于destop的C#调用蓝牙的程序,也就是使用普通版本的.NET Framework来调用编程,一般是有蓝牙的笔 ...
- [渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:为ASP.NET MVC应用程序更新相关数据
这是微软官方教程Getting Started with Entity Framework 6 Code First using MVC 5 系列的翻译,这里是第八篇:为ASP.NET MVC应用程序 ...
- [渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:为ASP.NET MVC应用程序处理并发
这是微软官方教程Getting Started with Entity Framework 6 Code First using MVC 5 系列的翻译,这里是第十篇:为ASP.NET MVC应用程序 ...
- 三种DSO(标准DSO、写优化DSO、直接更新DSO)、标准DSO覆盖合计规则
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- js获取url中的参数值
在博客园看家一片不错的文章:http://www.cnblogs.com/EnderH/p/5398409.html 在这里记下对自己比较有用的三种: 1.正则 function GetQuerySt ...
- Pycharm使用问题# 快捷键设置
Pycharm内部设置也已经变得非常复杂,此处说明我改变字体大小的快捷键设置. 在Settings中找到Keymap一项,默认显示会展开Editor Actions一项: 在里面分别找到Decreas ...