标签:log roman des pie lang should problem tin 二分
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 30430 | Accepted: 10183 |
Description
Input
Output
Sample Input
30 25 27 30 34 39 45 52 60 69 79 69 60 52 45 39 34 30 26 22 18 82 78 74 70 66 67 64 60 65 80 0
Sample Output
5
题目链接:POJ 1743
题意有点难懂,就是问你是否存在这样的串,是否至少在两个位置出现过且不重复覆盖的串,但是串里的数字不一定相同,只要通过整体增加K能相等即可,即1 2 3串与4 5 6串也是相同,只要前者每个数加3就变成了4 5 6,而且这样的串长度要大于等于5。
由于整体增加K的原因,可以用原串的$arr[i+1]-arr[i]$作为新串的b[i],然后这样就解决了未知数K的问题,然后进行二分答案,由于是不重复覆盖,因此把height大于等于二分答案mid的分为同一组,由于b串是前后的差值构成,因此相邻两项对应在原串实际是有覆盖关系的,因此要用最大值-最小值>mid判断。
代码:
#include <stdio.h> #include <iostream> #include <algorithm> #include <cstdlib> #include <cstring> #include <bitset> #include <string> #include <stack> #include <cmath> #include <queue> #include <set> #include <map> using namespace std; #define INF 0x3f3f3f3f #define LC(x) (x<<1) #define RC(x) ((x<<1)+1) #define MID(x,y) ((x+y)>>1) #define fin(name) freopen(name,"r",stdin) #define fout(name) freopen(name,"w",stdout) #define CLR(arr,val) memset(arr,val,sizeof(arr)) #define FAST_IO ios::sync_with_stdio(false);cin.tie(0); typedef pair<int, int> pii; typedef long long LL; const double PI = acos(-1.0); const int N = 20010; int wa[N], wb[N], cnt[N], sa[N]; int ran[N], height[N]; int temp[N], s[N]; inline int cmp(int r[], int a, int b, int d) { return r[a] == r[b] && r[a + d] == r[b + d]; } void DA(int n, int m) { int i, k; int *x = wa, *y = wb; for (i = 0; i < m; ++i) cnt[i] = 0; for (i = 0; i < n; ++i) ++cnt[x[i] = s[i]]; for (i = 1; i < m; ++i) cnt[i] += cnt[i - 1]; for (i = n - 1; i >= 0; --i) sa[--cnt[x[i]]] = i; for (k = 1; k <= n; k <<= 1) { int p = 0; for (i = n - k; i < n; ++i) y[p++] = i; for (i = 0; i < n; ++i) if (sa[i] >= k) y[p++] = sa[i] - k; for (i = 0; i < m; ++i) cnt[i] = 0; for (i = 0; i < n; ++i) ++cnt[x[y[i]]]; for (i = 1; i < m; ++i) cnt[i] += cnt[i - 1]; for (i = n - 1; i >= 0; --i) sa[--cnt[x[y[i]]]] = y[i]; swap(x, y); x[sa[0]] = 0; p = 1; for (i = 1; i < n; ++i) x[sa[i]] = cmp(y, sa[i - 1], sa[i], k) ? p - 1 : p++; m = p; if (p >= n) break; } } void gethgt(int n) { int i, k = 0; for (i = 1; i <= n; ++i) ran[sa[i]] = i; for (i = 0; i < n; ++i) { if (k) --k; int j = sa[ran[i] - 1]; while (s[i + k] == s[j + k]) ++k; height[ran[i]] = k; } } int check(int n, int mid) { int Max = -INF, Min = INF; for (int i = 1; i <= n; ++i) { if (height[i] >= mid) { Max = max(Max, max(sa[i], sa[i - 1])); Min = min(Min, min(sa[i], sa[i - 1])); if (Max - Min > mid) return 1; } else { Max = -INF; Min = INF; } } return 0; } int main(void) { int n, i; while (~scanf("%d", &n) && n) { for (i = 0; i < n; ++i) scanf("%d", &temp[i]); for (i = 0; i < n - 1; ++i) s[i] = temp[i + 1] - temp[i] + 88 ; s[n - 1] = 0; DA(n, 190); gethgt(n - 1); int L = 0, R = n / 2; int ans = -1; while (L <= R) { int mid = MID(L, R); if (check(n - 1, mid)) { L = mid + 1; ans = mid; } else R = mid - 1; } printf("%d\n", ans + 1 >= 5 ? ans + 1 : 0); } return 0; }
POJ 1743 Musical Theme(后缀数组+二分答案)
标签:log roman des pie lang should problem tin 二分
原文地址:http://www.cnblogs.com/Blackops/p/7499286.html