三部曲一(数据结构)-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 ...
随机推荐
- 仿souhu页面设计
仿搜狐页面设计 Html页面设计代码: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> ...
- 【C++/Qt】Qt中的parent形参
在 派生类的构造函数初始化列表中 调用 父类的带有参数的构造函数,是为了初始化从父类继承来的成员变量.因为这些变量无法直接初始化,只能采用这种方式初始化. 而在qt中,MainWindow中的某成员变 ...
- [渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:为ASP.NET MVC应用程序读取相关数据
这是微软官方教程Getting Started with Entity Framework 6 Code First using MVC 5 系列的翻译,这里是第七篇:为ASP.NET MVC应用程序 ...
- shell中 "" 跟 ''的区别
在bash里,这两个都是引号,用来表明字符串,区别是,双引号中的变量会被展开,而单引号中不再展开.举个例子:a="abc"echo "str=$a" # 结果显 ...
- http知识
http请求的过程:客户端发起请求,创建端口:http服务器在端口监听客户端请求:http服务器向客户端返回状态和内容. 浏览器搜索自身的DNS缓存-->搜索操作系统自身的DNS缓存(浏览器没有 ...
- (转载)TCP/IP的三次握手与四次挥手
TCP三次握手 所谓三次握手(Three-way Handshake),是指建立一个TCP连接时,需要客户端和服务器总共发送3个包. 三次握手的目的是连接服务器指定端口,建立TCP连接,并同步 ...
- Android pulltorefresh使用
pulltorefresh插件可以轻松实现上拉下拉刷新,github.com上直接搜索进行下载. 布局文件: <RelativeLayout xmlns:android="http:/ ...
- java.lang.UnsupportedClassVersionError: org/apache/maven/cli/MavenCli : Unsupported major.minor version 51
http://blog.csdn.net/e_wsq/article/details/52100234 一日换了一下MyEclipse,换成2016CI,结果从SVN上下载了一个工程后出现以下错误: ...
- 【转】关于 Web GIS
以下部分选自2015-03-01出版的<Web GIS从基础到开发实践(基于ArcGIS API for JavaScript)>一书中的前言部分: Web GIS 概念于1994 年首次 ...
- DES MAC PIN HEX
/* void DesEncrypt( UCHAR * auchInput,UCHAR * auchKey,UCHAR * auchOutput=NULL); Function: DesEncrypt ...