天天看點

Codeforces Round #363 (Div. 2) C Vacations

先爆搜一發,tle,然後改成記憶化搜尋,就一直wa

Codeforces Round #363 (Div. 2) C Vacations

http://blog.csdn.net/keyboarderqq/article/details/51960464

看了大佬代碼,發現我少寫一個狀态。。。相比dp,記憶化搜尋還是比較好寫的,思路也不難。

#include <bits/stdc++.h>
using namespace std;

const int MAXN = ;
int num[MAXN],n;
int dp[MAXN][];

int dfs(int cnt, int pre)
{

    if(cnt == n)
        return ;
    if(dp[cnt][pre] != -)
        return dp[cnt][pre];
    int ret = ;
    ret = min(ret,dfs(cnt+,)+);
    for(int i = ; i <= ; ++i)
    {
        if(i == pre) continue;
        if(!(i&num[cnt])) continue;
        ret = min(ret,dfs(cnt+,i));
    }
    return dp[cnt][pre] = ret;
}

int main()
{
    ios::sync_with_stdio(false);
    cin >>n;
    memset(dp,-,sizeof(dp));
    for(int i = ; i < n; ++i)
        cin >> num[i];
    cout << dfs(,) << endl;
    return ;
}