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

题目描述

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次

#include<iostream>
#include<cstdio>
using namespace std;
int n,m,sum[][];
bool check(int l,int r){
int sta=;
for(int i=;i<m;i++){
if(i!=&&sum[r][i]-sum[l-][i]!=sta)return ;
if(i==)sta=sum[r][i]-sum[l-][i];
if(sum[r][i]-sum[l-][i]==)return ;
}
return ;
}
int main(){
scanf("%d%d",&n,&m);
int x;
for(int i=;i<=n;i++){
scanf("%d",&x);
for(int j=;j<m;j++){
sum[i][j]=sum[i-][j];
if((<<j)&x)sum[i][j]++;
}
}
int ans=;
for(int i=;i<=n;i++){
int now=ans;
for(int j=i+now;j<=n;j++){
int l=j-i+;
if(check(i,j))
ans=max(ans,l);
}
}
printf("%d",ans);
}

68分 暴力

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#define mod 9901
using namespace std;
map<vector<int>,int>f;
int ans;
int main(){
int n,m;scanf("%d%d",&n,&m);
vector<int>now(m);
f[now]=;
for(int i=;i<=n;i++){
int x;scanf("%d",&x);
for(int j=;j<m;j++)
if(x&(<<j))now[j]++;
if(x&)for(int j=;j<m;j++)now[j]--;
if(f.count(now))ans=max(ans,i-f[now]);
else f[now]=i;
}
printf("%d",ans);
}

100分

最新文章

  1. 练手小游戏(代码篇之敌人AI
  2. 解决 Mac Pro 用 Excel 打开 CSV 文件不能正常显示的问题
  3. windows重新获取IP
  4. 文科生也能看懂的iptables教程(转载)
  5. php安装出现的部分错误
  6. QQ群共享文件下载很慢解决办法
  7. [原创]Microsoft.AnalysisServices.ManagementDialogs.ServerPropertiesLanguagePanel 的类型初始值设定项引发异常
  8. JAVA中正則表達式总结
  9. 电脑bios到底是什么?
  10. 基于Apriori算法的Nginx+Lua+ELK异常流量拦截方案 郑昀 基于杨海波的设计文档(转)
  11. HTK语音识别示例(Ubuntu)
  12. Github Page--CSDN新人的第二选择
  13. django---不使用view,直接从Url转到html
  14. 前后端分离框架前端react,后端springboot跨域问题分析
  15. [NOI2015]寿司晚宴(状压dp)
  16. Vertica系列:从一些细节看Vertica为什么是一个优秀的数据仓库平台
  17. JS实现抽奖(方形)
  18. XE2 运行时 item not found的解决办法
  19. Linux学习笔记:Jenkins的使用
  20. spring mvc 的请求流程

热门文章

  1. SSH Secure Shell 的使用
  2. Android之Widget学习总结
  3. Ffmpeg移植S3C2440
  4. MySQL_西安11月销售昨日未上架的产品_20161212
  5. bzoj 1007: [HNOI2008]水平可见直线 半平面交
  6. http头部解释
  7. DSP基础
  8. Python:format()方法
  9. Python:正则表达式(三)*、+、?的用法
  10. Java 编程规范,常见规范,命名规范,复杂度