Gold Balanced Lineup - poj 3274 (hash)
这题,看到别人的解题报告做出来的,分析:
大概意思就是:
数组sum[i][j]表示从第1到第i头cow属性j的出现次数。
所以题目要求等价为:
求满足
sum[i][0]-sum[j][0]=sum[i][1]-sum[j][1]=.....=sum[i][k-1]-sum[j][k-1] (j<i)
中最大的i-j
将上式变换可得到
sum[i][1]-sum[i][0] = sum[j][1]-sum[j][0]
sum[i][2]-sum[i][0] = sum[j][2]-sum[j][0]
......
sum[i][k-1]-sum[i][0] = sum[j][k-1]-sum[j][0]
令C[i][y]=sum[i][y]-sum[i][0] (0<y<k)
初始条件C[0][0~k-1]=0
所以只需求满足C[i][]==C[j][] 中最大的i-j,其中0<=j<i<=n。
C[i][]==C[j][] 即二维数组C[][]第i行与第j行对应列的值相等,
那么原题就转化为求C数组中 相等且相隔最远的两行的距离i-j
大概意思就是:
数组sum[i][j]表示从第1到第i头cow属性j的出现次数。
所以题目要求等价为:
求满足
sum[i][0]-sum[j][0]=sum[i][1]-sum[j][1]=.....=sum[i][k-1]-sum[j][k-1] (j<i)
中最大的i-j
将上式变换可得到
sum[i][1]-sum[i][0] = sum[j][1]-sum[j][0]
sum[i][2]-sum[i][0] = sum[j][2]-sum[j][0]
......
sum[i][k-1]-sum[i][0] = sum[j][k-1]-sum[j][0]
令C[i][y]=sum[i][y]-sum[i][0] (0<y<k)
初始条件C[0][0~k-1]=0
所以只需求满足C[i][]==C[j][] 中最大的i-j,其中0<=j<i<=n。
C[i][]==C[j][] 即二维数组C[][]第i行与第j行对应列的值相等,
那么原题就转化为求C数组中 相等且相隔最远的两行的距离i-j
以样例为例
7 3
7
6
7
2
1
4
2
先把7个十进制特征数转换为二进制,并逆序存放到特征数组feature[ ][ ],得到:
7 ->1 1 1
6 ->0 1 1
7 ->1 1 1
2 ->0 1 0
1 ->1 0 0
4 ->0 0 1
2 ->0 1 0
(行数为cow编号,自上而下从1开始;列数为特征编号,自左到右从0开始)
再求sum数组,逐行累加得,sum数组为
1 1 1
1 2 2
2 3 3
2 4 3
3 4 3
3 4 4
3 5 4
再利用C[i][y]=sum[i][y]-sum[i][0]求C数组,即所有列都减去第一列
注意C数组有第0行,为全0
0 0 0 -> 第0行
0 0 0
0 1 1 <------
0 1 1
0 2 1
0 1 0
0 1 1 <-------
0 2 1
显然第2行与第6行相等,均为011,且距离最远,距离为6-2=4,这就是所求。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int num[][];
int N,K;
struct hash{
int ind;
hash* next;
};
hash hashtable[];
int gethash(int id){
num[id][]=;
for(int i=;i<K;i++){
num[id][]+=num[id][i]*i;
}
num[id][]=(num[id][]&0x7fffffff)%;
return num[id][];
}
int isEqual(int id1,int id2){
int flag=;
for(int i=;i<K;i++){
if(num[id1][i]!=num[id2][i]){
flag=;
}
}
return flag;
}
int main(){
scanf("%d %d",&N,&K);
for(int j=;j<K;j++){
num[][j]=;
}
memset(hashtable,,sizeof(hash)*);
for(int i=;i<=N;i++){
int t;
scanf("%d",&t);
for(int j=;j<K;j++){
num[i][j]=t%;
t=t>>;
num[i][j]+=num[i-][j];
} } int result=;
for(int i=;i<=N;i++){
for(int j=;j<K;j++){
num[i][j]-=num[i][];
}
int h=gethash(i);
hash *t=(hash*)malloc(sizeof(hash));
t->next=hashtable[h].next;
t->ind=i;
hashtable[h].next=t;
while(t!=NULL){
if(isEqual(t->ind,i)){
int tmp=i-t->ind;
result=result>tmp?result:tmp;
}
t=t->next;
}
}
printf("%d\n",result);
return ;
}
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 13200 | Accepted: 3866 |
Description
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
Sample Input
7 3
7
6
7
2
1
4
2
Sample Output
4
Hint
Gold Balanced Lineup - poj 3274 (hash)的更多相关文章
- 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
Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10924 Accepted: 3244 ...
- 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 3274 Gold Balanced Lineup(哈希 )
题目:http://poj.org/problem?id=3274 #include <iostream> #include<cstdio> #include<cstri ...
- POJ 3274 Gold Balanced Lineup(哈希)
http://poj.org/problem?id=3274 题意 :农夫约翰的n(1 <= N <= 100000)头奶牛,有很多相同之处,约翰已经将每一头奶牛的不同之处,归纳成了K种特 ...
- 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 ...
随机推荐
- 【译】PHP中的Session及其一些安全措施
有一点我们必须承认,大多数web应用程序都离不开session的使用.这篇文章将会结合php以及http协议来分析如何建立一个安全的会话管理机制.我们先简单的了解一些http的知识,从而理解该协议的无 ...
- jquery给input标签添加data-options属性
//原生JS实现document.getElementById('startPrice').setAttribute("data-options", "required: ...
- C++中的模板学习
https://www.cnblogs.com/eleclsc/p/5918114.html
- python抓取360百科踩过的坑!
学习python一周,学着写了一个爬虫,用来抓取360百科的词条,在这个过程中.因为一个小小的修改,程序出现一些问题,又花了几天时间研究,问了各路高手,都没解决,终于还是自己攻克了,事实上就是对lis ...
- Servlet执行时一般实现哪几个方法?
public void init(ServletConfig config) public ServletConfig getServletConfig() public String getServ ...
- vim使用指北 ---- Multiple Windows in Vim
多窗口打开多个文件 vim -o file1 file2 ... ---- 默认上下分割窗口 vim -0n file1 file2 ... ---- vim默认会上下等分n个窗口 分割窗口 :[v] ...
- ./test.sh . ./test.sh source ./test.sh的区别
背景 今天写几个shell脚本,使用一个公共的config.sh,但是export出来的东西在另外的*.sh中不能直接用,这让我很惆怅,印象中就是可以export出来给别的shell用啊,只要不是下一 ...
- 有哪些通俗易懂的例子可以解释 IaaS、PaaS、SaaS 的区别?
有哪些通俗易懂的例子可以解释 IaaS.PaaS.SaaS 的区别? S 软件 P 中间件 I 基础设施
- windowsclient开发--依据可下载url另存为文件(微信windowsclient这样做的)
能够我的blog的标题会让你误解,那么好,没图说了xx: 比方微信windowsclient发送了一张图片,我们能够预览这张图片,还能够保存到本地: 那么windows程序是怎样下载这张图片的呢? 是 ...
- Alfresco 5.0.d 企业文档管理系统
Thanks for downloading Alfresco Community Edition. Your download should begin in three seconds. If i ...