Luogu P2690 pick up apples

Source: Internet
Author: User
Tags stdin

Topic background

Usaco

Title Description

Few people know that cows love apples. Farmer John has two apple trees on his farm (numbered 1 and 2), and each tree is covered with apples. Bessie could not pluck the apples from the tree, so she could only wait for the apples to fall from the trees. But as the apples fell to the ground, Bessie would have to catch the apples in midair (no one would like to eat the smashed apples). Bessie was quick to eat, and she was able to finish it in just a few seconds after receiving the apple. Every minute, one of the two apple trees will drop an apple. Bessie had been trained enough to catch the apple falling from the tree as long as she stood under the tree. At the same time, Bessie was able to move quickly between two trees (moving for far less than 1 minutes), so when the apples fell, she would stand under one of the two trees. In addition, cows are reluctant to keep going between two trees, so they miss some apples. The Apples drop one per minute, a total of T (1<=t<=1000) minutes, and Bessie is willing to move W (1<=w<=30) a maximum number of times. Now gives the number of trees that drop apples per minute, asking for the maximum number of apples that Bessie can catch. At first, Bessie was under tree 1th.

Input/output format

Input format:

The first line is 2 digits, T and K. The next T-line, one number per line, represents the moment the T-Apple falls from the No. 1th Apple tree or the 2nd apple tree.

Output format:

For each test point, the output line, one number, is the maximum number of apples the cow receives.

Input/Output sample Input Sample # #:
7 22112211
Sample # # of output:6 Problem Solving Ideas:

The main idea: This problem is a very simple memory of the search topic.

We can think of this: there are only two decisions at a time, that is, changing positions or not changing.

We have to know which of the apples we can get the most, and which one we choose.

In this way, we can see the sub-problems of the problem.

We can use dp[i][j][k] to indicate at the first time, the conversion of the J-position, now in K, the maximum number of apples to get?

The equation of state transition comes out: Dp[i][j][k]=max (Dp[i+1][j+1][3-k],dp[i+1][j][k]).

But be aware that if the current point can eat apples, the number of apples will have to add one.

//This is a memory search! #include <bits/stdc++.h>using namespacestd;intRead () {intret=0, ok=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9'){if(ch=='-') ok=-1; Ch=GetChar ();} for(; ch>='0'&&ch<='9'; ch=GetChar ()) RET=ret*Ten+ch-'0';returnret*OK;}intt,k;inta[5500];intdp[2500][2500][3];inlineintDfsintTimeintSumintNow//time is now, sum is changed several times, now is the current position{    if(time>t| | SUM&GT;K)//if out of bounds    {        return 0; }    if(dp[time][sum][now]!=0)//If a solution has been obtained    {        returnDp[time][sum][now]; } Dp[time][sum][now]=max (DFS (time+1, sum+1,3-now), DFS (time+1, Sum,now));//The former is changed position, the latter is not changed    if(A[time]==now)//if the current position at this time can eat apples{Dp[time][sum][now]++;//Apple number plus one    }    returnDp[time][sum][now];//return}intMain () {//freopen ("apple.in", "R", stdin);//freopen ("Apple.out", "w", stdout);T=read (), k=read (); for(intI=1;i<=t; i++) {A[i]=read ();} DFS (1,0,1); cout<<dp[1][0][1]; return 0;}
//this is DP#include <bits/stdc++.h>using namespacestd;intRead () {intret=0, ok=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9'){if(ch=='-') ok=-1; Ch=GetChar ();} for(; ch>='0'&&ch<='9'; ch=GetChar ()) RET=ret*Ten+ch-'0';returnret*OK;}inta[ the];intt,k;intdp[2500][3];intMain () {//freopen ("apple.in", "R", stdin);//freopen ("Apple.out", "w", stdout);T=read (), k=read (); for(intI=1; i<=t;i++) {A[i]=read ();} for(intI=1; i<=t;i++){     for(intj=k;j>=0; j--)    {        if(j>0) {Dp[j][a[i]]=max (dp[j][a[i]]+1, dp[j-1][3-a[i]]+1); }        Else if(a[i]==1) Dp[j][a[i]]++; }}cout<<max (dp[k][1],dp[k][2]); return 0;}

Luogu P2690 pick up apples

Related Article

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.