三部曲一(数据结构)-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 ...
随机推荐
- 20160308001 GridView的Sorting排序
参考地址: http://www.cnblogs.com/yinluhui0229/archive/2011/08/01/2124169.html 功能介绍:单击gridview的某一列列头,可以对该 ...
- 【java报错】Could not instantiate listener
这个错误以前出现过好几次,莫名其妙的出现,莫名其妙的解决掉...昨天好好的,今天又出现了,记下来 2015-03-03 09:38:45.105:INFO:oejs.Server:jetty-8.1. ...
- MacBook 显示隐藏文件夹命令
1. 显示:defaults write com.apple.finder AppleShowAllFiles -bool true 第一步:命令行执行上述命令 第二步:将Finder重新打开 第三步 ...
- [转]C#读写TEXT文件
1.使用FileStream读写文件 文件头: using System; using System.Collections.Generic; using System.Text; using Sys ...
- 我的android学习经历37
android虚拟机没有键盘的问题,那是因为建立虚拟机的时候没有选择skin,重新编辑选择skin就可以了
- Bootstrap_标签
一.标签 在一些Web页面中常常会添加一个标签用来告诉用户一些额外的信息,比如说在导航上添加了一个新导航项,可能就会加一个“new”标签,来告诉用户. 那么在Bootstrap框架中特意将这样的效果提 ...
- SerialChat与Arduino的配合使用
最近在开发过程中,用到了Arduino开发板以及其IDE:Arduino,这个IDE使用起来很方便,编码也很简单,但有一点美中不足的是Arduino只能输出数值,不能绘图,所以就用到了另外一款串口调试 ...
- 关于Youtube URL的十个技巧
你一定很熟悉Youtube了,知道它是一个视频分享网站.是的,youtube目前十分流行,你也许会常常访问.这里有一些关于youtube url的技巧,了解了这些技巧,你就可以更好的利用youtube ...
- TextInfo
https://msdn.microsoft.com/en-us/library/system.globalization.textinfo.totitlecase(v=vs.110).aspx Co ...
- Struts2拦截器初涉
Struts2拦截器初涉 正在练习struts,本例是从一个pdf上摘抄的例子,那本pdf都不知道叫什么名字,不过感觉很适合初学者. 在这里要实现一个简单的拦截器"GreetingInter ...