zoukankan      html  css  js  c++  java
  • UVA-1608

    We were afraid of making this problem statement too boring, so we decided to keep it short. A sequenceis called non-boring if its every connected subsequence contains a unique element, i.e. an element suchthat no other element of that subsequence has the same value.Given a sequence of integers, decide whether it is non-boring.InputThe first line of the input contains the number of test cases T. The descriptions of the test cases follow:Each test case starts with an integer n (1 ≤ n ≤ 200000) denoting the length of the sequence. Inthe next line the n elements of the sequence follow, separated with single spaces. The elements arenon-negative integers less than 109.OutputPrint the answers to the test cases in the order in which they appear in the input. For each test caseprint a single line containing the word ‘non-boring’ or ‘boring’.Sample Input451 2 3 4 551 1 1 1 151 2 3 2 151 1 2 1 1Sample Outputnon-boringboringnon-boringboring

    题意:就是判断一串数字的任意子序列是否满足至少有一个元素在该序列中只出现一次;题好理解,就是写有点难。

    题解:对于一组数在区间(l,r)中有一个数M只出现一次,则只需要判断(l,M-1)和(M+1,r)区间是否有这样的数就可以啦,如果找不到返回false。可以利用递归,首先将该数组中的数从其位置开始向左右遍历,找到其左右第一个与其位置相同的下标(可以利用map来查找)。

    AC代码为:

    #include <iostream>
    #include <algorithm>
    #include <map>
    #include <cstdio>
    #include<cstring>
    using namespace std;


    int a[200010];
    int l[200010], r[200010];
    int n;


    bool f(int left, int right)
    {
    if (left >= right)
    return true;
    for (int i = 0; i <= (right - left) / 2; i++)
    {
    if (l[left + i]<left && r[left + i]>right)
    return f(left, left + i - 1) && f(left + i + 1, right);
    if (l[right - i]<left && r[right - i]>right)
    return f(left, right - i - 1) && f(right - i + 1, right);
    }
    return false;
    }




    int main()
    {
    int T;
    scanf("%d", &T);
    while (T--)
    {
    map<int, int> m;
    scanf("%d",&n);
    for (int i = 0; i<n; i++)
    scanf("%d", a+i);

    m.clear();
    for (int i=0; i<n; i++)
    {
    if (m.count(a[i]))
    l[i] = m[a[i]];
    else
    l[i] = -2;
    m[a[i]] = i;
    }

    m.clear();
    for (int i = n - 1; i >= 0; i--)
    {
    if (m.count(a[i]))
    r[i] = m[a[i]];
    else
    r[i] = n;


    m[a[i]] = i;s
    }
    if (f(0,n-1))
    printf("non-boring ");
    else
    printf("boring ");
    }
    return 0;
    }

  • 相关阅读:
    [cf 947E] Perpetual Subtraction
    loj3120. 「CTS2019 | CTSC2019」珍珠
    loj「LibreOJ NOI Round #2」不等关系
    loj6395. 「THUPC2018」城市地铁规划 / City
    loj2553. 「CTSC2018」暴力写挂
    loj6270. 数据结构板子题
    loj6358. 前夕
    loj6677. EntropyIncreaser 与菱形计数
    fiddler模拟接口响应数据
    Fiddler请求详解/autoResponseder重定向
  • 原文地址:https://www.cnblogs.com/csushl/p/9386622.html
Copyright © 2011-2022 走看看