三部曲一(数据结构)-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 ...
随机推荐
- HDU Coprime
Coprime Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total S ...
- [问题2014A01] 解答一(第一列拆分法,由张钧瑞同学提供)
[问题2014A01] 解答一(第一列拆分法,由张钧瑞同学提供) (1) 当 \(a=0\) 时,这是高代书复习题一第 33 题,可用升阶法和 Vander Monde 行列式来求解,其结果为 \[ ...
- linux(centos )mongodb install
file down :http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.4.9.tgz /usr/local/ tar zxvf mongo ...
- 使用jquery中height()方法获取各种高度大全
alert($(window).height()); //浏览器当前窗口可视区域高度 alert($(document).height()); //浏览器当前窗口文档的高度 alert($(docum ...
- Java vs. C#
Java Program Structure C# package hello; public class HelloWorld { public static void main(String ...
- Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’解决方法 + Linux启动/停止/重启Mysql数据库的方法
启动mysql 报错: ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/m ...
- 描述Linux运行级别0-6的各自含义
中文: 0: 关机模式 1:单用户模式<==破解root密码 2:无网络支持的多用户模式 3:有网络支持的多用户模式(文本模式,工作中最长使用的模式) 4:保留,未使用 5:有网络支持又x-wi ...
- tiny_cnn 阅读(1)
从今天起, 我会每天把阅读tiny_cnn的阅读心得提交到博客园中希望大家在这个平台上可以多多交流: 关于如果阅读代码? 抓住重点,忽略细节 首先打开从github上下载的文件: 通过csdn和网上搜 ...
- python学习------dictionary和set
一.dictionary 1.字典的形式:a={‘key’:value,‘key1’:value,................} 2.字典的的key不能重复,是一个不可变对象 3.字典的的查找和添 ...
- 读javascript高级程序设计03-函数表达式、闭包、私有变量
一.函数声明和函数表达式 定义函数有两种方式:函数声明和函数表达式.它们之间一个重要的区别是函数提升. 1.函数声明会进行函数提升,所以函数调用在函数声明之前也不会报错: test(); functi ...