P1360 [USACO07MAR]Gold Balanced Lineup G
\(\mathbf{P1360}\) 题解
思路
设\(sum[t][i]\)为截至第t天第i项能力的提升总次数。
由题意可知一个时期为均衡时期\([t_1,t_2]\),当且仅当 \(\forall\;1\leq i \leq m,sum[t_2][i]-sum[t_1-1][i]\)都相等。
- 由上,对于每个\(t\),可以将序列\(sum[t]\)的每个数减去\(sum[t][1]\),得到一个序列\(f\)(长为\(m\)),它对应值\(t\)。
- 也可以用差分的方法构造序列\(f\),使\(f[i]=sum[t][i]-sum[t][i-1](1\leq i\leq m-1)\)。
这样得到一个序列集合,由这个序列集合就可以统计答案了。
可以用一个map或者Hash表维护。
根据贪心思想,在相同的\(f\)中最先出现的一定最优,所以对于每个\(t\),如果\(f\)出现过,那么更新答案\(ans=\max(ans,t-h[f])\),否则加入\(f\)。
代码
代码一:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<map>
#define N 100010
#define M 50
#define P 13331
using namespace std;
typedef unsigned long long ull;
int n,m;
int f[M];
map<ull,int>mp;
int read(){
int x=0,f=1;char c=getchar();
while(c<'0' || c>'9') f=(c=='-')?-1:1,c=getchar();
while(c>='0' && c<='9') x=x*10+c-48,c=getchar();
return x*f;
}
ull Hash(){
ull a=0;
for(int i=1;i<=m;i++) a=a*P+f[i];
return a;
}
int main(){
n=read(),m=read();
int ans=0;
mp[Hash()]=0;
for(int i=1;i<=n;i++){
int a=read();
for(int j=0;j<m;j++)
if(a&(1<<j)) ++f[j+1];
if(a&1)
for(int j=1;j<=m;j++) f[j]--;
ull H=Hash();
if(mp.find(H)!=mp.end()) ans=max(ans,i-mp[H]);
else mp[H]=i;
}
printf("%d\n",ans);
return 0;
}
代码二:
#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<map>
using namespace std;
int n,m,sum[32],ans;
vector<int>b;
map<vector<int>,int>h;
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<m;i++)
b.push_back(0);
h[b]=0;
for(int i=1,a;i<=n;i++)
{
scanf("%d",&a);
b.clear();
for(int j=0;j<m;j++)
{
if(a&(1<<j)) sum[j]++;
if(j) b.push_back(sum[j]-sum[j-1]);
}
if(h.find(b)!=h.end()) ans=max(ans,i-h[b]);
else h[b]=i;
}
printf("%d",ans);
return 0;
}
P1360 [USACO07MAR]Gold Balanced Lineup G的更多相关文章
- 洛谷 P1360 [USACO07MAR]Gold Balanced Lineup G (前缀和+思维)
P1360 [USACO07MAR]Gold Balanced Lineup G (前缀和+思维) 前言 题目链接 本题作为一道Stl练习题来说,还是非常不错的,解决的思维比较巧妙 算是一道不错的题 ...
- POJ 3274 Gold Balanced Lineup
Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10924 Accepted: 3244 ...
- 哈希-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 3274:Gold Balanced Lineup 做了两个小时的哈希
Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13540 Accepted: ...
- poj3274 Gold Balanced Lineup(HASH)
Description Farmer John's N cows (1 ≤ N ≤ 100,000) share many similarities. In fact, FJ has been abl ...
- Gold Balanced Lineup POJ - 3274
Description Farmer John's N cows (1 ≤ N ≤ 100,000) share many similarities. In fact, FJ has been abl ...
- POJ 3274 Gold Balanced Lineup 哈希,查重 难度:3
Farmer John's N cows (1 ≤ N ≤ 100,000) share many similarities. In fact, FJ has been able to narrow ...
- Gold Balanced Lineup(哈希表)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10711 Accepted: 3182 Description Farm ...
随机推荐
- Word rings
Word rings 题目描述 这道题就是想求出所有的环,然后在所有环中比较出环串的平均长度最长的那一个,然后就输出平均长度最长的,如果在一个点当中的样例中没有环的话我们就应该输出"No S ...
- visual studio 2015 安装MSDN全称Microsoft Developer Network 安装离线的MSDN
MSDN: 微软向开发人员提供的一套帮助系统,其中包含大量的开发文档,技术文章和示例代码. 这里介绍了vs2015 装离线的MSDN(说明一点是,如果不行,说明你的文件有缺陷,没安装好,之前我用vs2 ...
- 安装两个Eclipse 版本不一致,高版本无法打开
Could not create the JavaVirtual Machine,A fatal exception has occurred. 首先删除了 工作空间的配置 然后删除掉C:\Windo ...
- 在Linux命令行内的大小写转换
在编辑文本时大小写常常是需要注意的地方,大小写的转换是很枯燥而繁琐的工作,所幸,Linux 提供了很多能让这份工作变得容易的命令.接下来让我们看看都有哪些完成大小写转换的命令. tr 命令 tr (t ...
- 2016年 实验三 B2C模拟实验
实验三 B2C模拟实验 [实验目的] 掌握网上购物的基本流程和B2C平台的运营 [实验条件] ⑴.个人计算机一台 ⑵.计算机通过局域网形式接入互联网. (3).奥派电子商务应用软件 [知识准备] 本实 ...
- 多测师_git和github_004
git Git(读音为/gɪt/.),是目前世界上最先进的分布式版本控制系统,可以有效.高速的处理从很小到非常大的项目版本管理.Git 是 Linus Torvalds 为了帮助管理 Linux 内核 ...
- 题解:CF593D Happy Tree Party
题解:CF593D Happy Tree Party Description Bogdan has a birthday today and mom gave him a tree consistin ...
- 1、微信小程序开发介绍。
微信小程序如何能达到快速的开发效果,下面首先介绍一下需要的框架,使用这些框架可以减少大部分编写代码时间. 微信小程序使用的框架:weui开源框架 后端数据使用的框架(包含管理和api接口框架):YiS ...
- FrameworkElementFactory中的SetBinding与SetValue
public static Microsoft.Windows.Controls.DataGridColumn CreateDateColumn(string path, string header) ...
- Asp.Net Core如何根据数据库自动生成实体类
通过引用Nuget包添加实体类 运行 Install-Package Microsoft.EntityFrameworkCore.SqlServer 运行 Install-Package Micros ...