博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
bzoj 1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列
阅读量:2143 次
发布时间:2019-04-30

本文共 2646 字,大约阅读时间需要 8 分钟。

Time Limit: 5 Sec  
Memory Limit: 64 MB
Submit: 821  
Solved: 324
[ ][ ][ ]

Description

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 = 13. Since 13 written in binary is 1101, this means our cow exhibits features 1, 3, and 4 (reading right to left), but not feature 2. More generally, we find a 1 in the 2^(i-1) place if a cow exhibits feature i. Always the sensitive fellow, FJ lined up cows 1..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.

N(1<=N<=100000)头牛,一共K(1<=K<=30)种特色,

每头牛有多种特色,用二进制01表示它的特色ID。比如特色ID为13(1101),
则它有第1、3、4种特色。[i,j]段被称为balanced当且仅当K种特色在[i,j]内
拥有次数相同。求最大的[i,j]段长度。

Input

* Line 1: Two space-separated integers, N and K.

* Lines 2..N+1: Line i+1 contains a single K-bit integer specifying the features present in cow i. The least-significant bit of this integer is 1 if the cow exhibits feature #1, and the most-significant bit is 1 if the cow exhibits feature #K.

Output

* Line 1: A single integer giving the size of the largest contiguous balanced group of cows.

Sample Input

7 3
7
6
7
2
1 4
2

Sample Output

4

求出所有特色的前缀和,然后将相邻的两种特色做差

对于段[x, y],如果x处所有特色的差和y处相等,就说明K种特色在[x, y]内拥有次数相同

用Hash或者map都行

#include
#include
#include
#include
using namespace std;#define mod 998244356#define LL long longint sum[100005][33], a[100005], S[33];map
p;int main(void){ LL p1; int n, i, j, k, ans; scanf("%d%d", &n, &k); for(i=1;i<=n;i++) scanf("%d", &a[i]); k -= 1; for(i=1;i<=n;i++) { for(j=0;j<=k;j++) { sum[i][j] = sum[i-1][j]; if(a[i]&(1<
=1;j--) sum[i][j] = sum[i][j]-sum[i][j-1]; } for(i=1;i<=k;i++) S[i] = rand()+1; p1 = 1; for(j=1;j<=k;j++) p1 = p1*S[j]%mod; p[p1] = 0; ans = 0; for(i=1;i<=n;i++) { p1 = 1; for(j=1;j<=k;j++) p1 = (p1*S[j]+sum[i][j])%mod; if(p.count(p1)==0) p[p1] = i; else ans = max(ans, i-p[p1]); } printf("%d\n", ans); return 0;}/*3 31 2 1*/

转载地址:http://oqmgf.baihongyu.com/

你可能感兴趣的文章
(模板 重要)Tarjan算法解决LCA问题(PAT 1151 LCA in a Binary Tree)
查看>>
(PAT 1154) Vertex Coloring (图的广度优先遍历)
查看>>
(PAT 1115) Counting Nodes in a BST (二叉查找树-统计指定层元素个数)
查看>>
(PAT 1143) Lowest Common Ancestor (二叉查找树的LCA)
查看>>
(PAT 1061) Dating (字符串处理)
查看>>
(PAT 1118) Birds in Forest (并查集)
查看>>
数据结构 拓扑排序
查看>>
(PAT 1040) Longest Symmetric String (DP-最长回文子串)
查看>>
(PAT 1145) Hashing - Average Search Time (哈希表冲突处理)
查看>>
(1129) Recommendation System 排序
查看>>
PAT1090 Highest Price in Supply Chain 树DFS
查看>>
(PAT 1096) Consecutive Factors (质因子分解)
查看>>
(PAT 1019) General Palindromic Number (进制转换)
查看>>
(PAT 1073) Scientific Notation (字符串模拟题)
查看>>
(PAT 1080) Graduate Admission (排序)
查看>>
Play on Words UVA - 10129 (欧拉路径)
查看>>
mininet+floodlight搭建sdn环境并创建简答topo
查看>>
【linux】nohup和&的作用
查看>>
Set、WeakSet、Map以及WeakMap结构基本知识点
查看>>
【NLP学习笔记】(一)Gensim基本使用方法
查看>>