• UVa 1608 Non-boring sequences (分治)


    题意:给你一个长度为n序列,如果这个任意连续子序列的中都有至少出现一次的元素,那么就称这个序列是不无聊的,判断这个序列是不是无聊的。

    析:首先如果整个序列中有一个只出过一次的元素,假设是第 p 个,那么我就可以看他左边和右边的序列是不是不无聊,也就是判断 1~p-1  和 p+1 ~ n,这可以用分治来进行处理。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #include <sstream>
    #include <list>
    #include <assert.h>
    #include <bitset>
    #define debug() puts("++++");
    #define gcd(a, b) __gcd(a, b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define fi first
    #define se second
    #define pb push_back
    #define sqr(x) ((x)*(x))
    #define ms(a,b) memset(a, b, sizeof a)
    #define sz size()
    #define pu push_up
    #define pd push_down
    #define cl clear()
    #define all 1,n,1
    #define FOR(x,n)  for(int i = (x); i < (n); ++i)
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 1e20;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 200000 + 10;
    const LL mod = 1e9 + 7;
    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, 1, 0, -1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline bool is_in(int r, int c) {
      return r > 0 && r <= n && c > 0 && c <= m;
    }
    
    int a[maxn];
    int l[maxn], r[maxn];
    map<int, int> mp;
    
    bool dfs(int L, int R){
      if(L >= R)  return true;
      int t = R - L >> 1;
      for(int i = 0; i <= t; ++i){
        if(l[i+L] < L && r[i+L] > R)  return dfs(L, i+L-1) && dfs(i+1+L, R);
        if(l[R-i] < L && r[R-i] > R)  return dfs(L, R-i-1) && dfs(R-i+1, R);
      }
      return false;
    }
    
    
    int main(){
      int T;
      cin >> T;
      while(T--){
          scanf("%d", &n);
        mp.cl;
        for(int i = 1; i <= n; ++i){
          scanf("%d", a+i);
          if(mp.count(a[i]))  l[i] = mp[a[i]];
          else  l[i] = 0;
          mp[a[i]] = i;
        }
        mp.cl;
        for(int i = n; i; --i){
          if(mp.count(a[i]))  r[i] = mp[a[i]];
          else r[i] = n+1;
          mp[a[i]] = i;
        }
        puts(dfs(1, n) ? "non-boring" : "boring");
      }
      return 0;
    }
    

      

  • 相关阅读:
    【Java异常】idea 报错:无效的目标发行版:17 的解决办法
    springboot实现MyBatis分页
    已解决No converter for XXX with preset ContentType ‘application/vnd.msexcel;charset=utf8‘
    mybatis if标签判断boolean等于true或者flase
    Java实现Excel导入和导出
    文件上传报错:Current request is not a multipart request的解决办法
    二维数组内的元素获取交集
    .Net Core MemoryCache 缓存
    SQL Server调用OLE对象
    SQL Server中使用正则表达式
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/7430124.html
Copyright © 2020-2023  润新知