POJ 3274:Gold Balanced Lineup 做了两个小时的哈希
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 13540 | Accepted: 3941 |
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 3
7
6
7
2
1
4
2
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
Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10924 Accepted: 3244 ...
- poj 3274 Gold Balanced Lineup(哈希 )
题目:http://poj.org/problem?id=3274 #include <iostream> #include<cstdio> #include<cstri ...
- POJ 3274 Gold Balanced Lineup(哈希)
http://poj.org/problem?id=3274 题意 :农夫约翰的n(1 <= N <= 100000)头奶牛,有很多相同之处,约翰已经将每一头奶牛的不同之处,归纳成了K种特 ...
- 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 ...
- 哈希-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 ...
- 1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列
1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 510 S ...
- 洛谷 P1360 [USACO07MAR]Gold Balanced Lineup G (前缀和+思维)
P1360 [USACO07MAR]Gold Balanced Lineup G (前缀和+思维) 前言 题目链接 本题作为一道Stl练习题来说,还是非常不错的,解决的思维比较巧妙 算是一道不错的题 ...
- 【POJ】3264 Balanced Lineup ——线段树 区间最值
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34140 Accepted: 16044 ...
- POJ 题目3264 Balanced Lineup(RMQ)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 39046 Accepted: 18291 ...
随机推荐
- C++中的内联函数
(1)定义.函数声明和定义时,在返回值类型前加inline关键字. (2)作用.避免调用时的额外开销. (3)使用情况.函数体很小,且频繁调用. (4)本质.通过牺牲代码段空间,来提高程序运行的时间效 ...
- 操作系统OS - 线程中的join()为什么叫join
1. 问题:很好奇为什么叫Join? 参考: https://blog.csdn.net/frankarmstrong/article/details/55504161 https://stackov ...
- ubuntu 更改pip默认源
mkdir ~/.pip vim ~/.pip/pip.conf [global] timeout = 6000 https://pypi.tuna.tsinghua.edu.cn/simple 保存 ...
- nginx 缓存
浏览器缓存与nginx缓存 浏览器缓存 优点:使用有效缓存时,没有网络消耗,速度快:即使有网络消耗,但对失效缓存使用304响应做到网络消耗最小化 缺点:仅提升一个用户的体验 nginx 缓存 优点:提 ...
- js数组和java数组的区别
1,js数组可以自动扩容,不会出现数组越界的情况 2,js数组中可以存放任意数据类型 3,java数组一旦定义长度,不可以更改 4,java数组中的数据类型必须一致
- 3_02_MSSQL课程_Ado.Net_连接池_连接字符串
连接池技术:是一种对象池技术. 连接对象频繁的开启和关闭操作. innerConnection 先从池子里面拿,如果没有创建新的!!连接池有大小,最大/最小. 提高了连接对象的重用. Asp.ne ...
- C语言程序编译
原来GCC的含义是GNU C Compiler,当初知识编译C语言,而现在GCC不知编译C语言,除此之外它还支持编译Ada.C++.Java.Object C.Pascal.COBOL.等等许多语言, ...
- leetcode 390. 消除游戏
{20-01-29 19:22} class Solution { public int lastRemaining(int n) { return help(n); } public static ...
- Periodic-table
1. Periodic table 1.1 元素的排列 1.2 表中的行与列 1.3 元素区块 1.4 周期表中的一些趋势 1.5 元素周期律的本质 1.6 电子排布 2. 更多相关链接 2.1 维基 ...
- 498. (leetcode)对角线遍历
498. 对角线遍历 根据题目的图像看,主要有两种走法,第一种是向右上(顺时针方向),第二种是向左下(逆时针)走 我们设 x ,y初始为0,分别对应横纵坐标 现在分析右上(0,2) 为例:(注意右上的 ...