Bzoj1806&&vijos p1386 (Recursive)

Source: Internet
Author: User

Links: Click to open link

Test instructions: There are two coal mines, each employing a group of miners. Coal mining work is very hard, so the miners need a good diet. Whenever a food truck arrives at a coal mine, the miners produce a certain amount of coal. There are three types of food carts: meat carts, fish carts and vans.
The miners like changing recipes. If the supply of food can change constantly, the amount of coal they produce will increase. Whenever a new food truck arrives at a coal mine, the miners compare the new food with the first two (or less than two times, if the food is delivered less than two times before) and:
If the food carts are all of the same type of food, the miners produce a unit of coal.
If there are two different types of food in these food carts, the miners produce two units of coal.
If there are three different types of food in the food carts, the miners produce three units of coal.
Pre-known types of food carts and the order in which they are shipped. By determining which car food to send to which coal can affect the amount of coal. Food carts cannot be split, and each food cart must be delivered to one or another coal mine. Two coal mines are also not required to receive the same number of food carts (in fact, all food carts are also allowed to be delivered to a coal mine).
Task
Given the type of food carts and the order in which they are delivered, you are required to write a program that determines which food vehicle should be sent to the coal mine 1, which food vehicle should be sent to the coal mine 2, so that the total amount of coal produced in two coal mines is the largest.

Code:

#include <set> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream
> #include <algorithm> using namespace std;
Char s[100005];
int dp[2][4][4][4][4];
    int change (char c) {if (c== ' M ') return 1;
    if (c== ' F ') return 2;
return 3;
    } int cal (int a,int b,int c) {//judgment three has several set<int> s;
    if (a!=0) S.insert (a);
    if (b!=0) S.insert (b);
    if (c!=0) S.insert (c);
return S.size ();             } int main () {//dp[i][a][b][c][d] represents to the letter I, the first mine int n,i,j,a,b,c,d,ans,tmp,tmp_c;
        The last two are A, B, the last two of the second mine is the maximum value of the c,d while (scanf ("%d", &n)!=eof) {scanf ("%s", s+1);
        Memset (Dp,-1,sizeof (DP));
        dp[0][0][0][0][0]=0;
            for (i=1;i<=n;i++) {memset (dp[i%2],-1,sizeof (dp[i%2])); for (a=0;a<4;a++) {//with scroll array for (b=0;b<4;b++) {for (C=0;C&L t;4;c++) {for (d=0;d<4;d++) {if (dp[(i-1)%2][a][b][c][d]==-1)
                            Continue
                            Tmp_c=change (S[i]); Tmp=cal (A,b,tmp_c); When transferred, push backwards, and the last two become the top two Dp[i%2][b][tmp_c][c][d]=max (dp[i%2][b][tmp_c][c][d],dp[(i-1)%2][a][b][c
                            ][D]+TMP);
                            Tmp=cal (C,d,tmp_c);
                        Dp[i%2][a][b][d][tmp_c]=max (dp[i%2][a][b][d][tmp_c],dp[(i-1)%2][a][b][c][d]+tmp);
        }}}}} ans=0; for (a=0;a<4;a++) for (b=0;b<4;b++) for (c=0;c<4;c++) for (d=0;d<4;d++) Ans=max (an
        S,DP[N%2][A][B][C][D]);
    printf ("%d\n", ans);
} return 0;
 }

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.