Dynamic programming-cows pick apples

Source: Internet
Author: User

Reference: http://blog.sina.com.cn/s/blog_61034ad90100encg.html

F[I][J] Indicates that I move J times in the case, f[i][j] = Max (F[i-1][j], f[i-1][j-1]) + int (A[i] = = (j + 1)% 2) indicates that in this case, received the most Apple for the case at the last moment, this does not move And the maximum number of moves, plus whether the current position is under the new fruit tree.

  

Study.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <iostream> #include <vector> #include <unordered_map> #include < unordered_set> #include <queue> #include <string> #include <algorithm> #include <sstream># Include <set> #include <stack> #define INT_MAX 2147483647//Maximum (Signed) INT value#define int_min (-214748 3647-1)//Minimum (signed) int value, #define MIN (x) < (y)? (x):(Y) using namespace Std;int N, K, f[1004][34] = {0}, a[1004], ans = 0;int Max (int a, int b) {return a > B? a:b;} void Init () {cin >> n >> k;for (int i = 1; I <= n; i++) {cin >> a[i]; A[i]%= 2;} The number of the processing tree. }void DP () {int I, J, t;for (i = 1; I <= n; i++) {f[i][0] = f[i-1][0] + int (a[i] = = 1);} Initializes a 0-time jump. for (i = 1; l <= N; i++) for (j = 1; J <= min (i, k), J + +)//j<=i; {//t = f[i-1][j];//if (f[i-1][j-1]>t) t = f[i-1][j-1];//f[i][j] = t + int (a[i] = = (j + 1)% 2); F[i][j] = Max (f [I-1] [j], F[i-1][j- 1]); F[i][j] + = Int (A[i] = = (j + 1)% 2);} for (i = 1; I <= n; i++) {for (j = 1; J <= min (i, k); + j)//j<=i;{ cout << F[i][j] << "";} cout << Endl;} for (i = 0; I <= k; i++) if (f[n][i]>ans) ans = f[n][i];cout << ans;} int main () {init ();dp(); system ("pause"); return 0;}

  

Dynamic programming-cows pick 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.