"Codeforces 987C" three displays

Source: Internet
Author: User

"Link" I am the link, point me:)
Test instructions

Exercises


Dynamic planning
Set Dp[i][j] represents the first I number, the minimum cost of J is selected.
Dp[i][j] = min (dp[k][j-1]+b[i]);//Where a[i]>a[k] and k<i
This is actually where the enumeration of the selected J-1 is located.
Obviously only in front of it, and satisfies a[i]>a[k] position k is possible.
Complexity O (n^2)

Code

 #include <bits/stdc++.h>using namespace std;const int N = 3e3;const int INF = 4e8;int N;int a[n+10],b[n+10];in T Dp[n+10][4];int Main () {#ifdef local_define freopen ("Rush.txt", "R", stdin); #endif//Local_define ios::sync_with_stdio (0), Cin.tie (0); CIN >> N; for (int i = 1;i <= n;i++) cin >> A[i]; for (int i = 1;i <= n;i++) cin >> B[i]; for (int i = 1;i <= 3;i++) for (int j = 1;j <= n;j++) dp[j][i] = INF; for (int i = 1;i <= n;i++) dp[i][1] = B[i]; for (int j = 2;j <= 3;j++) for (int i = J;i <= n;i++) for (int k = j-1;k <= i-1;k++) if (A[i]>a[k] && dp[i][j]>dp[k][j-1]+b[i]) {dp[i][j] = Dp[k][j-1]+b[i]; } int ans = INF; for (int i = 3;i <= n;i++) ans = min (ans,dp[i][3]); if (Ans==inf) cout<<-1<<endl; else cout<<ans<<endl; return 0;} 

"Codeforces 987C" three displays

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.