[Usaco2007 Mar]Gold Balanced Lineup 平衡的队列

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 807  Solved: 317
[Submit][Status][Discuss]

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.

N(1<=N<=100000)头牛,一共K(1<=K<=30)种特色,
每头牛有多种特色,用二进制01表示它的特色ID。比如特色ID为13(1101),
则它有第1、3、4种特色。[i,j]段被称为balanced当且仅当K种特色在[i,j]内
拥有次数相同。求最大的[i,j]段长度。

Input

* Line 1: Two space-separated integers, N and K.

* 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

* 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

INPUT DETAILS:

The line has 7 cows with 3 features; the table below summarizes the
correspondence:
Feature 3: 1 1 1 0 0 1 0
Feature 2: 1 1 1 1 0 0 1
Feature 1: 1 0 1 0 1 0 0
Key: 7 6 7 2 1 4 2
Cow #: 1 2 3 4 5 6 7

Sample Output

4

OUTPUT DETAILS:

In the range from cow #3 to cow #6 (of size 4), each feature appears
in exactly 2 cows in this range:
Feature 3: 1 0 0 1 -> two total
Feature 2: 1 1 0 0 -> two total
Feature 1: 1 0 1 0 -> two total
Key: 7 2 1 4
Cow #: 3 4 5 6

HINT

鸣谢fjxmyzwd

Source

 
题解:多维的前缀和。
 #include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdio>
using namespace std; int k;
int hash[][],mod=,a[][],s[][]; inline bool check(int t,int xt)
{
int i;
bool flag=true;
for(i=;i<=k-;i++)
if(s[xt][i]!=hash[t][i])
return false;
return true;
}
inline int find(int x,int xt,int xp)
{
int t=x;
while(hash[t][]!=-)
{
if(!check(t,xt)) t=(t+)%mod;
else break;
}
if(hash[t][]==-)
{
int i;
for(i=;i<=k-;i++)
hash[t][i]=s[xt][i];
hash[t][]=xp;
hash[t][]=;
return xp;
}
return hash[t][];
}
int main()
{
int n;
scanf("%d%d",&n,&k);
int i,j;
int x;
for(i=;i<=n;i++)
{
scanf("%d",&x);
int p=;
while(x!=)
{
a[i][p]=x%;
x=x/;
p++;
}
}
for(i=;i<=n;i++)
for(j=;j<=k-;j++)
s[i][j]=s[i-][j]+a[i][j];
for(i=;i<=n;i++)
for(j=k-;j>=;j--)
s[i][j]-=s[i][];
memset(hash,-,sizeof(hash));
int ans=;
for(i=;i<=n;i++)
{
int p=;
for(j=k-;j>=;j--)
{
p=(p*+s[i][j])%mod;
while(p<)
p=-p;
}
int loc=find(p,i,i);
ans=max(ans,i-loc);
}
printf("%d\n",ans);
}

bzoj 1702 贪心,前缀和的更多相关文章

  1. 【贪心+前缀】C. Fountains

    http://codeforces.com/contest/799/problem/C [题意] 有n做花园,有人有c个硬币,d个钻石 (2 ≤ n ≤ 100 000, 0 ≤ c, d ≤ 100 ...

  2. hdu3613 Best Reward manacher+贪心+前缀和

    After an uphill battle, General Li won a great victory. Now the head of state decide to reward him w ...

  3. bzoj 1044 贪心二分+DP

    原题传送门http://www.lydsy.com/JudgeOnline/problem.php?id=1044 首先对于第一问,我们可以轻易的用二分答案来搞定,对于每一个二分到的mid值 我们从l ...

  4. bzoj 1193 贪心

    如果两点的曼哈顿距离在一定范围内时我们直接暴力搜索就可以得到答案,那么开始贪心的跳,判断两点横纵坐标的差值,差值大的方向条2,小的条1,不断做,直到曼哈顿距离较小时可以暴力求解. 备注:开始想的是确定 ...

  5. bzoj 2697 贪心

    就贪心就行了,首先可以看成n个格子,放物品,那么 一个物品假设放3个,放在1,k,n处,那么价值和放在1,n 是一样的,所以一个物品只放两个就行了,价值大的应该尽量放 在两边,那么排序之后模拟就行了 ...

  6. bzoj 3037 贪心

    我们可以贪心的分析,每个点的入度如果是0,那么这个点不可能 被用来更新答案,那么我们每次找入度为0的点,将他去掉,如果他连的 点没有被更新过答案,那么更新答案,去掉该点,环的时候最后处理就行了 /** ...

  7. suoi38 卖XY序列 (贪心+前缀和)

    因为只能带一个,买卖价格又一样,所以只要右边的比左边的大,就从这买下来然后带到下一个卖掉就行了(我想到别处再卖的话大不了再重新买回来嘛) 所以给max(w[i]-w[i-1],0)维护一个前缀和就行了 ...

  8. bzoj 1193 贪心+bfs

    1193: [HNOI2006]马步距离 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2015  Solved: 914[Submit][Statu ...

  9. bzoj 1899 贪心+dp

    思路:这个贪心排顺序我居然没看出来. 吃饭时间长的在前面, 用反证法很容易得出. 剩下的就是瞎dp啦. #include<bits/stdc++.h> #define LL long lo ...

随机推荐

  1. (转)Unity优化之减少Drawcall

    转载:http://www.jianshu.com/p/061e67308e5f Unity GUI(uGUI)使用心得与性能总结 背景和目的 小哈接触Unity3D也有一段时间了,项目组在UI解决方 ...

  2. bootstrap div 固定

    div固定在顶部样式: .navbar-fixed-top div固定在底部样式 .navbar-fixed-bottom

  3. 浅议block实现原理,block为什么使用copy关键字?

    1.block是一个特殊的oc对象,建立在栈上,而不是堆上,这么做一个是为性能考虑,还有就是方便访问局部变量. 2.默认Block使用到的局部变量会被copy,而不是retain.所以,他无法改变局部 ...

  4. poj3616 Milking Time

    思路: dp. 实现: #include <iostream> #include <cstdio> #include <algorithm> using names ...

  5. jquery实现上传图片及图片大小验证、图片预览效果代码

    jquery实现上传图片及图片大小验证.图片预览效果代码 jquery实现上传图片及图片大小验证.图片预览效果代码 上传图片验证 */ function submit_upload_picture() ...

  6. BigDecimal取余运算

    取余运算在编程中运用非常广泛,对于BigDecimal对象取余运算可以通过divideAndRemainder方法实现. public BigDecimal[] divideAndRemainder( ...

  7. sqlserver2012 offset

    /* * Hibernate, Relational Persistence for Idiomatic Java * * License: GNU Lesser General Public Lic ...

  8. Idea maven项目不能新建package和class的解决方法

    如下图,用IDEA新建的maven项目不能新建package,class等 原因是:这里的java文件夹是普通文件夹,要设置为Sources Root.(如下图) 就可以了(见下图)

  9. Swift学习——流程控制

    1.for in循环 (1)简单使用: for-in和范围运算符 for i in 1...3 { println(i) } (2)如果在循环中用不到i,可用_代替 for _ in 1...3 { ...

  10. 数组排序 sort

    数组排序 this.dataShow = this.data.sort((a, b) => { return parseInt(a[this.innerOrderBy]) - parseInt( ...