Gold Balanced Lineup POJ - 3274
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
Source
题解:
其实这个题是想到怎么hash比较难。
假设我们去统计这样一组数据
1 0 1 0
0 1 0 1
求前缀和是
0 0 0 0
1 0 1 0
1 1 1 1
观察这组数据,我们可以发现每个数据减去第一位就会得到相同的
- #include <cstdio>
- #include<cstring>
- #include <queue>
- #include <vector>
- #include <iostream>
- using namespace std;
- const int MAXN=1e5+10;
- int sum[MAXN][40];
- vector<int>Hash[MAXN];
- int n,k;
- const int pri=100005;
- int abs(int x)
- {
- if(x<0) return -x;
- return x;
- }
- queue<int>st;
- void reslove(int x,int i)
- {
- while(x)
- {
- int z=x%2;
- st.push(z);
- x/=2;
- }
- while(st.size()<k)
- {
- st.push(0);
- }
- int num=0;
- while(!st.empty())
- {
- if(st.front()==1)
- {
- sum[i][num]=sum[i-1][num]+1;
- } else{
- sum[i][num]=sum[i-1][num];
- }
- num++;
- st.pop();
- }
- }
- inline bool scan_d(int &num)//输入挂
- {
- char in;bool IsN=false;
- in=getchar();
- if(in==EOF) return false;
- while(in!='-'&&(in<'0'||in>'9')) in=getchar();
- if(in=='-'){ IsN=true;num=0;}
- else num=in-'0';
- while(in=getchar(),in>='0'&&in<='9'){
- num*=10,num+=in-'0';
- }
- if(IsN) num=-num;
- return true;
- }
- int main()
- {
- scanf("%d%d",&n,&k);
- int x;
- memset(sum,0, sizeof(sum));
- for (int i = 1; i <=n ; ++i) {
- scan_d(x);
- reslove(x,i);
- }
- for (int i = 0; i <=n ; ++i) {
- int key=0;
- for (int j = 1; j <k ; ++j) {
- sum[i][j]-=sum[i][0];
- key+=sum[i][j]*j;
- }
- key=abs(key)%pri;
- Hash[key].push_back(i);
- }
- int MAX=0;
- for (int i = 0; i <100005 ; ++i) {
- if(Hash[i].size()>1)
- {
- for (int j = 0; j <Hash[i].size()-1 ; ++j) {
- for (int l = j+1; l <Hash[i].size() ; ++l) {
- int flag=0;
- for (int m = 1; m <k ; ++m) {
- if(sum[Hash[i][j]][m]!=sum[Hash[i][l]][m])
- {
- flag=1;
- break;
- }
- }
- if(flag==0)
- {
- MAX=max(MAX,abs(Hash[i][j]-Hash[i][l]));
- }
- }
- }
- }
- }
- printf("%d\n",MAX);
- return 0;
- }
- /*
- 7 3
- 7
- 6
- 7
- 2
- 1
- 4
- 2
- -----------------------
- 11 5
- 30
- 28
- 24
- 16
- 1
- 3
- 7
- 15
- 16
- 24
- 31
- ------------
- 6 30
- 123456789
- 234567890
- 345678901
- 456789012
- 567890123
- 678901234
- ------------
- 250 3
- 6
- 3
- 1
- 3
- 6
- 7
- 0
- 7
- 6
- 0
- 1
- 3
- 3
- 4
- 3
- 3
- 2
- 2
- 4
- 7
- 3
- 2
- 3
- 0
- 0
- 6
- 2
- 1
- 7
- 2
- 3
- 3
- 5
- 2
- 5
- 2
- 2
- 5
- 0
- 2
- 4
- 5
- 5
- 1
- 4
- 4
- 6
- 3
- 5
- 7
- 7
- 5
- 7
- 7
- 4
- 0
- 0
- 7
- 1
- 3
- 4
- 7
- 2
- 3
- 7
- 5
- 2
- 6
- 2
- 3
- 2
- 0
- 3
- 2
- 4
- 6
- 1
- 1
- 2
- 7
- 1
- 6
- 0
- 5
- 3
- 4
- 6
- 1
- 7
- 5
- 7
- 4
- 2
- 2
- 7
- 1
- 7
- 0
- 7
- 6
- 2
- 3
- 0
- 3
- 5
- 5
- 3
- 1
- 7
- 1
- 7
- 6
- 5
- 3
- 1
- 5
- 6
- 3
- 4
- 0
- 1
- 1
- 2
- 7
- 6
- 1
- 2
- 4
- 1
- 3
- 0
- 4
- 6
- 2
- 6
- 6
- 7
- 6
- 0
- 1
- 7
- 1
- 7
- 5
- 7
- 7
- 3
- 7
- 7
- 5
- 0
- 2
- 6
- 4
- 1
- 3
- 1
- 3
- 7
- 4
- 0
- 6
- 1
- 1
- 4
- 1
- 6
- 7
- 2
- 5
- 4
- 7
- 1
- 7
- 4
- 2
- 2
- 3
- 4
- 0
- 1
- 1
- 0
- 1
- 6
- 1
- 7
- 4
- 6
- 3
- 3
- 7
- 7
- 5
- 3
- 6
- 5
- 1
- 4
- 4
- 1
- 3
- 6
- 4
- 4
- 2
- 0
- 4
- 3
- 7
- 7
- 7
- 3
- 6
- 7
- 3
- 1
- 0
- 2
- 2
- 6
- 1
- 6
- 4
- 7
- 4
- 4
- 6
- 5
- 6
- 5
- 6
- 7
- 4
- 3
- 7
- 1
- 5
- 4
- 0
- 2
- 7
- 1
- 7
- 4
- 4
- 7
- 2
- 2
- 4
- ------------
- 1 3
- 0
- =============================================
- 下面是答案:
- 4
- --------------
- 8
- ------------
- 0
- ------------
- 205
- ----------
- 1
- */
Gold Balanced Lineup POJ - 3274的更多相关文章
- Gold Balanced Lineup - poj 3274 (hash)
这题,看到别人的解题报告做出来的,分析: 大概意思就是: 数组sum[i][j]表示从第1到第i头cow属性j的出现次数. 所以题目要求等价为: 求满足 sum[i][0]-sum[j][0]=sum ...
- POJ 3274 Gold Balanced Lineup
Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10924 Accepted: 3244 ...
- POJ 3274:Gold Balanced Lineup 做了两个小时的哈希
Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13540 Accepted: ...
- 哈希-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 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 ...
随机推荐
- codevs原创抄袭题 5960 信使
题目描述 Description •战争时期,前线有n个哨所,每个哨所可能会与其他若干个哨所之间有通信联系.信使负责在哨所之间传递信息,当然,这是要花费一定时间的(以天为单位).指挥部设在第一个哨所. ...
- 2602 最短路径问题Dihstra算法
题目描述 Description 平面上有n个点(n<=100),每个点的坐标均在-10000~10000之间.其中的一些点之间有连线.若有连线,则表示可从一个点到达另一个点,即两点间有通路,通 ...
- spring笔记2-注解
一.属性与成员变量的区别: 属性:对外暴露的,getxxx/setxxx称为属性; 成员变量:private String name称为成员变量或字段 二.applicationContext.xml ...
- 解决javascript四舍五入不准确
function roundFixed(num, fixed) { var pos = num.toString().indexOf('.'), decimal_places = num.toStri ...
- count group by 组合用法
1 需求是 求订单表1个月内 订单累计费用超过500的有多少人 根据题意 最先写出的sql是这样的 SELECT SUM(totalfee)AS n FROM sendorder WHERE `add ...
- TP5.1:实现分页
前提: (1)为了让分页变得更加好看,我的案例加载了bootstrap和jq的文件,具体操作请参考:http://www.cnblogs.com/finalanddistance/p/9033916. ...
- CRUD全栈式编程架构总结
这里放出实例代码 github.com/SkyvenXiong/HCC
- Linux MySQL单实例源码编译安装5.6
cmake软件 tar -zxvf cmake-2.8.11.2.tar.gz cd cmake-2.8.11.2 ./bootstrap make make install cd ../ 依赖包 ...
- 20145238-荆玉茗 《Java程序设计》第一周学习总结
20145238 <Java程序设计>第一周学习总结 教材学习内容总结 Java三大平台:由于java领域的应用越来越广,根据不同级别的应用开发区分了不同的应用版本,后正式更名为Java ...
- 简单使用mybatis(idea中使用)
首先创建一个maven项目 第一步:在pom.xml中添加依赖 <dependencies> <!--mybatis--> <dependency> <gro ...