题目描述

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 = . Since  written in binary is , this means our cow exhibits features , , and  (reading right to left), but not feature . More generally, we find a  in the ^(i-) place if a cow exhibits feature i.

Always the sensitive fellow, FJ lined up cows ..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,,4的能力分别得到了一次提升。小R把每天表示能力提升的数字的记了下来,如果在连续的一段时间内,小R的每项能力都提升了相同的次数,小R就会称这段时间为一个均衡时期,比如在连续5天内,小R的每种能力都提升了4次,那么这就是一个长度为5的均衡时期。

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

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

输入输出格式

输入格式:
第一行有两个整数n,m,表示有n天,m种能力。接下来有n行,每行有一个整数,分别表示第1到n天的能力提升数字。能力提升数字转化为二进制后,从右到左的每一位表示对应的能力是否在当天得到了一次提升。 n<=, m<= 输出格式:
输出只有一个整数,表示长度最大的均衡时期的长度。 输入输出样例 输入样例#: 输出样例#: 说明 【样例解释】 每天被提升的能力种类分别为: 第一天:,, 第二天:, 第三天:,, 第四天: 第五天: 第六天: 第七天: 第三天到第六天为长度最长的均衡时期 因为 这四天 每种能力分别提升了 2次

题目

 #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 ;
}

  

  芒果君:一上来就粘代码2333333求一段最长的“平衡区间”,就是在这段区间内各种特征的数量和相等。首先十进制转二进制,然后求前缀和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。

------------------------------------悲剧的分割线--------------------------------------------

我第一次知道map里还可以塞vector O_o 虽然慢了点但这道题还是绰绰有余。

这道题最简单的方法是:用vector维护当前数值的情况,求出每种能力与能力1的相对值,强行塞进map里。因为如果两个时刻的相对值对应相等,说明他们在中间这段时间的增减了同一个值。

 #include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
typedef long long ll;
map<vector<int>,int>mp;
int n,m,x,ans;
int main()
{
scanf("%d%d",&n,&m);
vector<int>now(m);
mp[now]=;
for(int i=;i<=n;++i){
scanf("%d",&x);
for(int j=;j<m;++j) now[j]+=(x&(<<j)?:)-(x&);
if(mp.count(now)) ans=max(ans,i-mp[now]);
else mp[now]=i;
}
printf("%d\n",ans);
return ;
}

POJ 3274/洛谷 1360:Gold Balanced Lineup 黄金阵容平衡的更多相关文章

  1. 洛谷 P2880 [USACO07JAN]Balanced Lineup G (ST表模板)

    题意:给你一组数,询问\(q\)次,问所给区间内的最大值和最小值的差. 题解:经典RMQ问题,用st表维护两个数组分别记录最大值和最小值然后直接查询输出就好了 代码: int n,q; int a[N ...

  2. 洛谷P2880 [USACO07JAN] Balanced Lineup G(树状数组/线段树)

    维护区间最值的模板题. 1.树状数组 1 #include<bits/stdc++.h> 2 //树状数组做法 3 using namespace std; 4 const int N=5 ...

  3. POJ 3274 Gold Balanced Lineup

    Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10924 Accepted: 3244 ...

  4. POJ 3274:Gold Balanced Lineup 做了两个小时的哈希

    Gold Balanced Lineup Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13540   Accepted:  ...

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

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

  6. 哈希-Gold Balanced Lineup 分类: POJ 哈希 2015-08-07 09:04 2人阅读 评论(0) 收藏

    Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13215 Accepted: 3873 ...

  7. 1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列

    1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 510  S ...

  8. POJ 1845 (洛谷 :题目待添加)Sumdiv

    约数和 题目描述 给出a和b求a^b的约数和. 输入格式: 一行两个数a,b. 输出格式: 一个数表示结果对 9901 的模. Input 2 3 Output 15 SB的思路: 这是一道典型的数论 ...

  9. poj 3274 Gold Balanced Lineup(哈希 )

    题目:http://poj.org/problem?id=3274 #include <iostream> #include<cstdio> #include<cstri ...

随机推荐

  1. [React] Create a Query Parameter Modal Route with React Router

    Routes are some times better served as a modal. If you have a modal (like a login modal) that needs ...

  2. 边学边体验django--HttpRequest 对象

    每个view函数的第一个参数是一个HttpRequest对象. HttpRequest对象包含当前请求URL的一些信息: 属性 描述 path 请求页面的全路径,不包括域名'/hello/' meth ...

  3. PHP mysqli_errno() 函数

    返回最近调用函数的最后一个错误代码: <?php // 假定数据库用户名:root,密码:123456,数据库:RUNOOB $con=mysqli_connect("localhos ...

  4. 001_linux基础命令

    开局日常吹牛一小时,今天更新的是linux的基础命令.现在是2018/5/30,晴,心情挺好的. 回归正题,linux基础命令只是一些初学者常用的命令,如果其他更多高级的命令等我学我再发上来,因为这个 ...

  5. Noip2003 提高组 神经网络

    神经网络 题目背景 人工神经网络(Artificial Neural Network)是一种新兴的具有自我学习能力的计算系统,在模式识别.函数逼近及贷款风险评估等诸多领域有广泛的应用.对神经网络的研究 ...

  6. .net Core使用 MongoDB

    1.安装mogodb windows版本下载地址:https://www.mongodb.com/download-center/v2/community 查看mongod.conf文件,找到绑定的I ...

  7. linux系统rwx(421)、777权限详解

    摘要 linux的常见权限,mark一下 常用的linux文件权限如下: 444 r--r--r-- 600 rw------- 644 rw-r--r-- 666 rw-rw-rw- 700 rwx ...

  8. oracle自定义排序和NULL值排序

    1.自定义顺序 当我们希望将某个查询结果指定的显示顺序展示的时候 order by case when column1=1 then 0 case when column1=1 then 1 else ...

  9. Qt pro工程文件介绍

    app - 建立一个应用程序的makefile.这是默认值,所以如果模板没有被指定,这个将被使用. lib - 建立一个库的makefile. vcapp - 建立一个应用程序的Visual Stud ...

  10. python之selectors模块

    python之selectors模块 selectors模块是在python3.4版本中引进的,它封装了IO多路复用中的select和epoll,能够更快,更方便的实现多并发效果. 官方文档见:htt ...