File tree Expand file tree Collapse file tree 1 file changed +65
-0
lines changed Expand file tree Collapse file tree 1 file changed +65
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+ #define ms (x,v ) memset((x),v,sizeof (x))
3
+ #define INF 0x3f3f3f3f
4
+ using namespace std ;
5
+
6
+ const int MAXN = 100 +10 ;
7
+ typedef long long LL;
8
+
9
+
10
+
11
+ void solve (){
12
+ int N,K;
13
+ cin >> N >> K;
14
+
15
+ vector<vector<int > > dp (N,vector<int >(K+1 ,INF));
16
+ vector<vector<int > > num (N,vector<int >(N,0 ));
17
+ vector<int > h (N);
18
+ for (int i=0 ; i<N ; ++i)cin >> h[i];
19
+
20
+ for (int i=0 ; i<N ; ++i){
21
+ unordered_map<int ,int > cnt;
22
+ int max_value = -1 ;
23
+ for (int j=i ; j<N ; ++j)
24
+ {
25
+ cnt[h[j]]++;
26
+ if (cnt[max_value] < cnt[h[j]])
27
+ max_value = h[j];
28
+ num[i][j] = j -i +1 - (cnt[max_value]);
29
+ }
30
+ }
31
+
32
+ for (int i=0 ; i<N ; ++i)
33
+ {
34
+ dp[i][0 ] = num[0 ][i];
35
+ for (int k=1 ; k<=K ; ++k){
36
+ if (k >=i)
37
+ dp[i][k] =0 ;
38
+ for (int j=1 ; j<=i ; ++j)
39
+ dp[i][k] = min (dp[i][k], dp[j-1 ][k-1 ]+num[j][i]);
40
+ }
41
+ }
42
+ cout << dp[N-1 ][K] << " \n " ;
43
+ }
44
+
45
+
46
+
47
+
48
+ int main (){
49
+ ios :: sync_with_stdio (0 );
50
+ cin.tie (0 );
51
+ // cout.tie(0);
52
+ std::cout.precision (10 );
53
+ std::cout.setf ( std::ios::fixed, std:: ios::floatfield );
54
+
55
+ int T;
56
+ cin >> T;
57
+ for (int tt =1 ; tt <=T ; ++tt)
58
+ {
59
+ cout << " Case #" << tt << " : " ;
60
+ solve ();
61
+ }
62
+
63
+
64
+ return 0 ;
65
+ }
You can’t perform that action at this time.
0 commit comments