[模拟] 数列重组(牛客+库函数+模拟+暴力dfs)

32 篇文章 0 订阅
4 篇文章 0 订阅

1. 题目来源

链接:数列重组

在这里插入图片描述

2. 题目解析

暴力枚举全排列,再暴力判断三段是否有序即可。

  • 全排列:有序数组下,采用 next_permutation(q.begin(), q.end())
  • 判断有序:is_sorted(a.begin(), a.end(), cmp) 其默认为升序排序,支持自定义排序。

常见简化代码的操作。枚举将数组分成三段,就枚举分界点即可,但是要注意,区间一定是左开右闭的,若是 [1,1) 其实里面是没有数的,is_sorted() 在此时会判断成功,显然是错误的。


  • 时间复杂度 O ( n 2 n ! ) O(n^2n!) O(n2n!),不确定…
  • 空间复杂度 O ( n ) O(n) O(n)

代码:
自己写的繁琐的模拟代码

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

const int n = 10;
int a[10] = {2, 3, 3, 3, 5, 6, 6, 7, 7, 8};

bool cmp(int a, int b) {
    return a > b;
}

int main() {
    sort(a, a + n);

    int ans = 0;
    do {
        bool ok = false;
        for (int i = 0; i <= 7; i ++ ) {
            for (int j = i + 1; j <= 8; j ++ ) {
                if ((is_sorted(a, a+i+1) || is_sorted(a, a+i+1, cmp)) &&
                    (is_sorted(a+i+1, a+j+1) || is_sorted(a+i+1, a+j+1, cmp)) && 
                    (is_sorted(a+j+1, a+n) || is_sorted(a+j+1, a+n, cmp))) {
                        ok = true;
                        break;
                    }
            }
            if (ok) break;
        }
        if (ok) ans ++ ;
    } while (next_permutation(a, a + n));

    cout << ans << endl;
    return 0;
}

dfs 判断三段有序序列,参考学习!俗称暴力~

#include <iostream>
#include <algorithm>
#include <unordered_set>

using namespace std;

int a[10] = {2, 3, 3, 3, 5, 6, 6, 7, 7, 8};

string get()
{
    string s;
    for(int i = 0;i < 10;i ++)
        s += to_string(a[i]);
    return s;
}

bool dfs(string& s, int u, int c)
{
    if(c > 3) return false;
    if(u >= 10 && c <= 3) return true;
    int k = u;
    while(k + 1 < 10 && a[k] <= a[k + 1]) k ++;
    if(dfs(s, k +1, c + 1)) return true;
    
    k = u;
    while(k + 1 < 10 && a[k] >= a[k + 1]) k ++;
    if(dfs(s, k + 1, c + 1)) return true;
    return false;
}

int main()
{
    unordered_set<string> S;
    int res = 0;
    do {
        string s = get();
        if(S.count(s) == 0)
        {
           // cout << s << endl;
            if(dfs(s, 0, 0)) 
                S.insert(s);
        }
    }while(next_permutation(a, a +10));
    
    cout << S.size() << endl;
    
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ypuyu

如果帮助到你,可以请作者喝水~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值