luogu 1360 阵容均衡(前缀和+差分+hash)
要求一段最大的区间里每个能力的增长值是一样的。
我们首先求一遍前缀和,发现,如果区间内[l,r]每个能力的增长值是一样的话,那么前缀和[r]和[l-1]的差分也应该是一样的。
那么我们把前缀和的差分hash一下,排个序,求出hash值相同的且跨越最远的两个端点,就是答案了。
# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <set>
# include <cmath>
# include <algorithm>
using namespace std;
# define lowbit(x) ((x)&(-x))
# define pi acos(-1.0)
# define eps 1e-
# define MOD
# define INF
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define bug puts("H");
# define lch p<<,l,mid
# define rch p<<|,mid+,r
# define mp make_pair
# define pb push_back
typedef pair<int,int> PII;
typedef vector<int> VI;
# pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long LL;
int Scan() {
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
void Out(int a) {
if(a<) {putchar('-'); a=-a;}
if(a>=) Out(a/);
putchar(a%+'');
}
const int N=;
//Code begin... int a[N], wei[N][];
struct Node{int hash, id;}node[N]; bool comp(Node a, Node b){
if (a.hash!=b.hash) return a.hash<b.hash;
return a.id<b.id;
}
int main ()
{
int n, m, x;
scanf("%d%d",&n,&m);
FOR(i,,n) {
scanf("%d",&x);
int num=;
while (x) wei[i][++num]=x%, x/=;
FOR(j,,m) wei[i][j]+=wei[i-][j];
}
FOR(i,,n) {
int val=;
FOR(j,,m) val=val*+wei[i][j]-wei[i][j-];
node[i].hash=val; node[i].id=i;
}
sort(node,node+n+,comp);
int ans=, l, r;
FOR(i,,n) {
if (i==||node[i].hash!=node[i-].hash) l=node[i].id;
if (i==n||node[i].hash!=node[i+].hash) r=node[i].id, ans=max(ans,r-l);
}
printf("%d\n",ans);
return ;
}
luogu 1360 阵容均衡(前缀和+差分+hash)的更多相关文章
- [LuoguP1360][USACP07MAR]黄金阵容均衡
[LuoguP1360][USACP07MAR]黄金阵容均衡(Link) 每天会增加一个数\(A\),将\(A\)二进制分解为\(a[i]\),对于每一个\(i\)都增加\(a[i]\),如果一段时间 ...
- 洛谷 P1360 [USACO07MAR]黄金阵容均衡Gold Balanced L…
P1360 [USACO07MAR]黄金阵容均衡Gold Balanced L… 题目描述 Farmer John's N cows (1 ≤ N ≤ 100,000) share many simi ...
- Codeforces 479E Riding in a Lift:前缀和/差分优化dp
题目链接:http://codeforces.com/problemset/problem/479/E 题意: 有一栋n层的房子. 还有一个无聊的人在玩电梯,每次玩电梯都会从某一层坐到另外一层. 他初 ...
- 洛谷P1360 [USACO07MAR]黄金阵容均衡Gold Balanced L…
P1360 [USACO07MAR]黄金阵容均衡Gold Balanced L… 题目描述 Farmer John's N cows (1 ≤ N ≤ 100,000) share many simi ...
- 北京区域赛I题,Uva7676,A Boring Problem,前缀和差分
转载自https://blog.csdn.net/weixin_37517391/article/details/83821752 题解 其实这题不难,只要想到了前缀和差分就基本OK了. 我们要求的是 ...
- [CSP-S模拟测试]:stone(结论+桶+前缀和+差分)
题目描述 $Cab$有两行石子,每个石子上有一个字母,为$'C''A''B'$中的一个.一开始,在每行第一个石子上站着一只$lucky$,$Cab$每次可以选择一个字母,使得所站石子上字母为该字母的$ ...
- HDU-6514 Monitor(二维前缀和+差分)
http://acm.hdu.edu.cn/showproblem.php?pid=6514 Problem Description Xiaoteng has a large area of land ...
- [USACO07MAR]黄金阵容均衡Gold Balanced L…(洛谷 1360)
题目描述 Farmer John's N cows (1 ≤ N ≤ 100,000) share many similarities. In fact, FJ has been able to na ...
- [USACO07MAR]黄金阵容均衡Gold Balanced L…
https://www.luogu.org/problem/show?pid=1360 题目描述 Farmer John's N cows (1 ≤ N ≤ 100,000) share many s ...
随机推荐
- Using Xpath With Default XML Namespace in C#
If you have a XML file without any prefix in the namespace: <bookstore xmlns="http://www.con ...
- day4 边缘检测Canny
1.canny边缘检测 # coding=utf-8 import cv2 import numpy as np filename = 'woman.JPEG' #读入图像,以灰度格式 img = c ...
- iOS 影音新格式 HEIF HEVC
苹果在 iOS 11 的发布会上,推出了两种新的媒体格式 HEIF HEVC,都是为了保证画质的情况下,大大减少视频.照片的大小. 一.简介 HEVC全称 High Efficiency Video ...
- pager-taglib分页注意事项
必须先导包,尤其是 jsp 这种工具类和标签库的
- R Language Learn Notes
One #install package install.packages("ggplot2") #load library library(ggplot2) #update.pa ...
- NumPy v1.15手册汉化
NumPy参考 数组创建 零 和 一 empty(shape[, dtype, order]):返回给定形状和类型的新数组,而不初始化条目 empty_like(prototype[, dtype, ...
- 180716-Centos时区设置
使用timedatectl命令同步时间并设置时区 I. timedatactl命令 1. 使用帮助 timedatectl -h 2. 命令示例 2.1 显示系统的当前时间和日期 timedatect ...
- 配置文件语言之yaml
一. Yaml YAML 是一种简洁的非标记语言.YAML以数据为中心,使用空白,缩进,分行组织数据,从而使得表示更加简洁易读. 由于实现简单,解析成本很低,YAML特别适合在脚本语言中使用.列一下现 ...
- 微软职位内部推荐-Senior Software Engineer II-Sharepoint
微软近期Open的职位: SharePoint is a multi-billion dollar enterprise business that has grown from an on-prem ...
- 美国警察iPhone数据线挡住歹徒子弹获救
泡泡网手机频道11月1日 现在手机的功能越来越丰富,不仅可以接打电话.收发短信.玩游戏聊天,关键时刻还能救命.前天HTC手机再次忠心护主,让许多同学对HTC赞赏有加.而现在又有人捡了一条命,不过这次救 ...