zoukankan      html  css  js  c++  java
  • U

    Problem description

    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 (3n3000) — the number of displays.

    The second line contains n integers s1,s2,,sn (1si10^9) — the font sizes on the displays in the order they stand along the road.

    The third line contains n integers c1,c2,,cn (1ci10^8) — 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

    Note

    In the first example you can, for example, choose displays 14 and 5, because s1<s4<s5(2<4<10), and the rent cost is 40+10+40=90.

    In the second example you can't select a valid triple of indices, so the answer is -1.

    解题思路:题目的意思就是找出连续递增的三个数si,sj,sk,使得si<sj<sk,并且使得ci+cj+ck的和最小。做法:从二个数开始循环到倒数第二个数,每循环到当前值sj就向两边遍历查找符合条件的si、sk对应的最小ci,ck值,如果找得到即m1、m2都≠INF,就将三个c值相加再和原来的最小值mincost进行比较替换;最后如果mincost还是INF,说明找不到这样符合条件的情况,此时输出"-1"。时间复杂度为是O(n2),暴力即过。

    AC代码:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 const int INF=3e8+5;//这里INF要设置成比3e8大一点,因为三个c值相加有可能刚好为3e8
     4 struct NODE{int s,c;}node[3005];
     5 int main(){
     6     int n,m1,m2,mincost=INF;cin>>n;
     7     for(int i=0;i<n;++i)cin>>node[i].s;
     8     for(int i=0;i<n;++i)cin>>node[i].c;
     9     for(int i=1;i<n-1;++i){
    10         m1=m2=INF;
    11         for(int j=i-1;j>=0;--j)
    12             if(node[j].s<node[i].s&&node[j].c<m1)m1=node[j].c;
    13         for(int j=i+1;j<n;++j)
    14             if(node[j].s>node[i].s&&node[j].c<m2)m2=node[j].c;
    15         if(m1!=INF&&m2!=INF)mincost=min(mincost,node[i].c+m1+m2);
    16     }
    17     if(mincost<INF)cout<<mincost<<endl;
    18     else cout<<"-1"<<endl;
    19     return 0;
    20 }
  • 相关阅读:
    How to Setup a Private Proxy Server on EC2 in Under 10 Minutes
    How to Create a Cron Job (Scheduled Task) for Your Website or Blog
    NPM, BOWER, GIT, AND BASH PROXY CONFIGURATIONS
    Android: 通过Runtime.getRuntime().exec调用底层Linux下的程序或脚本
    Proguard breaking audio file in assets or raw
    SOCKSify Ruby
    UNIX / Linux: 2 Ways to Add Swap Space Using dd, mkswap and swapon
    窗体之间传值的方法
    扩展方法,
    反射 type 的基本用法,动态加载插件
  • 原文地址:https://www.cnblogs.com/acgoto/p/9196694.html
Copyright © 2011-2022 走看看