-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathInsanely Hard School Exams.cpp
119 lines (83 loc) · 2.65 KB
/
Insanely Hard School Exams.cpp
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
long long int fun(long long int num,int colour,vector<long long int> lines_set[]){
vector<long long int> lines = lines_set[colour];
for(long long int i=0;i<lines.size() && num>0 ;i++){
long long int temp = min(lines[i],num);
lines[i] = lines[i] - temp;
num = num - temp;
}
if(num>0){
return 0;
}
long long int sum1 = 0;
long long int k = lines.size();
for (int i=0; i<k; i++)
sum1 += lines[i];
long long int sum2 = 0;
long long int temp[k+1];
for (long long int i=0; i<k; i++)
{
temp[i] = lines[i]*(sum1-lines[i]);
sum2 += temp[i];
}
sum2 /= 2;
long long int sum3 = 0;
for (long long int i=0; i<k; i++)
sum3 += lines[i]*(sum2-temp[i]);
sum3 /= 3;
return sum3;
}
int main() {
long long int t;
cin>>t;
while(t--){
long long int N,C,K;
cin>>N>>C>>K;
unordered_map<long long int,int> colour[C+1];
vector<long long int> lines[C+1];
for(int i=1;i<=N;i++){
long long int a,b,col;
cin>>a>>b>>col;
colour[col][a]++;
}
long long int V[C+1]={0};
for(long long int i=1;i<=C;i++){
cin>>V[i];
}
for(long long int i=1;i<=C;i++){
vector<long long int> lines_set;
for(auto it : colour[i]){
lines_set.push_back(it.second);
}
sort(lines_set.begin(),lines_set.end());
lines[i] = lines_set;
}
long long int dp[K+1][C+1];
long long int fun_mem[K+1][C+1];
for(int i=0;i<=K;i++){
for(long long int j=0;j<=C;j++){
dp[i][j] = LLONG_MAX;
if(j==0){
dp[i][j]=0;
}
fun_mem[i][j] = -1;
}
}
for(long long int i=0;i<=K;i++){
for(long long int j=1;j<=C;j++){
long long int max_lines_rem = i/V[j];
for(long long int k=0;k<=max_lines_rem;k++){
long long int val_rem = k*V[j];
if(fun_mem[k][j]==-1){
fun_mem[k][j] = fun(k,j,lines);
}
dp[i][j] = min(dp[i][j] , dp[i-val_rem][j-1] + fun_mem[k][j] );
}
}
}
cout<<dp[K][C]<<"\n";
}
return 0;
}