题目链接:点击打开链接

Gold Balanced Lineup

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 16978   Accepted: 4796

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

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

Hint

In the range from cow #3 to cow #6 (of size 4), each feature appears in exactly 2 cows in this range

题目大意:n头牛,有k种特征。给出每种奶牛的ID,他的二进制的1的位数表示他有哪个特征,求奶牛每种特征出现次数相同的连续最长长度。

解释:

思路:看了题解理解了。根据条件:区间每种特征出现次数相同。用sum[i][j]表示从1到i头牛的j特征出现的次数。那么就有:sum[i][0] - sum[j][0] = sum[i][1] - sum[j][1] = ......= sum[i][k-1] - sum[j][k-1]   上式可以改写为: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][Y] = 0  所以只需要求 C[i][] == C[j][] 中j-i的最大值

AC代码:

#include<iostream>
#include<string.h>
#include<vector>
#include<math.h>
using namespace std;
const int N=100010;
const int inf=1<<29;
int n,k,tz[N][40],ms[N][40],sum[N][40],key[N],ans;
vector<int>a[N];// void search(int knum,int id) {
int len=a[knum].size();
for(int j=0; j<len; ++j) {//这种key里的id的C数组的数字是否全部一样
int f=1;
for(int l=0; l<k; ++l)
if(ms[a[knum][j]][l]!=ms[id][l]) {
f=0;
break;
}
if(f) {
ans=max(ans,id-a[knum][j]);
return;
}
}
a[knum].push_back(id);//这种key里所有的id
}
int main() {
int t,i,j;
scanf("%d%d",&n,&k);//得到sum数组
for(int i=1; i<=n; ++i) {
scanf("%d",&t);
for(int j=0; j<k; ++j) {
tz[i][j]=t%2;
t/=2;
}
}
for(i=0; i<N; ++i) a[i].clear();
a[0].push_back(0);
for(i=1; i<=n; ++i) {//得到C数组 并且求得每一头牛的哈希值
for(j=0; j<k; ++j) {
sum[i][j]=sum[i-1][j]+tz[i][j];
ms[i][j]=sum[i][j]-sum[i][0];
key[i]+=ms[i][j];
}
key[i]=abs(key[i])%N;
}
for(i=1; i<=n; ++i) search(key[i],i);//搜
printf("%d",ans);
return 0;
}

以上的代码比较好理解,上面的解释参考了两位神犇的博客:

神犇①点击打开链接                    神犇②点击打开链接

还有一道与该题思路一样的题目:点击打开链接  可以对比理解

POJ3274-Gold Balanced Lineup的更多相关文章

  1. poj3274 Gold Balanced Lineup(HASH)

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

  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. 洛谷 P1360 [USACO07MAR]Gold Balanced Lineup G (前缀和+思维)

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

  7. Gold Balanced Lineup POJ - 3274

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

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

  9. Gold Balanced Lineup(哈希表)

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

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

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

随机推荐

  1. 为什么jdk1.8不支持sql.append,该如何解决

    StringBuilder sql = new StringBuilder("SELECT ID,COMMAND,DESCRIPTION,CONTENT FROM message where ...

  2. 【leetcode刷题笔记】Recover Binary Search Tree

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  3. mci播放mp3

    1MIDI的播放---- 乐器数字化接口(MIDI)是由音乐界的一些大公司(包括生产电子音乐合成器的公司)制订的一项协议,后来被计算机产业所采用并成为多媒体音乐文件的标准格式.MIDI文件一般较小,对 ...

  4. JS通过经纬度计算两个地方的距离

    1 主要原理: Lat1 Lung1 表示A点纬度和经度,Lat2 Lung2 表示B点纬度和经度: a=Lat1 – Lat2 为两点纬度之差  b=Lung1 -Lung2 为两点经度之差: 63 ...

  5. LIBCMTD.lib(wincrt0.obj) : error LNK2019: 无法解析的外部符号 _WinMain@16,该符号在函数 ___tmainCRTStartup 中被引用

    无法解析的外部符号 _WinMain@16,该符号在函数 ___tmainCRTStartup 中被引用 出现原因: 连接程序在负责连接可执行程序时,选择相应的c/c++运行时启动函数.如果设定了/s ...

  6. python2.7系统性能监控psutil模块

    系统环境:Centos7.4,系统自带python2.7.5 登录psutil官网,下载psutil的tar包:psutil-5.4.6.tar.gz,并使用命名sha256sum和官网的包进行核对, ...

  7. TS学习之泛型

    可以使用泛型来创建可重用的组件,一个组件可以支持多种类型的数据 不适用泛型的函数 function myfn(args: number): number { return args; } functi ...

  8. 将List中部分字段转换为DataTable中

    由于原来方法导出数据量比较大 的时候,出现卡顿现象:搜索简单改造:(下面方法借助NPIO) /// <summary> /// 将List中原文和译文转换为Datatable /// &l ...

  9. javaScript之深度理解原型链

    经过多次的翻阅书籍终于对原型链在实际代码中的应用有了新的认识,但是不知道是否有错误的地方,还请大神多多指教. 构造函数.原型和实例的关系:每个构造函数都有一个原型对象funName.prototype ...

  10. C/C++常用数学函数

    math.h/cmath(C++)数学函数库 1 三角函数    double sin (double);    double cos (double);    double tan (double) ...