You are viewing a single comment's thread. Return to all comments →
// C++ solution for those who stuck in DP
using namespace std; ll n, k; ll dp[20][1024]; ll fun(ll start, vec> &cost, ll avail) { if(start == n) { return 0; } if(dp[start][avail] != -1) { return dp[start][avail]; } ll ans = -1; for(ll end = start; end < n; end++) { for(ll j = 0; j < k; j++) { if((1ll << j) & avail) { ll tmp = fun(end+1, cost, avail^(1ll<= 0) { if(ans == -1) { ans = cost[j][end] - (start > 0 ? cost[j][start-1] : 0) + tmp; } else { ans = min(ans, cost[j][end] - (start > 0 ? cost[j][start-1] : 0) + tmp); } } } } } return dp[start][avail] = ans; } int main() { cin >> n >> k; vec> a(k, vec(n)); for(ll i = 0; i < k; i++) { cin >> a[i][0]; for(ll j = 1; j < n; j++) { cin >> a[i][j]; a[i][j] += a[i][j-1]; } } memset(dp, -1, sizeof(dp)); cout << fun(0, a, (1ll<
Seems like cookies are disabled on this browser, please enable them to open this website
The Blacklist
You are viewing a single comment's thread. Return to all comments →
// C++ solution for those who stuck in DP
include
define ll long long
define ld long double
define vec vector
using namespace std; ll n, k; ll dp[20][1024]; ll fun(ll start, vec> &cost, ll avail) { if(start == n) { return 0; } if(dp[start][avail] != -1) { return dp[start][avail]; } ll ans = -1; for(ll end = start; end < n; end++) { for(ll j = 0; j < k; j++) { if((1ll << j) & avail) { ll tmp = fun(end+1, cost, avail^(1ll<= 0) { if(ans == -1) { ans = cost[j][end] - (start > 0 ? cost[j][start-1] : 0) + tmp; } else { ans = min(ans, cost[j][end] - (start > 0 ? cost[j][start-1] : 0) + tmp); } } } } } return dp[start][avail] = ans; } int main() { cin >> n >> k; vec> a(k, vec(n)); for(ll i = 0; i < k; i++) { cin >> a[i][0]; for(ll j = 1; j < n; j++) { cin >> a[i][j]; a[i][j] += a[i][j-1]; } } memset(dp, -1, sizeof(dp)); cout << fun(0, a, (1ll<