Non-boring sequences UVA - 1608
2017-08-17 19:06
447 查看
题目传送门
题意:给你长度为n的一个序列,如果这个序列的任何一个连续子序列都有至少一个出现了一次的数字就说这个序列是一个不无聊的序列,反之就是一个无聊的序列。
思路:这个题就是求出来每一个数字所在位置离他左右最近的相同的数字,然后进行搜索就可以了,从两边开始向中间搜索。
PS:这个题目遇到一个奇怪的问题,这个做法的复杂度应该是O(nlogn),但是花了2000+ms,我就一直在找原因,然后最后发现原因竟然是用memset把数组的所有元素都赋值为-1的问题,把memset改成了循环时间就只剩400ms了。
题意:给你长度为n的一个序列,如果这个序列的任何一个连续子序列都有至少一个出现了一次的数字就说这个序列是一个不无聊的序列,反之就是一个无聊的序列。
思路:这个题就是求出来每一个数字所在位置离他左右最近的相同的数字,然后进行搜索就可以了,从两边开始向中间搜索。
PS:这个题目遇到一个奇怪的问题,这个做法的复杂度应该是O(nlogn),但是花了2000+ms,我就一直在找原因,然后最后发现原因竟然是用memset把数组的所有元素都赋值为-1的问题,把memset改成了循环时间就只剩400ms了。
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <fstream> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #define MAXN 200010 #define MAXE 210 #define INF 1000000010 #define MOD 1000000007 #define LL long long #define pi acos(-1.0) using namespace std; int arr[MAXN]; int PRE[MAXN]; int NEXT[MAXN]; typedef pair<int, int> P; map<int, int> num; bool check(int l, int r) { if (l >= r) return true; int left = l, right = r; while (left <= right) { if (NEXT[left] > r && PRE[left] < l) { return check(l, left - 1) && check(left + 1, r); } if (NEXT[right] > r && PRE[right] < l) { return check(l, right - 1) && check(right + 1, r); } left++; right--; } return false; } int main() { std::ios::sync_with_stdio(false); int T; cin >> T; for (int kase = 1; kase <= T; ++kase) { num.clear(); int n; cin >> n; for (int i = 0; i < n; ++i) { cin >> arr[i]; } for (int i = 0; i <= n; ++i) { NEXT[i] = n + 1; PRE[i] = -1; } for (int i = 0; i < n; ++i) { if (num.count(arr[i])) { PRE[i] = num[arr[i]]; NEXT[num[arr[i]]] = i; num[arr[i]] = i; } else { num[arr[i]] = i; } } if (check(0, n - 1)) cout << "non-boring\n"; else cout << "boring\n"; } return 0; } /* 4 5 1 2 3 4 5 5 1 1 1 1 1 5 1 2 3 2 1 5 1 1 2 1 1 */
相关文章推荐
- CSU-ACM2017暑期训练3 J - Non-boring sequences UVA - 1608
- UVa 1608:Non-boring swquences(分治)
- Uva 1608 Non-boring sequences
- uva 1608 Non-boring sequences
- UVA 1608 Non-boring sequence 不无聊的序列(分治,中途相遇)
- uva 1608 Non-boring sequences
- UVA 1608:Non-boring sequences(递归+分治思想)
- UVA - 1608 Non-boring sequences : 分治
- UVa 1608 Non-boring sequences
- Uva 1608 Non-boring sequences(分治)
- 分治法,中途相遇法(Non-boring sequences,uva 1608)
- UVA 1608 Non-boring sequences
- UVA 1608 Non-boring sequences 递归分治
- UVA 1608 Non-boring sequence
- UVA - 1608 Non-boring sequences 贪心+map + 往中间靠
- UVA 1608 Non-boring sequences
- UVa 1608 (分治 中途相遇) Non-boring sequences
- UVa 1608 Non-boring sequences (分治)
- Uva-1608 Non-boring sequences(高效率算法)
- UVA 1608 Non-boring sequences (递归分治)