poj 3274 Gold Balanced Lineup
来源:互联网 发布:尹美莱 知乎 编辑:程序博客网 时间:2024/05/16 13:45
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
说实在话对于英文渣渣的我读题真的很难。
下面是题目大意,对提供者表示感谢。
代码操作如下:
1、先将十进制数转换成二进制数记录保存。
2、然后逐行累加,得出到某只牛时某种特征出现了几次。
3、每行减去第一个数,得出一个序列,若两头牛之间是平衡区间的话,各个特征的增长数是相等的,及减掉第一个数得出来的序列是相等的。
4、寻找距离最远的两个相等的序列,得出答案。
我们可以由样例举例:
转换成二进制特征值为:
数字 特征值 第几头牛
7 1 1 1 1
6 0 1 1 2
7 1 1 1 3
2 0 1 0 4
1 1 0 0 5
4 0 0 1 6
2 0 1 0 7
按行累加得:
1 1 1
1 2 2
2 3 3
2 4 3
3 4 3
3 4 4
3 5 4
都减去第一列得:
0 0 0
0 1 1
0 1 1
0 2 1
0 1 0
0 1 1
0 2 1
所以说 最大区间是 6-2 = 4.有一种特殊情况就是当到了某一行出现全都是零的情况,例如:
0 1 2 1 1 0 2
0 0 0 2 1 1 1
0 0 0 0 0 0 0
我们需要在所有序列之前加一行0以方便比较。
*/<pre name="code" class="cpp">#include <string.h>#include <stdio.h>#include <iostream>#include <algorithm>#define M 100005#define N 35using namespace std;int num[M];struct node{ int min1; int num1[N];}bin[M];int n,m;int cmp2 ( node x, node y )//Qsort的比较函数{ for( int i = 0; i < m; i++ ) { if ( x.num1[i] != y.num1[i] ) { return x.num1[i] < y.num1[i]; } } return x.min1 < y.min1;}void Binary(){ int i, j; int BIN_size = m; for ( i = 0; i <= n ; i++ ) { j = 0; while ( num[i] != 0 ) { bin[i].num1[j++] = num[i]%2; num[i] = num[i]/2; } while ( j <= BIN_size ) { bin[i].num1[j++] = 0; } }}void Accu (){ int i, j; for ( i = 1 ; i <= n ; i++ ) { for ( j = 0; j < m ; j++ ) { bin[i].num1[j] = bin[i-1].num1[j] + bin[i].num1[j]; } }}void Sub (){ int i, j; for ( i = 0 ; i <= n; i++ ) { for ( j = 1; j < m ; j++ ) { bin[i].num1[j] = bin[i].num1[j] - bin[i].num1[0]; } bin[i].num1[0] = 0; }}int compace ( int j ,int k){ for ( int i = 0;i < m; i++ ) { if( bin[j].num1[i] != bin[j-k].num1[i] ) return 0; } return 1;}int main(){ int i, j; while ( ~scanf ( "%d %d", &n, &m ) ) { num[0] = 0; bin[0].min1 = 0; for ( i = 1; i <= n ; i++ ) { scanf ( "%d", &num[i] ); bin[i].min1 = i; } /*Transfer number into a binary*/ Binary (); /*Accumulation of number*/ Accu (); /*The data were subtracted*/ Sub (); /*The test data*/// for ( i = 0;i <= n ; i++ )// {// for ( j = 0; j < m ; j++ )// {// printf("%d ",bin[i].num1[j]);// }// printf("\n");// } /*Order sort*/ sort(bin,bin+n+1,cmp2); int max1 = 0; j = 1; for ( i = 1;i <= n; i++ ) { if ( compace(i, j) ) { if ( max1 < bin[i].min1 - bin[i-j].min1 ) { max1 = bin[i].min1 - bin[i-j].min1; } i = i-1; j = j+1; } else { j = 1; } } printf ( "%d\n", max1 ); }}
- POJ 3274 Gold Balanced Lineup
- POJ 3274 Gold Balanced Lineup
- POJ 3274 Gold Balanced Lineup
- POJ 3274 Gold Balanced Lineup
- POJ 3274 Gold Balanced Lineup
- poj 3274 Gold Balanced Lineup
- POJ 3274 Gold Balanced Lineup
- POJ 3274 Gold Balanced Lineup
- poj 3274 Gold Balanced Lineup
- poj 3274 Gold Balanced Lineup
- Gold Balanced Lineup(POJ--3274
- POJ 3274 Gold Balanced Lineup
- POJ-3274 Gold Balanced Lineup
- poj 3274 Gold Balanced Lineup
- POJ 3274 Gold Balanced Lineup
- poj 3274 Gold Balanced Lineup
- POJ-3274-Gold Balanced Lineup
- poj 3274 Gold Balanced Lineup
- JAVA中Action层, Service层 ,modle层 和 Dao层的功能区分
- HDOJ 4135 Co-prime
- 又来中山了。。
- 安装egit
- andorid优化之图片的缩放
- poj 3274 Gold Balanced Lineup
- android动画基础
- CKEditor代码高亮显示插件Code Snippet安装及使用方法
- ubuntu14.04下SSH免密码登陆相关问题及解决办法
- zzuli 1907: 小火山的宝藏收益(邻接表加dfs)
- openGL 纹理使用
- 【基础-java-BIO流】BIO(1)常用IO流引索
- idea 安装 activate-power-mode
- poj2441 状压dp