POJ 3274:Gold Balanced Lineup 做了两个小时的哈希
来源:互联网 发布:js 判断数组 编辑:程序博客网 时间:2024/06/12 14:28
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,没有这个特征是为0。然后把每头牛的01串变成一个十进制的数。问找到一个最长的区间,满足这个区间内每一个特征含有的总数是相等的。
这个时候发现最大区间问题其中的一个思路就是哈希啊,之前求51nod 1393:0和1相等串这个也是哈希。
然后这道题就是考虑各种情况吧,自己一头牛也可能是最大的区间。
代码:
#include <iostream>#include <algorithm>#include <cmath>#include <vector>#include <string>#include <cstring>#include <map>#pragma warning(disable:4996)using namespace std;int n, k;int value[100005];int val[100005][31];int searc[100005];//看是否有冲突vector<int>dic[100005];int check(int i, int key){int j, h, answer = 0;for (j = 0; j < dic[searc[key]].size(); j++){for (h = 2; h <= k; h++){if ((val[dic[searc[key]][j]][h] - val[i][h]) != (val[dic[searc[key]][j]][h - 1] - val[i][h - 1]))break;}if (h == k + 1){if (i - dic[searc[key]][j] > answer){answer = i - dic[searc[key]][j];}}}if (answer == 0){dic[searc[key]].push_back(i);}return answer;}int main(){//freopen("i.txt", "r", stdin);//freopen("o.txt", "w", stdout);int i, ans, fea, key, dic_num, temp, temp2;scanf("%d%d", &n, &k);ans = 0;dic_num = 0;memset(searc,0,sizeof(searc));for (i = 0; i < 31; i++){val[0][i] = 0;}for (i = 1; i <= n; i++){scanf("%d", &value[i]);key = 0;fea = 1;temp2 = value[i];while (fea <= k){val[i][fea] = (temp2 & 1) + val[i - 1][fea];if (fea != 1) key += abs(val[i][fea] - val[i][fea - 1]);fea++;temp2 = temp2 >> 1;}if (value[i] == 0 || value[i] == pow(2.0, k) - 1){ans = max(ans, 1);}if (key == 0){ans = max(ans, i);}if (searc[key] == 0){searc[key] = ++dic_num;dic[dic_num].push_back(i);}else{temp = check(i, key);if (temp > ans){ans = temp;}}}cout << ans << endl;return 0;}
- 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
- 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
- HDU4607(最大直径 树DP)
- 工作中遇见的问题总结
- SQL中GROUP BY、HAVING操作
- hdoj Minimum Transport Cost 1385 (floyd++路径输出)
- mfc ocx 控件开发需要注意的问题
- POJ 3274:Gold Balanced Lineup 做了两个小时的哈希
- iOS轻量级多线程实现
- Java基础文章列表
- 单链表反转,要求空间复杂度O(1)
- 动态载入DLL所需要的三个函数详解(LoadLibrary,GetProcAddress,FreeLibrary)
- 连通性问题
- noip 10.27模拟
- 【c++面向对象编程】工资管理系统
- LightOJ - 1005 Rooks(统计)