三部曲一(数据结构)-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 ...
随机推荐
- zigbee学习之路(四):按键控制(中断方式)
一.前言 通过上次的学习,我们学习了如何用按键控制led,但是在实际应用中,这种查询方式占用了cpu的时间,如果通过中断控制就可以解决这个问题,我们今天就来学习按键控制的中断方式. 二.原理分析 传统 ...
- 微信JS SDK PHP Demo
一.JSSDK类定义 <?php class JSSDK { private $appId; private $appSecret; public function __construct($a ...
- Linux常见命令汇总
1.rz sz上传下载 若未安装使用:yum install lrzsz 安装 上传: rz 覆盖上传: rz -y 下载: sz bbb.jpg
- 2015弱校联盟(1) - B. Carries
B. Carries Time Limit: 1000ms Memory Limit: 65536KB frog has n integers a1,a2,-,an, and she wants to ...
- 改变Web Browser控件IE版本
默认的webbrowser控件使用的渲染模式版本似乎是IE7,想要更改更高版本,如下: 在注册表位置 HKEY_CURRENT_USER\Software\Microsoft\Internet Exp ...
- Jeff Dean
"--出自"关于 Jeff Dean 的事实" 其实,"关于 Jeff Dean 的事实"这个G+ 帖中描述的并非是真实的.不过有人大费周折为他建立了 ...
- OpenCV 绘制图像直方图
OpenCV绘制图像直方图,版本2.4.11 直方图可展示图像中的像素分布,是用以表示数字图像中亮度分布的直方图,标绘了图像中每个亮度值的像素数.可以借助观察该直方图了解需要如何调整亮度分布.这种直方 ...
- [poj2777] Count Color (线段树 + 位运算) (水题)
发现自己越来越傻逼了.一道傻逼题搞了一晚上一直超时,凭啥子就我不能过??? 然后发现cin没关stdio同步... Description Chosen Problem Solving and Pro ...
- [Oracle] SQL*Loader 详细使用教程(4)- 字段列表
在上一篇中我们介绍了SQL*Loader中最重要的文件——控制文件,而本篇要介绍控制文件中最重要的部分——字段列表,字段列表的作用是把数据文件中的记录和数据库中表的列对应起来,下面是字段列表的一个例子 ...
- Nginx 1.10.2 php 7 环境安装
1.安装编译工具和库文件,红色部分提示在centos镜像站点上查不到包,用yum安装的时候要认真看那些包没有找到,用yum的时候尽量不要使用-y选项 yum install gcc automake ...