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 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
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
感想:
本来以为是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的更多相关文章
- poj 3274 Gold Balanced Lineup(哈希 )
题目:http://poj.org/problem?id=3274 #include <iostream> #include<cstdio> #include<cstri ...
- POJ 3274 Gold Balanced Lineup
Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10924 Accepted: 3244 ...
- POJ 3274 Gold Balanced Lineup(哈希)
http://poj.org/problem?id=3274 题意 :农夫约翰的n(1 <= N <= 100000)头奶牛,有很多相同之处,约翰已经将每一头奶牛的不同之处,归纳成了K种特 ...
- Gold Balanced Lineup(哈希表)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10711 Accepted: 3182 Description Farm ...
- POJ 3274:Gold Balanced Lineup 做了两个小时的哈希
Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13540 Accepted: ...
- 哈希-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 ...
- 洛谷 P1360 [USACO07MAR]Gold Balanced Lineup G (前缀和+思维)
P1360 [USACO07MAR]Gold Balanced Lineup G (前缀和+思维) 前言 题目链接 本题作为一道Stl练习题来说,还是非常不错的,解决的思维比较巧妙 算是一道不错的题 ...
- 【POJ】3264 Balanced Lineup ——线段树 区间最值
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34140 Accepted: 16044 ...
随机推荐
- 【转】C/C++ struct/class/union内存对齐
原文链接:http://www.cnblogs.com/Miranda-lym/p/5197805.html struct/class/union内存对齐原则有四个: 1).数据成员对齐规则:结构(s ...
- poj1819Disks
链接 题意:从左到右按顺序给你n个圆的半径,把左右两边想象成两堵墙的话,就是左右两边向里挤压,问哪些圆是对最后的宽度不影响. 刚开始理解错了,..以为怎么放圆使宽度最小.. 这样就可以尽量使每个圆向左 ...
- Object Pascal 过程与函数
过程与函数 过程与函数是实现一定功能的语句块,是程序中的特定功能单元.可以在程序的其他地方被调用,也可以进行递归调用.过程与函数的区别在于过程没有返回值,而函数有返回值. 1.过程与函数的定义 过程与 ...
- The new day of my blog
今天开始了我的博客建造之旅,作为一个ACMer(虽说是弱校的),我也想象其他人一样把自己的题解和心得记录下来,一来可以和大家分享一下,二来也可以留给将来的自己作回顾,希望众大神能够给以指导指导,让我这 ...
- [css] 自适应布局 移动端自适应
一.宽度自适应 三列布局左右固定.中间不固定或者两列布局,左边固定右边不固定 原文链接:http://www.cnblogs.com/2050/archive/2012/07/30/2614852.h ...
- Java Performance - 优化和分析Garbage Collection/垃圾收集
随着硬件的不断提升,Java Heap 越来越大,合理的垃圾收集调优变得愈发重要.下面介绍一些最佳实践: 注意: 下面不涉及 IBM AIX Java. 同时不介绍原理,仅仅是建议以及初始配置/最佳实 ...
- graph-tool文档(一)- 快速开始使用Graph-tool - 1.创建和操纵图
目录: 快速开始使用graph-tool 创建和操纵图 -- 遍历顶点和边 ----- 遍历所有顶点或边 ----- 遍历一个顶点的neighbourhood 名词解释: instante:实例 di ...
- 抛弃vboot不格盘用grub4dos+firadisk安装Ghost版XP到VHD,轻松RAMOS!
http://bbs.wuyou.net/forum.php?mod=viewthread&tid=363198&extra=抛弃vboot不格盘用grub4dos+firadisk安 ...
- Nginx + Tomcat 配置
Windows下Nginx的启动.停止等命令 在Windows下使用Nginx,我们需要掌握一些基本的操作命令,比如:启动.停止Nginx服务,重新载入Nginx等,下面我就进行一些简单的介绍.1.启 ...
- SVN操作手册
目 录 第1章 简介 1 第2章 SVN服务端 2 2.1 安装VisualSVN 2 2.2 VisualSVN服务 3 2.3 版本库 4 2.3.1 创建版本库 ...