1717: [Usaco2006 Dec]Milk Patterns 产奶的模式
1717: [Usaco2006 Dec]Milk Patterns 产奶的模式
Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 952 Solved: 519
[Submit][Status][Discuss]
Description
农夫John发现他的奶牛产奶的质量一直在变动。经过细致的调查,他发现:虽然他不能预见明天产奶的质量,但连续的若干天的质量有很多重叠。我们称之为一个“模式”。 John的牛奶按质量可以被赋予一个0到1000000之间的数。并且John记录了N(1<=N<=20000)天的牛奶质量值。他想知道最长的出现了至少K(2<=K<=N)次的模式的长度。比如1 2 3 2 3 2 3 1 中 2 3 2 3出现了两次。当K=2时,这个长度为4。
Input
* Line 1: 两个整数 N,K。
* Lines 2..N+1: 每行一个整数表示当天的质量值。
Output
* Line 1: 一个整数:N天中最长的出现了至少K次的模式的长度
Sample Input
1
2
3
2
3
2
3
1
Sample Output
HINT
Source
Gold
[Submit][Status][Discuss]
这才是后缀数组的模板。。 sa、rank、height数组都出现了 先构建一发后缀数组 二分ans,,, 在height数组上,任意两个后缀的最长公共前缀就是它们之间所有height取个min 所以,,,如果存在,应该是连续一段,,贪心瞎搞之 bulabulabula
#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<bitset>
#include<algorithm>
#include<cstring>
#include<map>
#include<stack>
#include<set>
#include<cmath>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;const int maxn = 2E4 + 20;int n,T,cur = 1,a[maxn],c[maxn*100],t[maxn],t2[maxn],sa[maxn],rank[maxn],height[maxn];void Getsa()
{int *x = t,*y = t2,m = 1000000;for (int i = 1; i <= n; i++) ++c[x[i] = a[i]];for (int i = 2; i <= m; i++) c[i] += c[i-1];for (int i = n; i; i--) sa[c[x[i]]--] = i;for (int k = 1; k < n; k <<= 1) {int p = 0;for (int i = n; i > n - k; i--) y[++p] = i;for (int i = 1; i <= n; i++) if (sa[i] - k > 0) y[++p] = sa[i] - k;for (int i = 1; i <= m; i++) c[i] = 0;for (int i = 1; i <= n; i++) ++c[x[y[i]]];for (int i = 2; i <= m; i++) c[i] += c[i-1];for (int i = n; i; i--) sa[c[x[y[i]]]--] = y[i];p = 1; swap(x,y); x[sa[1]] = 1;for (int i = 2; i <= n; i++)x[sa[i]] = y[sa[i]] == y[sa[i-1]] && y[sa[i]+k] == y[sa[i-1]+k]?p:++p;if (p >= n) return;m = p;}
}void Rank_and_Height()
{for (int i = 1; i <= n; i++) rank[sa[i]] = i;int k = 0;for (int i = 1; i <= n; i++) {if (k) --k;int j = sa[rank[i] - 1];while (a[i+k] == a[j+k]) ++k;height[rank[i]] = k;}
}bool Judge(int now)
{int tot = 0;for (int i = 1; i <= n; i++) {if (height[i] < now) {if (tot + 1 >= T) return 1;tot = 0; continue;}++tot;}return tot + 1 >= T;
}int main()
{#ifdef DMCfreopen("DMC.txt","r",stdin);#endifcin >> n >> T;for (int i = 1; i <= n; i++) scanf("%d",&a[i]);Getsa();Rank_and_Height();int L,R; L = 0,R = n;while (R - L > 1) {int mid = (L + R) >> 1;if (Judge(mid)) L = mid;else R = mid;}if (Judge(R)) cout << R;else cout << L;return 0;
}
1717: [Usaco2006 Dec]Milk Patterns 产奶的模式相关推荐
- 后缀数组2.0--Height数组(bzoj 1717: [Usaco2006 Dec]Milk Patterns 产奶的模式)
前置技能:后缀数组 height[i]:排名第i的后缀与排名第i-1的后缀的最长公共前缀,也就是sa[i]和sa[i-1]的最长公共前缀 h[i]:以第i个字符为起点的后缀与排名在它前1名的后缀的最长 ...
- BZOJ 1717 [Usaco2006 Dec]Milk Patterns 产奶的模式(后缀数组)
[题目链接]http://www.lydsy.com/JudgeOnline/problem.php?id=1717 [题目大意] 求一个最长的串,使得其在母串中出现的次数达到要求 [题解] 二分答案 ...
- bzoj1717 [Usaco2006 Dec]Milk Patterns 产奶的模式
先求后缀数组.相邻两个后缀的最长公共前缀 二分答案,o(n)check 1 #include<algorithm> 2 #include<iostream> 3 #includ ...
- [bzoj1717][Usaco2006 Dec]Milk Patterns 产奶的模式 (hash构造后缀数组,二分答案)
以后似乎终于不用去学后缀数组的倍增搞法||DC3等blablaSXBK的方法了= = 定义(来自关于后缀数组的那篇国家集训队论文..) 后缀数组:后缀数组SA是一个一维数组,它保存1..n的某个排列S ...
- P2852 [USACO06DEC]Milk Patterns G
题目描述 Farmer John has noticed that the quality of milk given by his cows varies from day to day. On f ...
- bzoj 1650: [Usaco2006 Dec]River Hopscotch 跳石子(二分)
1650: [Usaco2006 Dec]River Hopscotch 跳石子 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 721 Solved: ...
- bzoj 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐(暴力DFS)
1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 774 Solved: 480 ...
- 乳牛悲惨的一生 -----奶牛为什么会产奶?
http://www.douban.com/group/topic/17241189/ 奶牛为什么怀孕呢? --因为,因为她被人工受精了!奶牛为什么会不断地产奶呢?因为她不断地生小牛.奶牛为什么不断地 ...
- POJ3261 Milk Patterns 【后缀数组】
牛奶模式 时间限制: 5000MS 内存限制: 65536K 提交总数: 16796 接受: 7422 案件时间限制: 2000MS 描述 农夫约翰已经注意到,他的牛奶的质量每天都在变化.经进 ...
- [POJ3261] Milk Patterns
LINK 此题的常规做法是 二分 + 后缀数组 ,但本蒟蒻还是习惯写 并查集 的做法 算法流程 1.离散化是肯定要有的,给的数据太大了,不离散化会RE 2.先跑一遍SA,把最重要的h数组求出来 3.把 ...
最新文章
- MyBatis中使用流式查询避免数据量过大导致OOM
- Webpack 代码分离
- iframe父子页面间通信总结
- express 框架中的参数小记
- keil debug时用断点管理抓取变量变化
- 记一次clickhouse查询问题Double-distributed IN/JOIN subqueries is denied (distributed_product_mode = ‘deny‘)
- 2018年智能音箱对比
- hbase regions_使用Regions ADF 11g进行Master Detail CRUD操作
- python解常微分方程_Python-sympy.dsolve求解常微分方程(组)
- PushMe(交互式)
- 苹果iOS手机暗藏间谍软件的揭秘者:扎徳尔斯基
- c语言指针 r,C语言指针的高级操作
- 【Alpha】Scrum Meeting 1
- 范式哈夫曼编码(Canonical Huffman Code)
- Win11系统使用DISM命令备份驱动程序的方法
- Everything软件配置
- 确定有限自动机DFA非确定有限自动机NFA
- CentOS 5 全功能WWW服务器搭建全教程 V3.0 【转】
- 外行转it_外行人员的微服务容器
- 借助栈将一个带头节点的单链表倒置