UVa 1608 Non-boring sequence (分治)
2017-08-16 14:50
465 查看
题目链接:https://vjudge.net/problem/UVA-1608
题意:给出一个整数序列,若序列的任意一个连续子序列都至少有一个只出现一次的元素,则该序列为不无聊序列。判断一个序列是否无聊。
思路:若某个元素在整个序列中只出现一次,则所有包含该元素的连续子序列都为不无聊序列。因此,找到这样一个元素后,只需判断该元素左边与右边的序列是否为无聊序列即可。 怎么找到这样的元素呢?可以对每个序列预先处理出其左边及其右边第一个和它相同的值得位置,这样就可以在O(1)的复杂度内判断了。
#include<cstdio>
#include<cstring>
#include<string>
#include<cctype>
#include<iostream>
#include<set>
#include<map>
#include<cmath>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<algorithm>
#define fin(a) freopen("a.txt","r",stdin)
#define fout(a) freopen("a.txt","w",stdout)
typedef long long LL;
using namespace std;
typedef pair<int, int> P;
const int INF = 1e8 + 10;
const int maxn = 1e5 + 10;
map<int, int> mp;
int a[maxn], n;
int pre[maxn], after[maxn];
bool judge(int L, int R, int p)
{
return pre[p] < L && after[p] > R;
}
bool solve(int L, int R)
{
if(L >= R) return true;
int i = L, j = R;
while(i <= j && (i <= R || j >= L))
{
if(i <= R && judge(L, R, i)) return solve(L, i-1) && solve(i+1, R);
if(j >= L && judge(L, R, j)) return solve(L, j-1) && solve(j+1, R);
i++, j--;
}
return false;
}
int main() {
int T;
scanf("%d", &T);
while(T--)
{
mp.clear();
scanf("%d", &n);
for(int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
if(mp.count(a[i]))
{
pre[i] = mp[a[i]];
after[mp[a[i]]] = i;
}
else
{
pre[i] = -1;
after[i] = n+1;
}
mp[a[i]] = i;
}
if(solve(1, n)) printf("non-boring\n");
a237
else printf("boring\n");
}
return 0;
}
题意:给出一个整数序列,若序列的任意一个连续子序列都至少有一个只出现一次的元素,则该序列为不无聊序列。判断一个序列是否无聊。
思路:若某个元素在整个序列中只出现一次,则所有包含该元素的连续子序列都为不无聊序列。因此,找到这样一个元素后,只需判断该元素左边与右边的序列是否为无聊序列即可。 怎么找到这样的元素呢?可以对每个序列预先处理出其左边及其右边第一个和它相同的值得位置,这样就可以在O(1)的复杂度内判断了。
#include<cstdio>
#include<cstring>
#include<string>
#include<cctype>
#include<iostream>
#include<set>
#include<map>
#include<cmath>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<algorithm>
#define fin(a) freopen("a.txt","r",stdin)
#define fout(a) freopen("a.txt","w",stdout)
typedef long long LL;
using namespace std;
typedef pair<int, int> P;
const int INF = 1e8 + 10;
const int maxn = 1e5 + 10;
map<int, int> mp;
int a[maxn], n;
int pre[maxn], after[maxn];
bool judge(int L, int R, int p)
{
return pre[p] < L && after[p] > R;
}
bool solve(int L, int R)
{
if(L >= R) return true;
int i = L, j = R;
while(i <= j && (i <= R || j >= L))
{
if(i <= R && judge(L, R, i)) return solve(L, i-1) && solve(i+1, R);
if(j >= L && judge(L, R, j)) return solve(L, j-1) && solve(j+1, R);
i++, j--;
}
return false;
}
int main() {
int T;
scanf("%d", &T);
while(T--)
{
mp.clear();
scanf("%d", &n);
for(int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
if(mp.count(a[i]))
{
pre[i] = mp[a[i]];
after[mp[a[i]]] = i;
}
else
{
pre[i] = -1;
after[i] = n+1;
}
mp[a[i]] = i;
}
if(solve(1, n)) printf("non-boring\n");
a237
else printf("boring\n");
}
return 0;
}
相关文章推荐
- UVA 1608 Non-boring sequence 不无聊的序列(分治,中途相遇)
- UVA 1608 Non-boring sequences (递归分治)
- UVa 1608 Non-boring sequences (分治)
- UVa 1608:Non-boring swquences(分治)
- 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(分治)
- 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
- 分治法,中途相遇法(Non-boring sequences,uva 1608)
- UVA - 1608 Non-boring sequences
- 【高效算法设计——递归】 UVa 1608 Non-boring sequences
- UVa 1608 Non-boring sequences