-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolve.cpp
More file actions
31 lines (30 loc) · 868 Bytes
/
Copy pathsolve.cpp
File metadata and controls
31 lines (30 loc) · 868 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
#include <bits/stdc++.h>
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
int main(){
IOS
int t;
cin >> t;
while(t--){
int n, m, arr[205], dp[205][205];
cin >> n >> m;
for(int i = 0;i < 205;i++){
for(int j = 0;j < 205;j++){
dp[i][j] = INF;
}
}
for(int i = 0;i < n;i++) cin >> arr[i], dp[i][i] = 1;
for(int l = 1;l < n;l++){
for(int i = 0, j = i + l;j < n;i++, j++){
if(arr[i] == arr[j] || arr[j -1] == arr[j]) dp[i][j] = dp[i][j - 1];
for(int k = i;k < j;k++){
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j]);
}
}
}
cout << dp[0][n - 1] << '\n';
}
return 0;
}