题目描述

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. Codeforces Round #539 (Div. 1) E - Sasha and a Very Easy Test 线段树

    如果mod是质数就好做了,但是做除法的时候对于合数mod可能没有逆元.所以就只有存一下mod的每个质因数(最多9个)的幂,和剩下一坨与mod互质的一部分.然后就能做了.有点恶心. CODE #incl ...

  2. Windows服务启动时候报错1053

    用.net 开发了一个C#语言的windows服务,在本地和测试环境,安装启动都正常,在新的线上环境报错,不能启动-报出-错误1053:服务没有及时响应启动或控制请求. 后来发现时线上.NET FRA ...

  3. JSON格式数据

    1. 基础知识 1. 什么是JSON格式? JSON格式是现在网站数据交互的标准数据格式,写入标准. 取代原来的XML:符合JS原生语法,可以被直接解析,不需要额外的解析文件. 书写简单,一目了然 2 ...

  4. MySQL5.7.6 general tablespace

    摘要: 从5.7.6开始,增加了一种新的 tablespace模式(成为general tablespace),实际上它和共享表空间比较类似:创建一个单独的ibd,ibd中包含多个表,兼容不同的格式. ...

  5. Spring boot请求参数

    GET请求: 1.restful风格: @GetMapping("/order/detail") public BaseOutput omsQueryDetail(@Request ...

  6. Celery和Flask

    第一章:Celery 第二章:Flask登录 第三章:flask简介 第四章:flask应用启动流程 第五章:路由第六章:上下文 第七章:请求 第八章:响应 第九章:session

  7. CodeForces - 999C Alphabetic Removals

    C - Alphabetic Removals ≤k≤n≤4⋅105) - the length of the string and the number of letters Polycarp wi ...

  8. HDOJ->考新郎(sdut1021)

    考新郎 Problem Description 在一场盛大的集体婚礼中,为了使婚礼进行的丰富一些,司仪临时想出了有一个有意思的节目,叫做"考新郎",具体的操作是这样的: 首先,给每 ...

  9. nodejs中http服务器,如何使用GET,POST请求发送数据、npm、以及一些插件的介绍

    浏览器给服务器传递参数,最常用的是地址栏传参(get),以及表单提交(post) 先说get传参,就是在url后跟上?key=value&key2=value2...... 但是按照前几篇的h ...

  10. c++ 判断是元音还是辅音

    #include <iostream> using namespace std; int main() { char c; int isLowercaseVowel, isUppercas ...