"Codeforces Round #493 (Div. 2) B" cutting

Source: Internet
Author: User

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


Enter the test instructions here

Exercises


Obviously only in the first I position odd even occurrences of the same place can be cut.
(and no matter how the front is cut, here can be cut.)
Then the equivalent of n items, each item cost is |a[i]-a[i-1]|, then the value is 1.
Ask you what is the maximum value of the cost of not exceeding B.
This translates into a 01 backpack model.
Or you can write it that way.
\ (Dp[i][j] Indicates the first I item selected (cut) the minimum cost of J)
Then Dp[i][j]=min (Dp[i-1][j-1]+cost[i],dp[i-1][j]), or//For each of the cut I and not cut the first I.
Then traverse the dp[n][i from large to small]; the output is the largest so that the dp[n][i]<=b I can

Code

#include <bits/stdc++.h>using namespace Std;const int N = 100;const int INF = 1e8;//dp[i][j] First I position, cut the minimum cost of J times//dp[i ][j] = min (dp[i-1][j-1]+cost[i],dp[i-1][j]); int Dp[n+10][n+10],n,b;int Pre[n+10][2],a[n+10],cost[n+10],cnt;int Main    () {#ifdef local_define freopen ("Rush.txt", "R", stdin);    #endif//Local_define ios::sync_with_stdio (0), Cin.tie (0);    CIN >> N >> B;    for (int i = 1;i <= n;i++) cin >> A[i];        for (int i = 1;i <= n;i++) {for (int j = 0;j < 2;j++) Pre[i][j] = Pre[i-1][j];    pre[i][a[i]&1]++;        } for (int i = 1;i <= n-1;i++) if (pre[i][0]==pre[i][1]) {cost[++cnt] = ABS (A[i+1]-a[i]);    } n = cnt;    for (int i = 0;i <= n;i++) for (int j = 0;j <= n;j++) dp[i][j] = INF;    Dp[0][0] = 0;            for (int i = 1;i <= n;i++) for (int j = 0;j <= i;j++) {Dp[i][j] = dp[i-1][j];//no cut I if (j>0) {dp[i][j] = min (dp[i][J],dp[i-1][j-1]+cost[i]);//cut i}} for (int j = n;j >= 0;j--) {if (dp[n][j]<=b) {            cout<<j<<endl;        return 0; }} return 0;}

"Codeforces Round #493 (Div. 2) B" cutting

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.