-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path607B.cpp
More file actions
51 lines (47 loc) · 980 Bytes
/
607B.cpp
File metadata and controls
51 lines (47 loc) · 980 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//21171_somesh || asomesh999
//AC
#include<bits/stdc++.h>
using namespace std;
#define fr(i,a,b) for(long long i=a;i<b;i++)
#define io ios::sync_with_stdio(false);cin.tie(NULL);
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
long int mod=1000000007;
int n;
vi arr;
int dp[510][510];
int rec(int left, int right) {
if (left == right) {
return 1;
}
if (left > right) {
return 0;
}
int &ans = dp[left][right];
if (ans != -1) return ans;
ans = (1e9 + 10);
for (int i = right; i > left; i--) {
if (arr[left] == arr[i]) {
if (left + 1 == i)
ans = min(ans, rec(left + 2, right) + 1);
else
ans = min(ans, rec(left + 1, i - 1) + rec(i + 1, right));
}
}
ans = min(ans, rec(left + 1, right) + 1);
return ans;
}
int main()
{
io
cin >> n;
arr.resize(n);
fr(i, 0, n) cin >> arr[i];
memset(dp, -1, sizeof dp);
cout << rec(0, n - 1);
return 0;
}