暴搜——51nod1400 序列分解

題面:51nod1400
真的是大暴力
有人說直接暴搜加一個小剪枝就能過。。。
我的搜索策略有點奇怪


先講一個錯誤的貪心:
我們開個數組q[i]記錄一個子序列的狀態
用一個指針p表示另一個子序列目前匹配到q[i]的第幾位
然後匹配下一位的時候往後找到和這一位數值相同的最近位置
然後匹配上就好了,複雜度O(n)
但是這是錯的,比如這組數據:

8
1 1 4 3 1 1 4 3

這就不行了
但是我們不要直接匹配上去,有兩條路可以走,一是匹配,二是繼續在前面一個串跟上去
這樣就有兩種策略了,搜索即可
時間複雜度O(2n2)
反正過了

#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <ctime>
#include <map>
#include <queue>
#include <cstdlib>
#include <string>
#include <climits>
#include <set>
#include <vector>
using namespace std;
inline int read(){
    int k=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){k=k*10+ch-'0';ch=getchar();}
    return k*f;
}
int a[10001],q[10001],n;
inline bool dfs(int l,int p,int r){
    if(p==l)q[++l]=a[r++];
    if(l>n/2)return 0;
    while(q[p+1]!=a[r]){
        q[++l]=a[r];r++;
        if(r>n)return 0;
    }
    if(r==n&&p+1==l)return 1;
    if(dfs(l,p+1,r+1))return 1;
    else{
        q[l+1]=a[r];
        return dfs(l+1,p,r+1);
    }
}
int main()
{
    int T=read();while(T--){
        n=read();
        for(int i=1;i<=n;i++)a[i]=read();
        q[1]=a[1];
        puts(dfs(1,0,2)?"Good job!!":"What a pity!");
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章