POJ3274
来源:互联网 发布:mac命令行安装jdk 编辑:程序博客网 时间:2024/05/01 12:58
题意大概是:输入N只牛和这些牛最多具有k个特征,接下来输入N行。,每行一个数字,转换成二进制,表示该只牛具有哪些特征。
求找到最大的区间长度使在那个区间内每种特征的总数一样。
用feature【i】【j】表示第i只牛是否具有j特征,是则为1,否则为0。
用sum【i】【j】存储从第一只到第i只牛具有特征j的总数。
题目要求求得
sum[i][1]-sum[j][1]==sum[i][2]-sum[j][2]=......=sum[i][k]-sum[j][k];
使i-j最大;
转化上式得sum[i][2]-sum[i][1]==sum[j][2]-sum[j][1];
sum[i][3]-sum[i][1]==sum[j][3]-sum[j][1];
.
.
.
.
.
.
sum[i][k]-sum[i][1]==sum[j][k]-sum[j][1];
用cal【i】【j】存储sum[i][j]-sum[i][1]的值;
只需找到cal[i][]==cal[j][]中最大的i-j即可。
g++会爆
#include <iostream>#include <cstdio>#include <queue>#include <algorithm>#include <cstring>#include <string>#include <stack>#include <stdlib.h>using namespace std;#define N 100011int feature[N][35];int sum[N][35],cal[N][35];const int mod=3000001;int n,m,maxlen;typedef class Hash{public: int pos; Hash *next; Hash(){next=0;}}Hashtable;Hashtable* hash[mod];bool cmp(int x,int y){ for(int i=1;i<=m;i++) if(cal[x][i]!=cal[y][i])return false; return true;}void Hash(int cur){ int key=0; for(int i=1;i<=m;i++) key+=cal[cur][i]*i; key=abs(key)%mod; if(!hash[key]) { Hashtable *p=new Hashtable; p->pos=cur; hash[key]=p; } else { Hashtable *p=hash[key]; if(cmp(cur,p->pos)) { int len=cur-(p->pos); maxlen=max(maxlen,len); return; } else { while(p->next) { if(cmp(cur,p->next->pos)) { int len=cur-(p->next->pos); maxlen=max(maxlen,len); return; } p=p->next; } Hashtable *temp=new Hashtable; temp->pos=cur; p->next=temp; } } return ;}int main(){ cin>>n>>m; int f; for(int i=0;i<=m;i++) { cal[0][i]=sum[0][i]=0; } memset(hash,0,sizeof(hash)); Hash(0); maxlen=0; for(int i=1;i<=n;i++) { cin>>f; for(int j=1;j<=m;j++) { feature[i][j]=f%2; f=f/2; sum[i][j]=sum[i-1][j]+feature[i][j]; cal[i][j]=sum[i][j]-sum[i][1]; } Hash(i); } cout<<maxlen<<endl; return 0;}
0 0
- POJ3274
- poj3274
- poj3274
- poj3274
- poj3274
- poj3274
- POJ3274
- poj3274
- poj3274 hash
- poj3274 hash
- Gold Balanced Lineup poj3274
- POJ3274 Gold Balanced Lineup
- poj3274 hash数组
- poj3274数位HASH
- poj3274 Gold Balanced Lineup
- [POJ3274]-Gold Balanced Lineup
- [POJ3274] Gold Balanced Lineup
- poj3274(数字hash)
- hibernate 延迟加载(转载)
- 树链剖分+线段树 hdu3966 Aragorn's Story
- Search a 2D Matrix
- mysql5.6中mrr和icp优化简述
- 安卓APP加载HTML5页面解决方案总结
- POJ3274
- 程序员总结:帮助你早些明白一些道理
- TCP协议
- stm32 HardFault_Handler调试及问题查找方法
- 用HTML5 canvas制作简单游戏
- [知其然不知其所以然-12] intel_pstate忘记对cpu区别对待导致的问题
- 树链剖分+线段树 poj3237 Tree
- notes_1(canvas)
- VMware Workstation VT-x禁用问题