POJ-3274 Gold Balanced Lineup
2016-03-31 22:47
399 查看
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 13958 | Accepted: 4038 |
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 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
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 3 7 6 7 2 1 4 2
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
分析:对前i个奶牛的特长求前缀和,然后hash判断之前有没有出现过相对差一样的串,注意一开始的没有奶牛时也要插入hash。
#include <cstdio> #include <iostream> using namespace std; const int Func[31]={10000,1000,100,10,1,10,100,1000,1000,1000,100,10,100,1000,1000,1000,10,10,11,12,13,15,100,15,16,17,18,16,100,1000}; bool jud[400001]; int n,k,num,ans,val[400001],f[400001][31],now[200001],F[200001]; bool JUD(int s) { for(int i=1;i <= k;i++) if(f[s][i] != now[i]) return false; return true; } int Find() { int s=0; for(int i=1;i <= k;i++) s+=now[i]*Func[i]; s=s%230007; while(jud[s] && !JUD(s)) s++; if(jud[s]) return val[s]; return -1; } void insert(int x) { int s=0; for(int i=1;i <= k;i++) s+=now[i]*Func[i]; s=s%230007; while(jud[s]) s++; for(int i=1;i <= k;i++) f[s][i]=now[i]; jud[s]=true; val[s]=x; } int main() { scanf("%d %d",&n,&k); insert(0); for(int i=1;i <= n;i++) { scanf("%d",&num); int Min=2147483647; for(int j=1;j <= k;j++) { if(num & (1<<(j-1))) F[j]++; Min=min(Min,F[j]); } for(int j=1;j <= k;j++) now[j]=F[j]-Min; int pos=Find(); if(pos >= 0) ans=max(ans,i-pos); else insert(i); } cout<<ans<<endl; }
相关文章推荐
- Django框架使用
- Django:之BBS项目
- Sereja and Algorithm
- Django:之安全、国际化和session
- 08.Django自定义模板,自定义标签和自定义过滤器
- go语言开启本地帮助文档
- Google Drive Oauth2.0认证流程
- Puppet使用方法总结
- DJango框架对Models操作
- django 用户登陆注册
- 减少2D碰撞体的顶点数量——PolygonCollider2D
- 最大子数组问题-GoLang实现
- 举例讲解Go语言中函数的闭包使用
- CF-Sereja and Algorithm
- 基站定位(Google API)
- jango中如何使用django-celery完成异步任务 (1)
- mongo创建集合
- Golang 环境配置建议(Atom)
- a/b测试,不要再拍脑袋决策了!看看fb/google怎么玩?
- Google Drive开启API和创建Credential