Uva 1608 Non-boring sequences

思路和紫书一样,首先记录每个数字前面和该数字一样的下标,以及后面与该数字一样的下标,然后从0到n-1的区间开始判断,如果能够找到以某个唯一出现的数字为中心点,左边的序列满足条件并且右边的序列也满足条件,那么最后就能得出该序列是“non boring”的,如果找不到这样的一个点,那么就直接返回false,利用递归操作,实现的难度不大,具体实现见如下代码:

#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<deque>
#include<functional>
using namespace std;

const int maxn = 200000+10;

int data[maxn], pre[maxn], nt[maxn];

bool compare(int cur,int L,int R){
	return pre[cur]<L&&nt[cur]>R;
}

bool getRes(int L,int R){
	if (L >= R) return true;
	for (int length = 0; L + length <= R - length; length++){
		if (compare(L + length, L, R))
			return getRes(L, L + length - 1) && getRes(L + length + 1, R);
		if (L + length == R - length) break;
		if (compare(R - length, L, R))
			return getRes(L, R - length - 1) && getRes(R - length + 1, R);
	}
	return false;
}

int main(){
	int T;
	cin >> T;
	while (T--){
		int n;
		cin >> n;
		map<int, int> record;
		record.clear();
		for (int i = 0; i < n; i++){
			cin >> data[i];
			if (record.find(data[i]) == record.end()) pre[i] = -1;
			else pre[i] = record[data[i]];
			record[data[i]] = i;
		}
		record.clear();
		for (int i = n - 1; i >= 0; i--){
			if (record.find(data[i]) == record.end()) nt[i] = n;
			else nt[i] = record[data[i]];
			record[data[i]] = i;
		}
		if (getRes(0, n - 1)) cout << "non-boring\n";
		else cout << "boring\n";
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值