前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >CF思维联系– Codeforces-987C - Three displays ( 动态规划)

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

作者头像
风骨散人Chiam
发布2020-11-03 16:44:55
发布2020-11-03 16:44:55
41700
代码可运行
举报
文章被收录于专栏:CSDN旧文CSDN旧文
运行总次数:0
代码可运行

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]也需要大于之前选的。然后这个题我觉得数据有点水 n 2 n^2 n2的复杂度竟然能这么快。

代码语言:javascript
代码运行次数:0
复制
#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;
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/02/28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档