[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. SpringMvc返回@ResponseBody中文乱码

    使用SpringMvc的@ResponseBody返回指定数据的类型做为http体向外输出,在浏览器里返回的内容里有中文,会出现乱码,项目的编码.tomcat编码等都已设置成utf-8,如下返回的是一 ...

  2. QQ面板拖拽(慕课网DOM事件探秘)(下)

    2.鼠标事件坐标获取 function fnDown(event) { var event = event || window.event; var oDrag = document.getEleme ...

  3. js中不容小觑的var声明

    在学习vue相关课程中,有一次跟着老师敲代码,写出了如下代码: var Child = { template:`<div @click='handleClick'><slot> ...

  4. 移动端1px边框伪类宽高计算

    移动端1px边框在手机上看显得比较粗,于是我们用伪类结合css3缩放的方法去设置线条,但是如果设置div的一条边,水平线就设置宽度100%,垂直线就设置高度100%,那么如果是div的四条边呢?宽高1 ...

  5. IOS的水滴文件效果

    @implementation ViewController - (void)viewDidLoad{ [super viewDidLoad]; NSDictionary *dict = [NSDic ...

  6. SQL Server时间类型datetime

    SQL Server时间类型datetime 兼容ADO的COleDateTime. SQL datetime 日期和时间数据,可表示1753.1.1 至 9999.12.31的时间,精度为1/300 ...

  7. Web服务器安全设置

    Web服务器安全方面一直重视程度不够,是各种网站经常被黑的主要原因.下面笔者总结了一下关于怎样保证Web服务器安全的措施,希望能给那些服务器尚存在漏洞的用户提供一些帮助. 本文主要以Windows s ...

  8. uva11491 Erasing and Winning

    边读入边处理 优化了速度一开始有想错了的地方.处理输入有点想用stringstream, 的问题在于他把字符串连续的数字作为一个整体,遇到空格才分开,所以不适用 #include<cstdio& ...

  9. B6. Concurrent 内存模型与线程交互

    [概述]

  10. PHP100视频教程-->视频下载

    链接:https://pan.baidu.com/s/14tbX1rz3hYSKY6k0T6WVzg提取码:kypy PHP是一种目前最流行的服务端Web程序开发语言之一.PHP主要的特点是语法简单易 ...