[POJ3274]-Gold Balanced Lineup
来源:互联网 发布:s曲线加速算法 博客 编辑:程序博客网 时间:2024/05/21 17:43
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 37672142
Sample Output
4
Hint
#include <cstdio>#include <iostream>#include <cmath>#include <cstring>#include <string>#include <algorithm>using namespace std;const int maxn = 1000001;const int prime = 1000003;const int INF = -1;struct node{int b[31];//存储每种特性}cowb[maxn],cnt[maxn];int n,k;int HASH[prime];int hashcode(const int *v,int m){int p=0;for(int i = 1;i <= m;++ i)p = ((p<<2)+(v[i]>>4))^(v[i]<<10);p=p%prime;p = p<0? (p+prime):p;return p;}void debug_print(){for(int j = 1;j <= k;++ j){printf("%d ",cnt[i].b[j]);}cout << endl;}int main(){freopen("data3274.in","r",stdin);while(~scanf("%d%d",&n,&k)){memset(cowb,0,n);memset(cnt,0,n);memset(HASH,INF,prime);HASH[hashcode(cnt[0].b,k)]=0;//初始化int res=0;for(int i = 1;i <= n;++ i){int cow;scanf("%d",&cow);for(int j = 1;j <= k;++ j){int b = cow%2;cowb[i].b[j]=b+cowb[i-1].b[j];cnt[i].b[j] = cowb[i].b[j]-cowb[i].b[1];cow/=2;}// debug_print();int p=hashcode(cnt[i].b,k);//每次获得该cowID处理后的hahs值while(HASH[p]!=-1){bool flag=true;//判断每个特性是否相同for(int j = 1;j <= k;++ j)if(cnt[i].b[j]!=cnt[HASH[p]].b[j]){flag=false;//如果有一个特性值不同就为falsebreak;}if(flag){res=max(res,i-HASH[p]);//若果找打了就把res更新break;}//printf("res = %d\n",res);p++;}if(HASH[p]==-1)//没有被赋值的话HASH[p]=i;//将其赋值为i cowID}printf("%d\n",res);}return 0;}
- Gold Balanced Lineup poj3274
- POJ3274 Gold Balanced Lineup
- poj3274 Gold Balanced Lineup
- [POJ3274]-Gold Balanced Lineup
- [POJ3274] Gold Balanced Lineup
- poj3274--Gold Balanced Lineup(hash)
- POJ3274《Gold Balanced Lineup》方法:哈希
- POJ3274----Gold Balanced Lineup(黄金平衡)
- POJ3274 -Gold Balanced Lineup- HASH+前缀和
- poj3274——Gold Balanced Lineup
- POJ3274 Gold Balanced Lineup 数组的哈希
- PKU3274 Gold Balanced Lineup
- POJ Gold Balanced Lineup
- 哈希-Gold Balanced Lineup
- Gold Balanced Lineup ( hash )
- Gold Balanced Lineup
- Gold Balanced Lineup POJ
- POJ 3274 Gold Balanced Lineup
- thinking in java test3.11练习(11)(12)(13)
- Selenium
- 《WINDOWSPE权威指南》学习笔记(三)- 导入表相关机制
- (复习)图论--最短路--SPFA算法
- mongo shell 之文档 分页&排序
- [POJ3274]-Gold Balanced Lineup
- mongo shell 之文档 索引
- sikulix
- HDU-2188-悼念512汶川大地震遇难同胞——选拔志愿者(巴什博弈)
- LBP 源码分析
- parent\manager\manager-pojo\target\classes\META-INF\MANIFEST.MF (系统找不到指定的路径。)
- mongo shell 之索引 -- 2D索引,全文索引
- 对加速度二次积分求位移
- win10下安装theano和实现GPU加速