1#include <bits/stdc++.h>2 #defineLLD I64d3 using namespacestd;4typedefLong Longll;5 intCalintx) {6 intRET =0;7 while(x) {8 if(X &1) RET + +;9X >>=1;Ten } One returnret; A } - Const intN = -; - Const intINF =0x7f7f7f7f; the intdp[1<< -][n]; - inta[n],p[n],bits[1<<N]; - /** - * Dp[i][j]: Indicates the optimal value of state I, last number is a[j] + * - * A[n] = 0 + * Dp[0][n] = 0 A * dp[i | (1<<k)] [K] = max (Dp[i | (1 << k)] [K],dp[i][j] + a[j] * a[k]); at * */ - intMain () { - intN,t,kase =1; -scanf"%d",&T); - while(t--){ -scanf"%d",&n); in for(inti =0; I < n; i + +){ -scanf"%d%d",&a[i],&p[i]); to } + for(inti =0; I < (1<< N); i + +){ - for(intj =0; J <= N; J + +){ theDP[I][J] =-INF; * } $ }Panax Notoginseng for(inti =0; I < (1<< N); i + +) bits[i] =cal (i); -A[n] = dp[0][n] =0; the for(inti =0; I < (1<< N); i + +){ + for(intj =0; J <= N; J + +){ A if(Dp[i][j]! =-INF) { the for(intK =0; K < n; K + +){ + if((I & (1<< k) = =0&& (p[k] = =-1|| P[k] = =Bits[i])) { - //State transitions: $Dp[i | (1<<k)][k] = max (Dp[i | (1<< k)][k],dp[i][j] + a[j] *a[k]); $ } - } - } the } - }
cout << "dp[1][n": "<< dp[1][n" << Endl; -infWuyi intAns =-INF; the for(inti =0; I <= N; i + +) ans = max (ans,dp[(1<<n)-1][i]); -printf"Case #%d:\n%d\n", kase++, ans); Wu } -}
cout << "dp[1][n": "<< dp[1][n" << Endl; Dp[1][n] =-inf
P[K] = = 1 = a[k] arbitrary put, p[k] = = Bits[i], said A[k] to be placed in the bits[i] a
* A[n] = 0
DP[1][1] = 0
DP[11][2]
DP[101][3]
DP[1001][4]
DP[10][2] = 0
....
DP[100][3] = 0
DP[1000][4] = 0
Hdu 5691 Sitting in line (pressure DP)