[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. 由浅入深一个Demo带你认识Restful风格的架构

    java作为一门后端语言,其厉害之处在于web,大家比较熟知的各种网络应用,java都能做,那么在这个移动优先的时代,如何继续发挥java的强大之处呢? 通常是让java作为一个app的服务端,为ap ...

  2. 原生js的容易忽略的相似点(一)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. powerDesigner的name和comment转化

    name2comment.vbs '****************************************************************************** '* ...

  4. tomcat https协议

    一.tomcat证书 JDK自带的keytool工具来生成证书 1. 在jdk的安装目录\bin\keytool.exe下打开keytool.exe 2. 在命令行中输入以下命令: keytool - ...

  5. Object.assign(o1, o2, o3) 对象 复制 合拼

    Object 对象方法学习之(1)—— 使用 Object.assign 复制对象.合并对象 合并对象 var o1 = {a: 1}; var o2 = {b: 2}; var o3 = {c: 3 ...

  6. docker 镜像管理操作

    镜像特点 1. 分层存储的文件 2.一个软件运行环境 3.一个镜像可以创建多个容器 4.一种标准交付 5.不包含Linux内核而又精简的Linux操作系统 6.不是一个单一的文件而是由多层构成的,可以 ...

  7. Kubernetes 架构(上)【转】

    Kubernetes Cluster 由 Master 和 Node 组成,节点上运行着若干 Kubernetes 服务. Master 节点 Master 是 Kubernetes Cluster ...

  8. Vsphere中ESXi主机ssh开启的三种方法

    ESXi 5.5是直接安装在物理主机上的一个虚拟机系统,本质上是一个Linux系统. 平时可以通过VMware Client端或者VMware vCenter进行管理,但对于一些特殊的VMware命令 ...

  9. mybatis-5 手写代理

    @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface Select { public St ...

  10. django insert data into mysql

    #!/usr/bin/python # -*- coding:utf-8 -*- # @filename: search # @author:wwx399777 wuweiji # @date: 20 ...