例题8-16 不无聊的序列(Non-boring sequences, CERC 2012, UVa1608)

思路:
若序列中存在某个只出现一次的元素,横跨此元素左右的序列必然ok,只需考虑左右序列。
由此T(n) = T(k-1) + T(n - k) + T(find唯一元素)。
预处理出每个元素左右相同元素的位置,则可以以T(find) = O(n)。
优化查找方式,每次从序列的左右两端同时查找,最坏情况T(n) = O(nlogn)。
orz.真的好困难,步履维艰。。。
两种不同的查找方式在最坏情况下的复杂度不同,涨姿势了。。。
#include <set>
#include <map>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <deque>
#include <cstdio>
#include <string>
#include <vector>
#include <cctype>
#include <sstream>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <algorithm>
#define SF(a) scanf("%d", &a)
#define PF(a) printf("%d\n", a)  
#define SFF(a, b) scanf("%d%d", &a, &b)  
#define SFFF(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define SFFFF(a, b, c, d) scanf("%d%d%d%d", &a, &b, &c, &d)
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define IN() freopen("in.txt", "r", stdin)
#define OUT() freopen("out.txt", "w", stdout)
#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define LL long long
#define maxn 200005
#define maxm 205
#define mod 1000000007
#define inf 1000000007
#define eps 1e-4
using namespace std;
int buf[20], l;
int read() {
	int x = 0; char ch = getchar(); bool f = 0;
	while (ch < '0' || ch > '9') { if (ch == '-') f = 1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}
void write(int x) {
	if (!x) { putchar(48); return; }
	l = 0; if (x < 0) putchar('-'), x = -x;
	while (x) buf[++l] = x % 10, x = x / 10;
	while (l) putchar(buf[l--] + 48);
}
//-------------------------chc------------------------------//
int n, a[maxn], low[maxn], high[maxn];
map<int, int> Map;

void pre() {
	Map.clear();
	FOR(i, 0, n) {
		if (!Map.count(a[i])) low[i] = -1;
		else low[i] = Map[a[i]];
		Map[a[i]] = i;
	}
	Map.clear();
	for (int i = n - 1; i >= 0; --i) {
		if (!Map.count(a[i])) high[i] = n;
		else high[i] = Map[a[i]];
		Map[a[i]] = i;
	}
}

int find_unique(int l, int r) {
	int i = l, j = r - 1;
	while (i <= j) {
		if (low[i] < l && high[i] >= r) return i;
		if (low[j] < l && high[j] >= r) return j;
		++i, --j;
	}
	return -1;
}

bool solve(int l, int r) {
	if (r - l <= 1) return true;
	int pos = find_unique(l, r);
	if (pos == -1) return false;
	else return solve(l, pos) && solve(pos + 1, r);
}

int main() {
	int t = read();
	while (t--) {
		n = read();
		FOR(i, 0, n) a[i] = read();
		pre();
		puts(solve(0, n) ? "non-boring" : "boring");
	}
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值