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 ...
随机推荐
- 简明Vim练级攻略(转载)
前言 今天看到这篇文章,共鸣点非常多.它把Vim使用分为4个级别,目前我自己是熟练运用前面三级的命令,在培养习惯使用第四级.完全就是我这一年来坚持使用Vim的过程.所以不管怎么我要转载这篇文章.翻译自 ...
- JavaWeb学习总结(九)--JDBC入门
一.什么是JDBC JDBC(Java DataBase Connectivity)就是Java数据库连接,说白了就是用Java语言来操作数据库.原来我们操作数据库是在控制台使用SQL语句来操作数据库 ...
- js数组知识
js数组 shift:删除原数组第一项,并返回删除元素的值:如果数组为空则返回undefined var a = [1,2,3,4,5]; var b = a.shift(); //a:[2,3, ...
- JAVA EE 第一阶段项目问题
一: 乱码 原因: 由于同组的其他同学的myeclipse默认的编码方式是GBK,而我的默认的是UTF-8.所以当我使用svn把其他同学提交到组长那里去的代码下载下来的时候,就全乱码了! 解决问题: ...
- x265
1.编译库 https://bitbucket.org/multicoreware/x265/src/tip/build/README.txt?at=default 2.无法定位程序输入点x265_e ...
- Animation用法
测试代码及说明: <!DOCTYPE html> <html lang="en-US"> <head> <meta charset=&qu ...
- Python核心编程-闭包
百度搜了一下闭包的概念:简而言之,闭包的作用就是在外部函数执行完并返回后,闭包使得收机制不会收回函数所占用的资源,因为内部函数的执行需要依赖外函数中的变量.这是对闭包作用的非常直白的描述,不专业也不严 ...
- java 多线程1
进程: 线程: 多线程: 假象:只是CPU在做快速的切换 多线程的好处: 1.解决了一个进程里面可以同时运行多个任务(执行路径) 2.提高资源利用率,而不是效率. 多线程的弊端: 1.降低了一个进程里 ...
- Calendar类中add/set/roll方法的区别
欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...
- 从zepto中学习方法
前言,今天开始学习Zepto源码,这里仅仅几下里面能用到的方法..陆续补充ing... 一,判断类型函数 function type(obj) { return obj == null ? Strin ...