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 ...
随机推荐
- 00 what is C Programming?C编程是什么?
C语言简介 C is a programming language that lets us give a computer very specifio commands. C语言是一种编程语言,它让 ...
- vue3.0版本安装
如果安装过其他版本的vue的话先卸载 npm uninstall -g vue-cli //卸载指令 卸载不会影响以前项目的启动 然后安装 NPM安装: npm install -g @vue/cli ...
- 用python处理excel文件有多轻松?工作从未如此简单
最近需要频繁读写 excel 文件,想通过程序对 excel 文件进行自动化处理,发现使用 python 的 openpyxl 库进行 excel 文件读写实在太方便了,结构清晰,操作简单.本文对 o ...
- Oracle 按不同时间分组统计
1.按年 select to_char(record_date,'yyyy'), sum(col_8) as total_money from table_name where group by to ...
- redis哨兵搭建
redis哨兵搭建 1.复制配置文件到conf #单机安装以后[root@t3 redis-5.0.8]# pwd/app/redis-5.0.8[root@t3 redis-5.0.8]# cp s ...
- 使用git 版本控制的代码在线修调试,如何还原
在线调试: 先切换成www用户进入项目的根目录比如/data/wwwroot/website su www cd /data/wwwroot/website vi ./api/controllers/ ...
- 彻底根治window弹窗小广告(今日热点)
在一个阴雨蒙蒙的下午,我上完厕所回到工位,输入锁屏密码,解锁,蹦出来三个小广告,我......这还能忍??? 废话不多说,开搞! 一.广告分为两种: 红色字的今日热点 蓝色字的今日热点 二.追溯根源: ...
- webpack5文档解析(上)
webpack5 声明:所有的文章demo都在我的仓库里 webpack5 起步 概念 webpack是用于编译JavaScript模块. 一个文件依赖另一个文件,包括静态资源(图片/css等),都会 ...
- day34 Pyhton 网络编程
一今日内容 # 函数 # 面向对象 # 进阶 # 网络编程 4 # 并发编程 6-7 # 概念 # 网络基础 # 局域网的概念 # 交换机和路由器的工作流程 # ip地址 # mac地址 # 子网掩码 ...
- centos8安装fastdfs6.06集群方式三之:storage的安装/配置/运行
一,查看本地centos的版本 [root@localhost lib]# cat /etc/redhat-release CentOS Linux release 8.1.1911 (Core) 说 ...