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 K different 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.

Input

Line 1: Two space-separated integers, N and K.
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.

Output

Line 1: A single integer giving the size of the largest contiguous balanced group of cows.

感想:

本来以为是dp,一次tle才发现N*N有点问题,改成hash之后这个函数并不太好,但是有负数,稍后再去找个分布更好的函数吧

文章翻译:

农民约翰的要去展会的N头牛(1≤N≤100000)有许多相似之处。事实上,约翰给牛列出K种不同特性(1≤K≤30)并制表。例如,牛# 1可能有斑点,牛# 2可能更喜欢C ,等等。先进又无聊的农夫甚至设计了一种简明的方式--特性ID来描述每个牛.ID是一个K-bit整数的二进制,表示告诉我们展出的牛的特性。作为一个例子,假设一个牛特性ID = 13。因为1101=13,这意味着我们的牛展品有特性1、3和4(阅读右到左),而不是特性2,作为一个没事儿秀智商的农夫,约翰把牛赋予序号从1-N排序,而且发现牛的特性在某个区间内是平衡的,(平衡的,展示每个特征的牛总数相同.)约翰好奇最大的平衡区间长度,请你找出它

输入

第一行 N: 总牛数 K: 总特征数

第二行到N+1行 第i行代表第i-1号牛的特征ID

输出

一行 最大平衡区间长度+换行

题目理解:

1 阅读从哪个方向都一样

2 设区间[i,j],按位相加压缩成一行k,对单一行,当其他特征相对于第一个特征为0时,所有特征这一行满足条件

3 设区间[i,j],令memo[i][0]=0,memo[i][ki]为第ki个特征相对于第一个特征的积累值,若memo[i]=memo[j],那么区间[i+1,j]满足平衡,平衡长度为i-j

//13080K 610MS

#include <iostream>
#include<cstdlib>
#include <cstring>
using namespace std;
const int MAXF =30;
const int MAXN=100005;
const int MAXK=(1<<14);
int memo[MAXN][MAXF];//权值储存在memo里
int first[MAXK];//采用邻接表形式存储,也即存储关系R {<x,y>|<first[i],j>,<j,next[j]> i是键值.j则为牛的序号}
int next[MAXN];//下一条
int num[MAXK];//方便对每个键值对应的边权查重,存储对应键值加入了多少
bool vis[MAXN];//是否已经查重完毕,因为关系<next[i],i>没有重复所以不用memset,否则反而败笔
int n,k;
#define CONJECTURE(n) if(n) {cout<<"ERROR"<<endl;exit(-1);}//
bool cmp(int a,int b){//边权查重
for(int i=0;i<k;i++){
if(memo[a][i]!=memo[b][i])return false;
}
return true;
}
int ckey(int i){//键值分配
int ans=0;
for(int j=0;j<k;j++){
ans+=memo[i][j];
}
ans=ans>=0?ans:-ans;//错误一次,有负值,没有改动整个函数只是改为绝对值
ans%=MAXK;
return ans;
}
int main(){
ios::sync_with_stdio(false);
//一:输入
 cin>>n>>k;
int temp;
// if(k==1){cout<<n<<endl;return 0;}
//bool fl=false;//有没有都可以..加入了第0行:000000...00,这意味着存在完全状态也会检出
// if(temp==(1<<k)-1)fl=true;
cin>>temp;
for(int j=1;j<k;j++){
memo[1][j]=((temp&(1<<j))>>j)-(temp&1);
}
for(int i=2;i<=n;i++){
int temp;
cin>>temp;
// if(temp==(1<<k)-1)fl=true;
for(int j=1;j<k;j++){
memo[i][j]=memo[i-1][j]+((temp&(1<<j))>>j)-(temp&1);//错误一次,忘记了temp&(1<<j)不是1
}
}
memset(first,-1,sizeof(first));//加入了<0,0>点,所以起点定-1
memset(next,-1,sizeof(next));
//二:添加边
 for(int i=0;i<=n;i++){
int keynum=ckey(i);
if(first[keynum]!=-1){
next[i]=first[keynum];
}
first[keynum]=i;
num[keynum]++;
}
//三:比较
 int ans=0;
int minn,maxn;
for(int i=0;i<MAXK;i++){
int start;
while(num[i]){
int p=first[i];
//bool fl=false;
while(p!=-1){
if(!vis[p]){
start=p;
// fl=true;
break;
}
p=next[p];
}
minn=maxn=start;
vis[start]=true;
num[i]--;
while(next[p]!=-1){
p=next[p];
if(vis[p])continue;
if(cmp(start,p)){
minn=p;
vis[p]=true;
num[i]--;
}
}
ans=max(maxn-minn,ans);
}
}
//四:输出
 cout<<ans<<endl;
return 0;
}

POJ 3274 Gold Balanced Lineup 哈希,查重 难度:3的更多相关文章

  1. poj 3274 Gold Balanced Lineup(哈希 )

    题目:http://poj.org/problem?id=3274 #include <iostream> #include<cstdio> #include<cstri ...

  2. POJ 3274 Gold Balanced Lineup

    Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10924 Accepted: 3244 ...

  3. POJ 3274 Gold Balanced Lineup(哈希)

    http://poj.org/problem?id=3274 题意 :农夫约翰的n(1 <= N <= 100000)头奶牛,有很多相同之处,约翰已经将每一头奶牛的不同之处,归纳成了K种特 ...

  4. Gold Balanced Lineup(哈希表)

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10711   Accepted: 3182 Description Farm ...

  5. POJ 3274:Gold Balanced Lineup 做了两个小时的哈希

    Gold Balanced Lineup Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13540   Accepted:  ...

  6. 哈希-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 ...

  7. 1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列

    1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 510  S ...

  8. 洛谷 P1360 [USACO07MAR]Gold Balanced Lineup G (前缀和+思维)

    P1360 [USACO07MAR]Gold Balanced Lineup G (前缀和+思维) 前言 题目链接 本题作为一道Stl练习题来说,还是非常不错的,解决的思维比较巧妙 算是一道不错的题 ...

  9. 【POJ】3264 Balanced Lineup ——线段树 区间最值

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34140   Accepted: 16044 ...

随机推荐

  1. tracert命令详解

    一.windows.Linux系统下 tracert ip/网站域名 二.mac traceroute IP/域名 ---------2016-10-10 15:29:07-- source:[1]t ...

  2. 微信开发时遇到的UrlConnection乱码的问题

    昨天做一个微信的模板消息推送的功能,功能倒是很快写完了,我本地测试微信收到的推送消息是正常的,但是一部署到服务器后微信收到的推送消息就变成乱码了. 为了找到原因,做了很多测试,查了一下午百度,最后得出 ...

  3. jquery input选择弹框

    index.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pag ...

  4. Python学习笔记8—语句

    条件语句 有的程序里写的是 /usr/bin Python,表示 Python 解释器在/usr/bin 里面.但是,如果写成 /usr/bin/env,则表示要通过系统搜索路径寻找 Python 解 ...

  5. error C2783: 无法为“T”推导 模板 参数

    原则:“模板参数推导机制无法推导函数的返回值类型” 版本一: // 缺少<T> 参数 int n 对比第三个版本( 缺少<T> 参数 T n) ! 编译错误提示: 错误 1 e ...

  6. js上下滚屏效果,代码通过测试

    这是html代码 <div class="box"> <div class="bcon"> <h1><b>领号实 ...

  7. hdu 2102

    简单的3维BFS 大写的YES和NO,这心粗的....唉 #include<iostream> #include<cstdio> #include<queue> u ...

  8. hibernate的懒加载问题

    产生原因: 当使用hibernate查询一个对象的时候,如果Session关闭,再调用该对象关联的集合或者对象的时候,会产生懒加载异常! 解决方案: 方案一: 在Session关闭之前,查询对象关联的 ...

  9. hdu----(4521)小明系列问题——小明序列

    小明系列问题——小明序列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tota ...

  10. 你不知道的JavaScript--大白话讲解Promise

    转载:http://blog.csdn.net/i10630226/article/details/50867792 一.Promise小试 复杂的概念先不讲,我们先简单粗暴地把Promise用一下, ...