题目描述

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. BootStrap有用代码片段(持续总结)

    > 如题.持续总结自己在使用BootStrap中遇到的问题.并记录解决方法.希望能帮到须要的小伙伴 1.bootstrap上下布局.顶部固定下部填充 应用场景:经典上下布局中,顶部导航条固定,下 ...

  2. Car Talk2

    #! /usr/bin/python # -*- coding: utf-8 -*- # # # “Recently I had a visit with my mom and we realized ...

  3. cnmp安装失败,报错npm ERR! enoent ENOENT: no such file or directory,

    1.cnmp安装失败 2.提示如下: bogon:node_modules liangjingming$ sudo npm install cnpm -g --registry=https://reg ...

  4. 《剑指offer》矩形覆盖

    一.题目描述 我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形.请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法? 二.输入描述 输入n 三.输出描述 输出有多少种不同的覆 ...

  5. hdu 2647 Reward(拓扑排序+优先队列)

    Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he ...

  6. exsi从磁盘中加载虚拟机

    e

  7. vue里面模板解析数据的时候

    页面中新建信息的时候值之间有多个空格的时候 可以使用pre标签,你写了多少个空格,页面就会渲染出来 html解析 什么是pre标签

  8. php八大设计模式之观察者模式

    例如在登录时,需要判断用户是第几次登录,登录过于频繁我们就给用户提示异常.根据用户的爱好,在用户登录后给予相应的猜你喜欢.如果都在 登录时判断密码的方法内完成,不符合面向对对象的单一职责.那我们该怎么 ...

  9. vue-router路由配置

    转自http://www.cnblogs.com/padding1015/ 两种配置方法:在main.js中 || 在src/router文件夹下的index.js中 src/router/index ...

  10. bzoj 3408 热浪 最短路

    一道最短路的模板题,就当练习一下SPFA和dijkstra了 SPFA #include<bits/stdc++.h> using namespace std; struct edge{ ...