Gold Balanced Lineup

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 3   Accepted Submission(s) : 3
Problem 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 Kdifferent 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.
 
Sample Input
7 3
7
6
7
2
1
4
2
 
Sample Output
4
 
Source
PKU
 
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <stdlib.h>
  6. #define maxn 100010
  7. #define seed 999983
  8. using namespace std;
  9.  
  10. int n,k;
  11. int feature[maxn][],sum[maxn][],c[maxn][],maxlen=;
  12.  
  13. struct Hash
  14. {
  15. int id,next;
  16. } h[];
  17.  
  18. bool compair(int a,int b)
  19. {
  20. int i;
  21. for(i=; i<k; i++)
  22. {
  23. if(c[a][i]!=c[b][i])
  24. return false;
  25. }
  26. return true;
  27. }
  28.  
  29. int count_key(int index)
  30. {
  31. int i,key=;
  32. //Hash公式
  33. for(i=; i<k; i++)
  34. key=((key<<)+(c[index][i]>>))^(c[index][i]<<);
  35. key=key%seed;
  36. if(key<)
  37. key=key+seed;
  38. return key;
  39. }
  40.  
  41. //int count_key(int index)
  42. //{
  43. // int i,key=0;
  44. // for(i=0; i<k; i++)
  45. // {
  46. // key+=abs(c[index][i]);
  47. // key%=seed;
  48. // }
  49. // return key;
  50. //}
  51.  
  52. void InsertAndCheck(int index)
  53. {
  54. int i,key,last=;
  55. key=count_key(index);
  56. // cout<<"key="<<key<<endl;
  57. // cout<<key<<endl;
  58. if(h[key].id==-)
  59. {
  60. h[key].id=index;
  61. }
  62. else
  63. {
  64. bool found=false;
  65. for(i=key; i!=-; i=h[i].next)
  66. {
  67. // cout<<"run "<<index<<' '<<i<<endl;
  68. last=i;
  69. if(!found&&compair(index,h[i].id))
  70. {
  71. if(index-h[i].id>maxlen)
  72. {
  73. maxlen=abs(h[i].id-index);
  74. found=true;
  75. }
  76. }
  77. }
  78. for(i=last; h[i].id!=-; i=(i+)%seed);
  79. h[last].next=i;
  80. h[i].id=index;
  81. }
  82. }
  83.  
  84. int main()
  85. {
  86. // freopen("in.txt","r",stdin);
  87. memset(h,-,sizeof(h));
  88. scanf("%d%d",&n,&k);
  89. int i,j,tmp;
  90. n++;
  91. for(i=; i<maxn; i++)
  92. h[i].next=-;
  93. InsertAndCheck();
  94. for(i=; i<=n; i++)
  95. {
  96. scanf("%d",&tmp);
  97. for(j=; j<k; j++)
  98. {
  99. feature[i][j]=tmp%;
  100. sum[i][j]=sum[i-][j]+feature[i][j];
  101. c[i][j]=sum[i][j]-sum[i][];
  102. tmp>>=;
  103. }
  104. InsertAndCheck(i);
  105. }
  106. //// for(i=0;i<=n;i++)
  107. //// {
  108. //// for(j=0;j<k;j++)
  109. //// cout<<c[i][j]<<' ';
  110. //// cout<<endl;
  111. //// }
  112. // for(i=0;i<6;i++)
  113. // cout<<h[i].id<<' '<<h[i].next<<endl;
  114. printf("%d\n",maxlen);
  115. return ;
  116. }

三部曲一(数据结构)-1022-Gold Balanced Lineup的更多相关文章

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

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

  2. POJ 3274 Gold Balanced Lineup

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

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

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

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

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

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

  6. Gold Balanced Lineup POJ - 3274

    Description Farmer John's N cows (1 ≤ N ≤ 100,000) share many similarities. In fact, FJ has been abl ...

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

  8. Gold Balanced Lineup(哈希表)

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

  9. bzoj 1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列——map+hash+转换

    Description N(1<=N<=100000)头牛,一共K(1<=K<=30)种特色, 每头牛有多种特色,用二进制01表示它的特色ID.比如特色ID为13(1101), ...

  10. poj3274 Gold Balanced Lineup(HASH)

    Description Farmer John's N cows (1 ≤ N ≤ 100,000) share many similarities. In fact, FJ has been abl ...

随机推荐

  1. Wow! Such Sequence!(线段树4893)

    Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...

  2. Doctrine2 SQL语句

    $q = Doctrine_Query::create() ->update('WebusersTable q') ->set('q.login_name','?','John') ) - ...

  3. Spark 1.1.0 编译(为了支持hbase 0.98.6)

    为了支持hbase0.98.6,需要重新编译spark 1. 下载spark 1.1.0源代码,以及 scala-2.10.4的bin包. 将环境变量 SCALA_HOME 设置为 scala-2.1 ...

  4. IntelliJ IDEA14 配置 SVN

    最新升级IDEA13到14版本,升级后发现IDEA中SVN无法正常使用,但文件夹下能够正常使用. 并且报错:svn: E204899: Cannot run program "svn&quo ...

  5. javscript 中的术语和俚语

    语言中俚语和方言.在JavaScript中也有一些俚语或者说是术语,看似奇淫巧技,还是有一些用处,有三种语言组件可以来构造术语:强转.逻辑运算符和位变换. 1.强转:在javascript和大部分的语 ...

  6. [问题2014S12] 解答

    [问题2014S12]  解答 先证明一个简单的引理. 引理  设 \(B\) 为 \(n\) 阶半正定 Hermite 阵, \(\alpha\) 为 \(n\) 维复列向量, 若 \(\overl ...

  7. 第十四天 jni 的使用

    1. ndk 环境 2.jni hello 程序. 3.javah 生成头文件. 4.java 和c 之间数据传递. 5.C语言回调java 代码 通过反射.得到字节码,得到方法...

  8. 在mysql数据库原有字段后增加新内容

    update table set user=concat(user,$user) where xx=xxx; [注释]这个语法要求原来的字段值不能为null(可以为空字符''):

  9. Hibernate各种主键生成策略与配置详解

    出自:http://www.cnblogs.com/kakafra/archive/2012/09/16/2687569.html 1.assigned 主键由外部程序负责生成,在 save() 之前 ...

  10. java关于时间

    import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * Created b ...