\(\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]\)都相等。

  1. 由上,对于每个\(t\),可以将序列\(sum[t]\)的每个数减去\(sum[t][1]\),得到一个序列\(f\)(长为\(m\)),它对应值\(t\)。
  2. 也可以用差分的方法构造序列\(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的更多相关文章

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

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

  2. POJ 3274 Gold Balanced Lineup

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

  3. 哈希-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 ...

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

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

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

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

  6. poj3274 Gold Balanced Lineup(HASH)

    Description Farmer John's N cows (1 ≤ N ≤ 100,000) share many similarities. In fact, FJ has been abl ...

  7. Gold Balanced Lineup POJ - 3274

    Description Farmer John's N cows (1 ≤ N ≤ 100,000) share many similarities. In fact, FJ has been abl ...

  8. 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 ...

  9. Gold Balanced Lineup(哈希表)

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10711   Accepted: 3182 Description Farm ...

随机推荐

  1. RTThread DFS文件系统使用: 基于使用SFUD驱动的SPI FLASH之上的ELM FATFS文件系统

    参考博文: 博文很长,但是实际要操作的步骤没几下. http://m.elecfans.com/article/730878.html  为了防止几年后文章链接找不到,我把文章复制过来了 /***** ...

  2. Linux驱动知识点

    # i2c_add_driver和i2c_new_device匹配后调用i2c_probe # 启动开发板,在超级终端中输入命令"cat /proc/misc"也可以查看对应的杂项 ...

  3. [Angular JS教程] HeroService: getHeroes failed: undefined 问题解决方法

    最近在学习入门Angular JS,学习资源是https://angular.cn/tutorial, 在学习到 "https://angular.cn/tutorial/toh-pt6模拟 ...

  4. Java基础系列-RandomAccess

    原创文章,转载请标注出处:https://www.cnblogs.com/V1haoge/p/10755424.html Random是随机的意思,Access是访问的意思,合起来就是随机访问的意思. ...

  5. idea 2020.1 Mybatis log plugin破解插件

    下载 链接: https://pan.baidu.com/s/1FTgtJiyzxxaNxWLyX4OgZw 密码: w7w8 idea安装本地插件

  6. Windows 10 如何在桌面上显示“此电脑”和“控制面板”

    新电脑安装好 Windows 10 系统,默认在桌面上是不显示 "此电脑" 和 "控制面板" 图标的. 如果是 Windows 10 家庭版,桌面一般只显示&q ...

  7. C#与sql进行图片存取

    1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Da ...

  8. centos7 安装k8s kubectl 客户端

    1. 配置k8s的kubelet 管理客户端 1 cat <<EOF > /etc/yum.repos.d/kubernetes.repo 2 [kubernetes] 3 name ...

  9. NB 的开源项目遍地开花——GitHub 热点速览 Vol.41

    作者:HelloGitHub-小鱼干 本周的 GitHub 热点速览的关键词便是 nb,也是本周特推之一的项目名字,这个功能如名字一般 nb 的项目是一个脚本,帮你处理笔记.书签.归档和知识库应用程序 ...

  10. 换掉7z-zip默认的ico图标,自定义压缩文件图标更美观。

    下图就是7z官网源代码里面的ico文件,如果有条件自己编译,可以直接替换下面的图标,然后编译一个你自己的7z工具就行.不过我比较懒,还是通过修改注册表的方式改成别的ico图标吧. 源码和可执行程序下载 ...