Hdu 5691 Sitting in line (pressure DP)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.