poj 3274(sum矩阵+sort)

来源:互联网 发布:淘宝网男士时尚毛线衣 编辑:程序博客网 时间:2024/04/29 05:10
Gold Balanced Lineup
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 11195 Accepted: 3305

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

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 cowsi..j is balanced if each of theK 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 andK.
Lines 2..N+1: Line i+1 contains a single K-bit integer specifying the features present in cowi. 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 37672142

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

Source

USACO 2007 March Gold



这道题目非常有意思。充分考察了对于sum预处理的理解(求0-i的sum). 考察了操作符重载和sort。

1.先将输入转化为矩阵,'1'表示有该特征,’0'表示没有该特征:

cow:    0 1 2 3 4 5 6 7

特征1: 0 1 0 1 0 1 0 0

特征2: 0 1 1 1 1 0 0 1

特征3: 0 1 1 1 0 0 1 0


2.然后预处理出从位置0到位置i的所有特征的总数:

cow:    0 1 2 3 4 5 6 7

特征1: 0 1 1 2 2 3 3 3

特征2: 0 1 2 3 4 4 4 5

特征3: 0 1 2 3 3 3 4 4

那么,我们可以想到,在上面的矩阵中,如果第j列与第i列相比,如果每个特征的差值都相同,那么显然从i 到 j 就是一个满足要求的range。比如2-6,所有特征都差2.那么2-6就是一个符合要求的range。


3.那么我们进一步转化,将每一列都最小化,即都减去各列最小的那个值。这样判断的时候,只需要判断相等即可。

cow:    0 1 2 3 4 5 6 7

特征1: 0 0 0 0 0 0 0 0

特征2: 0 0 1 1 2 1 1 2

特征3: 0 0 1 1 1 0 1 1


4.这样就非常简单啦。

发现第0, 1列是相等的,第2,3,6列是相等的, 第4,7列是相等的

那么我们的任务再转化为取值相等的列里面,列标相差最大的两个值的差,就是最后的答案。

这个例子中,6-2=4 差值最大。就选它了。


总结这类问题的思路:

输入预处理->求sum矩阵->sum矩阵归一化->sort

因为只有sort后,才能更快地查找。


提交记录:

1.Wrong Answer. 切记!排序后遍历,跳出循环后,还要再加一个判断,这样才能保证最后的那个元素也被考虑在内。

  下面的最后一行:

    int result = 0;    int left = c[0].num, right = c[0].num;    for (i = 1; i <= n; i++) {        if (!equal(c[i-1], c[i])) {            if (right-left > result) result = right-left;            left = c[i].num;                right = c[i].num;        }        else right = c[i].num;    }    if (right - left > result) result = right - left;

2.Accepted!