-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCC-CHEFWED_DP.cpp
More file actions
90 lines (80 loc) · 2.29 KB
/
CC-CHEFWED_DP.cpp
File metadata and controls
90 lines (80 loc) · 2.29 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//This code is written by Shammi Anand
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define Max(a,b,c) max(a,max(b,c))
#define Min(a,b,c) min(a,min(b,c))
#define pb push_back
#define mp make_pair
#define ers clear
#define f first
#define s second
#define mod 1000000007
#define nl "\n"
#define w(x) int x; cin>>x; while(x--)
void shammi() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
//minimum cost to reach index j when last partition was at index i : state variables!
int dp[101][1001];
int freq[101] = {0};
int main() {
shammi();
w(t) {
memset(dp, 0, sizeof(dp));
memset(freq, 0, sizeof(freq));
int n, k; cin >> n >> k;
int Family[n + 1];
int mn = 1000000000;
for (int i = 0; i < n; i++) cin >> Family[i];
int adj[n + 1][n + 1];
memset(adj, 0, sizeof(adj));
// form the adj matrix!
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
if (j == 0) {
adj[i][j] = 0;
} else adj[i][j] = adj[i][j - 1];
if (freq[Family[j]]) {
// if adding for first time cost increase by 2
if (freq[Family[j]] == 1) adj[i][j] += 2;
else adj[i][j]++;
}
freq[Family[j]]++;
}
//set the values of freq arr to zero
memset(freq, 0, sizeof(freq));
}
// cost to reach index i when partition was at 1
for (int i = 1; i < n + 1; i++) {
dp[1][i] = adj[0][i - 1];
}
// cost 1 for different partitions
for (int i = 2; i < 101; i++) { // is it necessary?
dp[i][1] = 0;
}
//calculate all possibilities!
for (int i = 2; i < 101; i++) {
for (int j = 2; j <= n; j++) {
int curr_min = 1e9;
// find the best cost to reach index (1.....j) at cost i
for (int sub_pos = 1; sub_pos < j; sub_pos++) {
//calculate for every sub_pos;
curr_min = min(curr_min, dp[i - 1][sub_pos] + adj[sub_pos][j - 1]);
}
//store the minumun cost to reach at index j with i partitions at dp[i][j];
dp[i][j] = curr_min;
}
}
// calculate the absolute minimum from the stored values!
for (int i = 1; i < 101; i++) {
mn = min(i * k + dp[i][n], mn);
}
cout << mn << endl;
}
return 0;
}