poj 3274(sum矩阵+sort)
来源:互联网 发布:淘宝网男士时尚毛线衣 编辑:程序博客网 时间:2024/04/29 05:10
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
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
Sample Input
7 37672142
Sample Output
4
Hint
Source
这道题目非常有意思。充分考察了对于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!
- poj 3274(sum矩阵+sort)
- UVA 108 Maximum Sum (最大子矩阵和) POJ 1050
- poj 2785 4 Values whose Sum is 0 (二分+sort)
- matlab sort矩阵排序
- [POJ 3274]Gold Balanced Lineup[math][sort]
- poj sum
- 矩阵最大 max sum
- sum 矩阵求和 matlab
- (Relax 矩阵快速幂 1.2)POJ 3233 Matrix Power Series(用矩阵加法+矩阵快速幂来求sum= A + A2 + A3 + … + Ak)
- POJ 3761 Bubble Sort
- POJ 3761 Bubble Sort
- POJ 1520 Scramble Sort
- POJ 3761 Bubble Sort
- poj 3761 Bubble Sort
- 矩阵的行列sum() Python
- poj 2479 Maximum sum
- POJ 1844 Sum
- poj 1844 Sum
- linux一些常用命令
- windows的MAX_PATH
- 7.磁盘基本概念
- MySQL中进行树状所有子节点的查询(MYSQL综合)
- poj 3349(重载比较操作符+排序)
- poj 3274(sum矩阵+sort)
- IT外企那点儿事(3):奇怪的面试
- 归并排序
- POJ-2892 Tunnel Warfare 树状数组
- poj 2151(概率dp)
- 黑马程序员—数据库的连接与命令操作
- poj 1840(移动几项到等式另外一边减少循环深度)
- poj 2002(全等三角形计算正方形另外两个点坐标)
- poj 2503(scanf和gets读取字符串)