题目描述

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次

思路:

暴力(没有优化)   60分

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n,m,ans;
int sum[][];
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
int opt,num=;
scanf("%d",&opt);
for(int j=;j<=m;j++) sum[i][j]=sum[i-][j];
while(opt){
num++;
if(opt%==) sum[i][num]+=;
opt/=;
}
}
for(int i=;i<=n;i++){
for(int j=i+;j<=n;j++){
int tmp=sum[j][]-sum[i-][],flag=;
for(int k=;k<=m;k++)
if(sum[j][k]-sum[i-][k]!=tmp){
flag=;
break;
}
if(!flag) ans=max(ans,j-i+);
}
}
cout<<ans;
}

暴力:加上一个剪枝    68分

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n,m,ans;
int sum[][];
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
int opt,num=;
scanf("%d",&opt);
for(int j=;j<=m;j++) sum[i][j]=sum[i-][j];
while(opt){
num++;
if(opt%==) sum[i][num]+=;
opt/=;
}
}
for(int i=;i<=n;i++){
if(n-i+<ans) break;
for(int j=i+;j<=n;j++){
int tmp=sum[j][]-sum[i-][],flag=;
for(int k=;k<=m;k++)
if(sum[j][k]-sum[i-][k]!=tmp){
flag=;
break;
}
if(!flag) ans=max(ans,j-i+);
}
}
cout<<ans;
}

暴力:在剪枝的基础上卡一下时,76分

#include<ctime>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n,m,t,ans;
int sum[][];
int main(){
t=clock();
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
int opt,num=;
scanf("%d",&opt);
for(int j=;j<=m;j++) sum[i][j]=sum[i-][j];
while(opt){
num++;
if(opt%==) sum[i][num]+=;
opt/=;
}
}
for(int i=;i<=n;i++){
if(n-i+<ans) break;
for(int j=i+;j<=n;j++){
int tmp=sum[j][]-sum[i-][],flag=;
for(int k=;k<=m;k++)
if(sum[j][k]-sum[i-][k]!=tmp){
flag=;
break;
}
if(!flag) ans=max(ans,j-i+);
}
if(clock()-t>){
cout<<ans;
return ;
}
}
cout<<ans;
}

正解思路:

啊我写个稍微详细一点的……

求一段最长的“平衡区间”,就是在这段区间内各种特征的数量和相等。首先十进制转二进制,然后求前缀和sum,这是为了方便作差判断——区间开头两特征的差sum[start][x]-sum[start][0]==结尾两特征的差(sum[start][x]+y)-(sum[start][0]+y)==sum[end][x]-sum[end][0],0<=x<k,y是每个特征增加的数量。那么,如果对于每个x都满足以上等式,就可以判断区间成立。为了减小时间复杂度,我们考虑一个哈希操作,minus[i][x]=sum[i][x]-sum[i][0],将∑minus[i][x] %mod表示为key[i],它就是我们哈希的“钥匙”。当然,只是求和+mod还不够,我们需要进一步比较每一位是否都满足,更新答案为编号的差,这样我们就有了一个能AC的思路。

此外还有好多细节(如果你觉得太烦可以不看23333333)。

①22行 将key[i]变为正数再模——不然如果它还是负数就会数组越界。

②search找答案,如果判出相等直接return而不用放入队列——我们设a,b,c三个minus,id a<id b<id c,找到a==b更新答案,如果a==c,那么id c-id a>id b-id a,显然b没有必要放入队列。

③把list[0]放入0——给一组数据1 3 0,应该输出1而不是0。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#define mod 100003
#define inf 1<<29
using namespace std;
int n,k,tz[mod][],ms[mod][],sum[mod][],key[mod],ans;
vector<int>list[mod];
void init(){
for(int i=;i<mod;++i) list[i].clear();
list[].push_back();
for(int i=;i<=n;++i){
for(int j=;j<k;++j){
sum[i][j]=sum[i-][j]+tz[i][j];
ms[i][j]=sum[i][j]-sum[i][];
key[i]+=ms[i][j];
}
key[i]=abs(key[i])%mod;
}
}
void search(int knum,int id){
int len=list[knum].size();
for(int j=;j<len;++j){
int f=;
for(int l=;l<k;++l)
if(ms[list[knum][j]][l]!=ms[id][l]){
f=;
break;
}
if(f){
ans=max(ans,id-list[knum][j]);
return;
}
}
list[knum].push_back(id);
}
int main(){
int t;
scanf("%d%d",&n,&k);
for(int i=;i<=n;++i){
scanf("%d",&t);
for(int j=;j<k;++j){
tz[i][j]=t%;
t/=;
}
}
init();
for(int i=;i<=n;++i) search(key[i],i);
printf("%d",ans);
return ;
}

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

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

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

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

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

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

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

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

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

  5. [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 ...

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

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

  7. 洛谷 P1360 [USACO07MAR]Gold Balanced Lineup G (前缀和+思维)

    P1360 [USACO07MAR]Gold Balanced Lineup G (前缀和+思维) 前言 题目链接 本题作为一道Stl练习题来说,还是非常不错的,解决的思维比较巧妙 算是一道不错的题 ...

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

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

  9. 洛谷P3121 审查(黄金)Censoring(Gold) [USACO15FEB] AC自动机

    正解:AC自动机 解题报告: 传送门! 啊我好呆啊其实就挺模板题的,,,只是要一个栈搞一下,,,然后我就不会了,,,是看了题解才get的,,,QAQ 然后写下解法趴QwQ 首先看到多串匹配不难想到AC ...

随机推荐

  1. CodeWars for JavaScript

    SubClass https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes Sub classing with ...

  2. numeric and int in sql server

    类型映射 https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/sql-server-data-type-mappings C#关 ...

  3. [JZOJ3383] [NOIP2013模拟] 太鼓达人 解题报告(数位欧拉)

    来源:XLk 摘录 HDU2894 Description 七夕祭上,Vani牵着cl的手,在明亮的灯光和欢乐的气氛中愉快地穿行.这时,在前面忽然出现了一台太鼓达人机台,而在机台前坐着的是刚刚被精英队 ...

  4. vue --- 路由传参的几种方式

    方案一: getDescribe(id) { // 直接调用$router.push 实现携带参数的跳转 this.$router.push({ path: `/describe/${id}`, }) ...

  5. 再谈Ubuntu和CentOS安装好之后的联网问题(桥接和NAT、静态和动态ip)(博主推荐)

    不多说,直接上干货! 首先,普及概念. hostonly.桥接和NAT的联网方式 对于CentOS系统,用的最多的就是,NAT和桥接模式 CentOS 6.5静态IP的设置(NAT和桥接联网方式都适用 ...

  6. OpenGL编程逐步深入(八)伸缩变换

    准备知识 伸缩变换非常简单,它的目的是增大或者缩小对象的尺寸.例如:你可能希望用同一个模型创建不同大小的对象(例如形状相同,但大小不同的树木)或者你想改变对象的大小使它和游戏场景匹配.这些例子中你可能 ...

  7. Monad的基本运算

    A monad is created by defining a type constructor M and two operations, bind and return (where retur ...

  8. Slimming Paint (a.k.a. Redesigning Painting and Compositing)

    Slimming Paint is a Paint team project to re-implement the Blink<->cc picture recording API to ...

  9. Embedding Flash Fullscreen in the Browser Window

    For Developers‎ > ‎Design Documents‎ > ‎ Embedding Flash Fullscreen in the Browser Window Auth ...

  10. BZOJ 3637: Query on a tree VI LCT_维护子树信息_点权转边权_好题

    非常喜欢这道题. 点权转边权,这样每次在切断一个点的所有儿子的时候只断掉一条边即可. Code: #include <cstring> #include <cstdio> #i ...