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

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

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<stdio.h>
#include<map>
#include<stdlib.h>
#include<algorithm>
using namespace std;
#define mod 998244356
#define LL long long
int sum[100005][33], a[100005], S[33];
map<LL, int> 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<<j))sum[i][j]++;}}for(i=1;i<=n;i++){for(j=k;j>=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;elseans = max(ans, i-p[p1]);}printf("%d\n", ans);return 0;
}
/*
3 3
1 2 1
*/

bzoj 1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列相关推荐

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

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

  2. BZOJ1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列

    n<=100000个数表示每头牛在K<=30种物品的选取情况,该数在二进制下某位为0表示不选1表示选,求一个最大的区间使区间内选择每种物品的牛一样多. 数学转化,把不同状态间单变量的关系通 ...

  3. POJ3274 Gold Balanced Lineup【Hash函数】

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

  4. [BZOJ] 1637: [Usaco2007 Mar]Balanced Lineup

    1637: [Usaco2007 Mar]Balanced Lineup Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 697  Solved: 463 ...

  5. bzoj 1637: [Usaco2007 Mar]Balanced Lineup

    1637: [Usaco2007 Mar]Balanced Lineup Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 689  Solved: 456 ...

  6. BZOJ 1639: [Usaco2007 Mar]Monthly Expense 月度开支( 二分答案 )

    直接二分答案然后判断. ----------------------------------------------------------------------------- #include&l ...

  7. bzoj 1638: [Usaco2007 Mar]Cow Traffic 奶牛交通(拓扑排序?+DP)

    1638: [Usaco2007 Mar]Cow Traffic 奶牛交通 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 971  Solved: 37 ...

  8. bzoj 1639: [Usaco2007 Mar]Monthly Expense 月度开支(二分)

    1639: [Usaco2007 Mar]Monthly Expense 月度开支 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 1057  Solve ...

  9. bzoj 1704: [Usaco2007 Mar]Face The Right Way 自动转身机

    1704: [Usaco2007 Mar]Face The Right Way 自动转身机 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 457  So ...

最新文章

  1. 中山大学计算机学院运动会,喜讯!我院获2019中大校运会教工组团体第二名
  2. (收藏)Html相关网址
  3. 一个在线编写前端代码的好玩的工具
  4. mvc的视图中显示DataTable的方法
  5. TensorFlow2.0:单层感知机梯度计算
  6. matlab中sqp的算法原理_利用fmincon函数中的SQP算法求解非线性规划问题,模型本身.......
  7. weblogic 找不到数据源问题
  8. linux 安装pyaudio
  9. 关于双硬盘电脑掉盘的问题
  10. 校友故事|我在科大感受理工科“严谨的浪漫主义”
  11. Jenkins平台搭建
  12. python 转盘 圆形,用python实现一个转盘
  13. pyinstaller 打包exe启动慢的问题
  14. 夕阳美,美到骨子里了
  15. 设置Word文档密码的两种方式
  16. Chromium for windows (2015)
  17. 如何复制那些无法复制的网站
  18. 鸿蒙os电脑界面,鸿蒙OS平板UI界面曝光:双Dock栏设计+超级终端互联功能
  19. Authentication token manipulation error问题解决
  20. Fortran中go to语句的罕见用法

热门文章

  1. python基础知识-python基础知识整理(值得收藏)
  2. python画折线图-python绘制简单折线图代码示例
  3. python经典程序实例-Python简单基础小程序的实例代码
  4. python发音模块-python 利用pyttsx3文字转语音
  5. python必背入门代码-初学Python必背手册
  6. 自学python考哪些证书-Python自学难吗?有哪些课程内容?
  7. 【链表】判断链表是否有环
  8. 最简单的视音频播放示例1:总述
  9. ffplay播放器移植VC的工程:ffplay for MFC
  10. ActionScript 3.0 API 中的 Video 类