HDU 5691--sitting in line —————— "shaped pressure gauge"

Source: Internet
Author: User

Sitting in line

Time limit:10000/5000 MS (java/others) Memory limit:262144/262144 K (java/others)
Total submission (s): 597 Accepted Submission (s): 274


Problem description degree bears are the greatest mathematicians of his time, and all figures are to be obeyed. Now, it's time for the Bears and his digital servants to play row and row games. The rules of the game are very simple, the n-integers participating in the game will be made into a row, they will continue to exchange their position, eventually achieve all the next two products and the largest goal, the number of participating games have integers and negative numbers. Degree bear in order to show his authority in front of his digital servant, he stipulated that certain numbers could be placed in a seated position, and that the numbers that were not restricted by the degree of bear were free to exchange positions.

Input first line an integerTSaidTGroup data.
Each set of test data is read in the following format from the standard input:

N

a1p1

a2p2

:

anPn

First line, integern(1≤n≤) That represents the number of integers participating in the game.

From the second line to the first(N+1) Lines, two integers per line,ai(−10000≤ai≤10000) 、Pi(pi=−1 Or0≤pi<N) , separated by a space.ai Represents the value of the number that participates in the game,pI Represents the degree to which the bear is specified for the number of positions, if pi=−1, the position that represents the number is not limited. The degree of the bear guarantees that no two digits will be assigned the same position.

Output first line: "Case #i:".IOn behalf of sectionIGroup test data.

The second line outputs the sum of the largest of all adjacent two numbers after the number is rearranged, i.e.MAX{A1⋅A2+A2⋅A3+ . +an−1< Span id= "mathjax-span-164" class= "Mo" >⋅an} 。

Sample input26-1 02 1-3 24 3-5 46 5540-150-130-120-110-1

Sample outputcase #1: -70case #2:4600

Source2016 "Baidu Star"-Preliminary (Astar round2a)

Problem-Solving ideas: state compression. Use dp[i][j] to indicate when the number represented by the I state is selected, the maximum and at the end of the first J number. Transfer equation: dp[i| ( 1<<k)][k] = max (dp[i| ( 1<<k)][k], Dp[i][j] + a[i]*a[j]).

#include <stdio.h> #include <algorithm> #include <string.h> #include <math.h> #include < string> #include <iostream> #include <queue> #include <stack> #include <map> #include < Vector> #include <set>using namespace std;typedef long long LL; #define MID (L+r)/2#define Lson rt*2,l,mid#define Rson Rt*2+1,mid+1,r#pragma COMMENT (linker, "/stack:102400000,102400000") const int MAXN = 1e5+300;const LL INF = 100000000 0000;typedef Long long ll;typedef unsigned long long ull;int a[30], p[30];int CHOICE[MAXN];    LL dp[maxn][30];int cal (int x) {int ret = 0;        while (x) {if (x&1) ret++;    x = x>>1; } return ret;}    int main () {int T, n, cas = 0;    scanf ("%d", &t);    Preprocessing out number I binary how many 1, because in the fixed position number, need to ensure that there are so many 1 for (int i = 0; I <= (1<<16); i++) {Choice[i] = cal (i);        } while (t--) {scanf ("%d", &n);     for (int i = 0; i < n; i++) {scanf ("%d%d", &a[i],&p[i]);   } for (int i = 0; I <= (1<<n), i++) {for (int j = 0; J <= N; j + +) {Dp[i][j]            =-inf;   }} A[n] = 0;   Just to initialize dp[0][n] = 0; Just to initialize for (int i = 0; i < (1<<n), i++) {for (int j = 0; J <= N; j + +) {if (DP I [j]!=-inf) {for (int k = 0; k < n; k++) {if (i& (1<<k)) = = 0&&am p; (p[k] = =-1 | | P[k] = = Choice[i]) {dp[i| ( 1<<k)][k] = max (dp[i| (                        1&LT;&LT;K)][k],dp[i][j] + a[j]*a[k]);        }}}}} LL ans =-inf;        for (int i = 0; i < n; i++) {ans = max (ans, dp[(1<<n) -1][i]);        } printf ("Case #%d:\n", ++cas);    printf ("%lld\n", ans); } return 0;}

  

HDU 5691--sitting in line —————— "shaped pressure gauge"

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.