CF思维联系– Codeforces-987C - Three displays ( 动态规划)

ACM思维题训练集合
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.

There are n displays placed along a road, and the i-th of them can display a text with font size si only. Maria Stepanovna wants to rent such three displays with indices i<j<k that the font size increases if you move along the road in a particular direction. Namely, the condition si<sj<sk should be held.

The rent cost is for the i-th display is ci. Please determine the smallest cost Maria Stepanovna should pay.

Input
The first line contains a single integer n (3≤n≤3000) — the number of displays.

The second line contains n integers s1,s2,…,sn (1≤si≤109) — the font sizes on the displays in the order they stand along the road.

The third line contains n integers c1,c2,…,cn (1≤ci≤108) — the rent costs for each display.

Output
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices i<j<k such that si<sj<sk.

Examples
Input
5
2 4 5 4 10
40 30 20 10 40
Output
90
Input
3
100 101 100
2 4 5
Output
-1
Input
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output
33
一看到这题,我觉得像是个背包,实际上差不多,只不过就是有了限制条件,后选的序号一定大于之前的序号,且给定的S[i]也需要大于之前选的。然后这个题我觉得数据有点水n2n^2的复杂度竟然能这么快。
在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;
template <typename t>
void read(t &x)
{
    char ch = getchar();
    x = 0;
    int f = 1;
    while (ch < '0' || ch > '9')
        f = (ch == '-' ? -1 : f), ch = getchar();
    while (ch >= '0' && ch <= '9')
        x = x * 10 + ch - '0', ch = getchar();
    x *f;
}
#define wi(n) printf("%d ", n)
#define wl(n) printf("%lld ", n)
#define rep(m, n, i) for (int i = m; i < n; ++i)
#define P puts(" ")
typedef long long ll;
#define MOD 1000000007
#define mp(a, b) make_pair(a, b)
//---------------https://lunatic.blog.csdn.net/-------------------//
const int N = 3005;
const int INF = 0x3f3f3f3f;
int s[N], cost[N], maxi[N];
int dp[N][10], weight[N][10];
int main()
{
    int n;
    read(n);
    rep(1, n + 1, i)
    {
        read(s[i]);
    }
    rep(1, n + 1, i)
    {
        read(cost[i]);
    }
    memset(dp, 0x3f, sizeof(dp));
    //rep(0, n + 1, i)  weight[i] =s[i];
    rep(1, n, i)
    {
        dp[i][1]=cost[i];
        weight[i][1]=s[i];
        for (int j =2; j <= 3; j++)
        {
            for (int k = i+1; k <= n; k++)
                if (s[k] > weight[i][j-1])
                {
                    //cout<<1;
                    if (dp[k][j] > dp[i][j - 1] + cost[k])
                    {
                        //cout<<2;
                        dp[k][j] = dp[i][j - 1] + cost[k];
                        weight[k][j] = s[k];
                    }
                }
        }
    }
    int ans = INF;
    rep(1, n + 1, i)
        ans = min(ans, dp[i][3]);
    if (ans == INF)
        puts("-1");
    else
    {
        wi(ans);
        P;
    }
}
posted @ 2020-02-28 02:04  风骨散人  阅读(136)  评论(0编辑  收藏  举报