POJ 3274/洛谷 1360:Gold Balanced Lineup 黄金阵容平衡
题目描述 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 黄金阵容平衡的更多相关文章
- 洛谷 P2880 [USACO07JAN]Balanced Lineup G (ST表模板)
题意:给你一组数,询问\(q\)次,问所给区间内的最大值和最小值的差. 题解:经典RMQ问题,用st表维护两个数组分别记录最大值和最小值然后直接查询输出就好了 代码: int n,q; int a[N ...
- 洛谷P2880 [USACO07JAN] Balanced Lineup G(树状数组/线段树)
维护区间最值的模板题. 1.树状数组 1 #include<bits/stdc++.h> 2 //树状数组做法 3 using namespace std; 4 const int N=5 ...
- POJ 3274 Gold Balanced Lineup
Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10924 Accepted: 3244 ...
- POJ 3274:Gold Balanced Lineup 做了两个小时的哈希
Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13540 Accepted: ...
- 洛谷 P1360 [USACO07MAR]Gold Balanced Lineup G (前缀和+思维)
P1360 [USACO07MAR]Gold Balanced Lineup G (前缀和+思维) 前言 题目链接 本题作为一道Stl练习题来说,还是非常不错的,解决的思维比较巧妙 算是一道不错的题 ...
- 哈希-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 ...
- 1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列
1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 510 S ...
- POJ 1845 (洛谷 :题目待添加)Sumdiv
约数和 题目描述 给出a和b求a^b的约数和. 输入格式: 一行两个数a,b. 输出格式: 一个数表示结果对 9901 的模. Input 2 3 Output 15 SB的思路: 这是一道典型的数论 ...
- poj 3274 Gold Balanced Lineup(哈希 )
题目:http://poj.org/problem?id=3274 #include <iostream> #include<cstdio> #include<cstri ...
随机推荐
- SpringBoot启动过程原理(转)
1.1 Springboot启动: @SpringBootApplication public class ServerApplication { public static void main(St ...
- java内存溢出定位
一.内存溢出问题分类 瞬时流量过大造成的创建大量对象 内存泄漏导致的内存溢出,一般就是程序编码的BUG引起的 二.内存泄漏问题分析 step1: 收集内存泄漏的堆内存异常日志 > 添加HeapD ...
- list,tuple,set,dict基础
list # @Auther : chen # @Time : 2018/4/26 19:55 # @File : list_ex.py # @SoftWare : PyCharm # list1 = ...
- SP703 SERVICE - Mobile Service
思路:DP 提交:1次 题解: 我们把处理到的要求作为阶段. \(f[i][x][y][z]\)表示第 \(i\) 个要求,三个人分别的位置. 发现这样有很多无用状态,因为显然在第 \(i\) 个要求 ...
- controller层直接通过server类调用mapper的通用方法
自己写的方法没有,但是逆向生成的server类会有继承maybatis-plus的框架 与下图的配置有关
- speech-to-text-wavenet
docker pull buriburisuri/speech-to-text-wavenet docker run -it buriburisuri/speech-to-text-wavenet p ...
- 3.5寸1.44M软盘结构
结构: 划分: 簇:磁盘驱动器在向磁盘读取和写入数据时,要以扇区为单位.在磁盘上,DOS操作系统是以“簇”为单位为文件分配磁盘空间的.硬盘的簇通常为多个扇区,与磁盘的种类.DOS 版本及硬盘分区的大小 ...
- CodeForces 494B Obsessive String ——(字符串DP+KMP)
这题的题意就很晦涩.题意是:问有多少种方法,把字符串s划分成不重叠的子串(可以不使用完s的所有字符,但是这些子串必须不重叠),使得t串是所有这些新串的子串.譬如第一个样例,"ababa&qu ...
- Hdu5178
Hdu5178 题意: 题目给你N个点,问有多少对点的长度小于K . 解法: 首先将所给的坐标从大到小排序,则此题转化为:对排序后的新数列,对每个左边的\(x_a\)找到它右边最远的 $ x_b $ ...
- instr和like的使用区别
1.instr函数 instr函数是一个字符串处理函数,它在Oracle/PLSQL中是返回子字符串在源字符串中的位置,如果在源串中没有找到子串,则返回0. instr函数定义如下: /* * 返回子 ...