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 ...
随机推荐
- Android的启动模式(上)
1. 基本介绍 大家平时只要懂一点Android知识的话,都一定会知道,一个应用的组成,往往包含了许多的activity组件,每个activity都应该围绕用户的特定动作进行跳转设计.比如说,一个电话 ...
- Handler知识点详解
Handler是在多线程之间使用的,用于线程之间进行通信. 要想知道为什么需要Handler就首先说明android的主线程和工作线程. 主线程又称为UI线程.正是因为在android中,所有与UI有 ...
- JavaWeb 4 XML
4 XML 1 XML入门 1.1 引入 HTML: 负责网页的结构 CSS: 负责网页的样式(美观) Javascript: 负责在浏 ...
- EMV技术学习和研究(转)
刚开始学习EMV&PBOC,磕磕碰碰,感谢xuture的<EMV技术学习和研究>给了很大帮助,让我少走了很多弯路,也感谢广俊.surge.艾零.小SO.Spinach.龙行天下的帮 ...
- PHP_解析xss攻击、sql注入
/** * PHP解决XSS(跨站脚本攻击)的调用函数 * PHP跨站脚本漏洞补丁,去除XSS(跨站脚本攻击)的函数,把以下代码保存在function.php文件中,在需要防御的页面中include ...
- SciTE 文本编辑器
个人一直使用,强大,轻型基于 Scintilla. http://www.scintilla.org/SciTEDownload.html 解压缩后,能右键文件打开 修改注册表, 使能资源管理器 右键 ...
- php学习注意点
1 多阅读手册和源代码 没什么比阅读手册更值得强调的事了–仅仅通过阅读手册你就可以学习到很多东西,特别是很多有关于字符串和数组的函数.就在这些函数里面包括许多有用的功能,如果你仔细阅读手册,你会经常发 ...
- 在Ubuntu 14 上安装 Nginx-RTMP 流媒体服务器
一:RTMP RTMP流媒体协议是 一套 Adobe 开发的音频视频实时传输协议: 二:Nginx-rtmp nginx-rtmp 是一个基于nginx的 RTMP服务模块,开源,免费 https:/ ...
- dede 调用四级导航
一.修改文件:\include\taglib目录下的channel.lib.php,请将以下代码全部复制替换上述文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...
- 屏幕输出VS文件输出
问题1:我们在编写程序时经常需要数一些数据到屏幕,来查看我们的结果是否正确,虽然直接输出到屏幕,查看起来呢很方便,但当数据量很大时,需要耗费大量的时间.于是我们想到能不能通过输出到文件来减少时间 ...