题目描述

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.

神牛小R在许多方面都有着很强的能力,具体的说,他总共有m种能力,并将这些能力编号为1到m。他的能力是一天一天地提升的,每天都会有一些能力得到一次提升,R对每天的能力提升都用一个数字表示,称之为能力提升数字,比如数字13,转化为二进制为1101,并且从右往左看,表示他的编号为1,3,4的能力分别得到了一次提升。小R把每天表示能力提升的数字的记了下来,如果在连续的一段时间内,小R的每项能力都提升了相同的次数,小R就会称这段时间为一个均衡时期,比如在连续5天内,小R的每种能力都提升了4次,那么这就是一个长度为5的均衡时期。

于是,问题来了,给出 小R n天的能力提升数字,请求出均衡时期的最大长度。

【数据规模】对于50%的数据,N <= 1000。

输入输出格式

输入格式:

第一行有两个整数n,m,表示有n天,m种能力。接下来有n行,每行有一个整数,分别表示第1到n天的能力提升数字。能力提升数字转化为二进制后,从右到左的每一位表示对应的能力是否在当天得到了一次提升。

n<=100000, m<=30

输出格式:

输出只有一个整数,表示长度最大的均衡时期的长度。

输入输出样例

输入样例#1:

7 3
7
6
7
2
1
4
2
输出样例#1:

4

说明

【样例解释】

每天被提升的能力种类分别为:

第一天:1,2,3

第二天:2,3

第三天:1,2,3

第四天:2

第五天:1

第六天:3

第七天:2

第三天到第六天为长度最长的均衡时期

因为 这四天 每种能力分别提升了 2次

/*
刚开始以为是求区间和%(2^m-1)=0的最大区间,后来发现是不对的,应该将状态全部储存进去。
当两个数相同时,说明在这两个数之间出现的数在每个特征上出现的数目相同,否则是不会两个数相
同的。因为只有最右边的那个数增加了和左边所有的数增加的数字相同,他们才会减去最右边的数,
出现相同。
*/
#include<string>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define mod 100007
using namespace std;
int hash[mod+][];
int a[mod][];
int s[mod][];
int k;
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;
}
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));//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);
return ;
}

[USACO07MAR]黄金阵容均衡Gold Balanced L…(洛谷 1360)的更多相关文章

  1. 洛谷P1360 [USACO07MAR]黄金阵容均衡Gold Balanced L…

    P1360 [USACO07MAR]黄金阵容均衡Gold Balanced L… 题目描述 Farmer John's N cows (1 ≤ N ≤ 100,000) share many simi ...

  2. 洛谷 P1360 [USACO07MAR]黄金阵容均衡Gold Balanced L…

    P1360 [USACO07MAR]黄金阵容均衡Gold Balanced L… 题目描述 Farmer John's N cows (1 ≤ N ≤ 100,000) share many simi ...

  3. [USACO07MAR]黄金阵容均衡Gold Balanced L…

    https://www.luogu.org/problem/show?pid=1360 题目描述 Farmer John's N cows (1 ≤ N ≤ 100,000) share many s ...

  4. [USACO07MAR]黄金阵容均衡Gold Balanced L… map

    题目描述 Farmer John's N cows (1 ≤ N ≤ 100,000) share many similarities. In fact, FJ has been able to na ...

  5. [luoguP1360] [USACO07MAR]黄金阵容均衡Gold Balanced L…

    传送门 真的骚的一个题,看了半天只会个前缀和+暴力.. 纯考思维.. 良心题解 #include <cstdio> #include <cstring> #include &l ...

  6. 洛谷P1360 [USACO07MAR]黄金阵容均衡题解

    题目 不得不说这个题非常毒瘤. 简化题意 这个题的暴力还是非常好想的,完全可以过\(50\%\)的数据.但是\(100\%\)就很难想了. 因为数据很大,所以我们需要用\(O(\sqrt n)\)的时 ...

  7. [LuoguP1360][USACP07MAR]黄金阵容均衡

    [LuoguP1360][USACP07MAR]黄金阵容均衡(Link) 每天会增加一个数\(A\),将\(A\)二进制分解为\(a[i]\),对于每一个\(i\)都增加\(a[i]\),如果一段时间 ...

  8. 洛谷 P4999(数位DP)

    ###洛谷 P4999 题目链接 ### 题目大意:给你一个区间,求这段区间中所有数的,数位上的,数字之和. 分析: 这题与 洛谷 P2602 相似,稍微改一下就可以了. 求出 0 ~ 9 的个数,然 ...

  9. POJ 3274/洛谷 1360:Gold Balanced Lineup 黄金阵容平衡

    题目描述 Farmer John's N cows (1 ≤ N ≤ 100,000) share many similarities. In fact, FJ has been able to na ...

随机推荐

  1. Android 全屏显示

    Android全屏显示: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInst ...

  2. 传统BIOS+UEFI 系统引导修复

    一.    去网上下载一款pe软件:BIOS+UEFI引导修复工具这个软件支持传统bios和最新的UEFI引导(1)进入PE环境(win也可以,不过引导损坏一般不能进win),打开软件         ...

  3. FineUI第九天---表单验证

    表单验证 步骤: 1.比如为TextBox设置Required=”true”或者ShowRedStar=”true”两个属性,表示此输入为必填项,并且在标签的后面显示一个红色的星号提示. 2.Vali ...

  4. Redis优化之CPU充分利用

    Linux Redis Server之CPU充分利用 不知道大家有没有注意到你们公司的集群配置是否是有一种配置是这样的: 多个Redis Server分布在同一个节点,只是端口不同,如果有的话,应该是 ...

  5. Java工程为什么要加一个biz层

    biz是Business的缩写,实际上就是控制层(业务逻辑层).解释:控制层的主要作用就是协调model层和view层直接的调用和转换.能够有效的避免请求直接进行数据库内容调用,而忽略了逻辑处理的部分 ...

  6. explicit,violate,volatile,mutable小结

    转自:http://blog.csdn.net/helonsy/article/details/7091130 explicit:放在构造函数前面可以阻止构造函数的隐式类型转换.这样可以避免不必要的错 ...

  7. django之form表单验证

    django中的Form一般有两种功能: 输入html 验证用户输入 #!/usr/bin/env python # -*- coding:utf- -*- import re from django ...

  8. 浅析mongoEngine的document对象

    引言: from mongoengine import * connect('local')class Test(Document): name=StringField(max_length=32) ...

  9. 网页的title左边的小图片怎么添加

    首先,代码中的title标签里是不能加图片的.但是浏览器标提栏前面是可以加一个小图标的. 解决方案:第一步,做一个16 X 16像素的ico格式的图标.具体操作方法是,先在Photoshop中做一个透 ...

  10. Range Sum Query 2D - Mutable & Immutable

    Range Sum Query 2D - Mutable Given a 2D matrix matrix, find the sum of the elements inside the recta ...