uva 1608(分治 + 中途相遇法)

来源:互联网 发布:midaspro3离线编辑软件 编辑:程序博客网 时间:2024/05/20 00:51

参考紫书,利用stl map,找到数组中相同元素的关系。每找到一个所求范围唯一元素,对元素所在的范围进行分治。

注意:不能用prev和next作数组名会使juge无法判断

#include<cstdio>#include<map>using namespace std;const int maxn = 200000 + 5;int num[maxn], pre[maxn], nex[maxn];map<int, int> cur;inline bool arry_unique(int p, int L, int R){    //printf("%d %d,%d\n", p, pre[p],nex[p]);    return (pre[p] < L && nex[p] > R);}bool check(int L, int R){    if(L >= R) return true;    for(int d = 0; L + d <= R - d; d++)    {        //printf("%d\n", d);        if(arry_unique(L + d, L, R))            return check(L, L + d - 1) && check(L + d + 1, R);        if(L + d == R - d) break;        if(arry_unique(R - d, L, R))            return check(L, R - d -1) && check(R - d + 1, R);    }    return false;}int main(){    int T;    scanf("%d", &T);    while(T--)    {        cur.clear();        int n;        scanf("%d", &n);        for(int i = 0; i < n; i++)            scanf("%d", &num[i]);        for(int i = 0; i < n; i++)        {            if(!cur.count(num[i])) pre[i] = -1;            else {                pre[i] = cur[num[i]];            }            cur[num[i]] = i;        }    cur.clear();        for(int i = n - 1; i >= 0; i--)        {            if(!cur.count(num[i])) nex[i] = n;            else{                nex[i] = cur[num[i]];            }            cur[num[i]] = i;        }        int L = 0, R = n - 1;        //continue;        if(check(L, R)) printf("non-boring\n");        else printf("boring\n");    }    return 0;}