codeforces 607B(区间dp)

    xiaoxiao2022-07-03  130

    题目链接:https://codeforc.es/problemset/problem/607/B

    思路:模板题,枚举一下断点,区间左右端点相同更新一下就好

    #include <cstdio> #include <cstdlib> #include <cassert> #include <cstring> #include <bitset> #include <cmath> #include <cctype> #include <iostream> #include <algorithm> #include <string> #include <vector> #include <queue> #include <map> #include <set> #include <sstream> #include <iomanip> using namespace std; typedef long long ll; typedef vector<long long> VI; typedef unsigned long long ull; const ll inff = 0x3f3f3f3f3f3f3f3f; #define FOR(i,a,b) for(int i(a);i<=(b);++i) #define FOL(i,a,b) for(int i(a);i>=(b);--i) #define SZ(x) ((long long)(x).size()) #define REW(a,b) memset(a,b,sizeof(a)) #define inf int(0x3f3f3f3f) #define si(a) scanf("%d",&a) #define sl(a) scanf("%lld",&a) #define sd(a) scanf("%lf",&a) #define ss(a) scanf("%s",a) #define mod ll(998244353) #define pb push_back #define eps 1e-6 #define lc d<<1 #define rc d<<1|1 #define Pll pair<ll,ll> #define P pair<int,int> #define pi acos(-1) int n,a[508],dp[508][508]; int dfs(int l,int r) { if(l>r) return 1; if(dp[l][r]) return dp[l][r]; if(l==r) return dp[l][r]=1; int res=inf; if(a[l]==a[r]) res=min(res,dfs(l+1,r-1)); for(int k=l;k<r;k++) res=min(res,dfs(l,k)+dfs(k+1,r)); return dp[l][r]=res; } int main() { cin.tie(0); cout.tie(0); cin>>n; FOR(i,1,n) si(a[i]); cout<<dfs(1,n)<<endl; return 0; }

     

    最新回复(0)