Poj3056the Bavarian beer party (no delivery pairing, interval DP)

Source: Internet
Author: User
Problem descriptionthe professors of the Bayerische mathematiker Verein have their annual party in the local biergarten. they are sitting at a round table each with his own pint of beer. as a ceremony each using sor raises his pint and toasts one of the other guests in such a way that no arms cross.

Figure 2: toasting internal ss a table with eight persons: no arms crossing (left), arms crossing (right)

We know that the professors like to toast with someone that is drinking the same brand of beer, and we like to maximize the number of pairs of professors toasting with the same brand, again without crossing arms. write an algorithm to do this, keeping in mind that every partition sor shocould take part in the toasting.
Inputthe frist line of the input contains a single number: the number of test cases to follow. Each test case has the following format:
One line with an even number P, satisfying 2 <= P <= 1000: the number of participants
One line with P integers (separated by single spaces) indicating the beer brands fro the consecutive professors (in clockwise order, starting at an arbitrary position ). each value is between 1 and 100 (boudaries encoded ).
Outputfor every test case in the input, the output shoshould contain a single number on a single line: the maximum number of non-Intersecting toasts of the same beer brand for this test case.
Sample Input
261 2 2 1 3 3221 7 1 2 4 2 4 9 1 1 9 4 5 9 4 5 6 9 2 1 2 9

Sample output
36

Question: If n people are surrounded by Zhuo, all people must give a toast to each other, and do not have any friendship, please find the maximum number of toast pairs.

Question: If we want to make all possible pairs and do not make any difference, the number of people in the interval must be an even number.

# Include <stdio. h> # include <string. h> int DP [2005] [2005]; int max (int A, int B) {return A> B? A: B;} int main () {int t, n, ANS [2005]; scanf ("% d", & T); While (t --) {scanf ("% d", & N); For (INT I = 1; I <= N; I ++) {scanf ("% d ", & Ans [I]) ;}for (INT I = 1; I <= N; I ++) for (Int J = 1; j <= N; j ++) DP [I] [J] = 0; For (INT r = 1; r <n; R + = 2) // ensure that the number of people in the interval is an even number, only an even number can be used to salute everyone for (INT I = 1; I <= N-R; I ++) {Int J = I + R; DP [I] [J] = DP [I + 1] [J-1]; // if there is no pair with I in the interval for (int K = I + 1; k <= J; k + = 2) // keep the even number of cells. If (ANS [I] = ans [k]) DP [I] [J] = max (DP [I] [J], DP [I + 1] [k-1] + 1 + dp [k + 1] [J]); else DP [I] [J] = max (DP [I] [J], DP [I] [k] + dp [k + 1] [J]);} printf ("% d \ n", DP [1] [N]);}


Poj3056the Bavarian beer party (no delivery pairing, interval 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.