CF-Three displays

这题上来就来了一手暴力。。然后T了。看了网上的代码,大佬的暴力就是不一样啊,枚举中间的数,然后分别从左右找符合条件的。一个巧妙的n2复杂度。

Three displays

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 nn displays placed along a road, and the ii-th of them can display a text with font size sisi only. Maria Stepanovna wants to rent such three displays with indices i

Input

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

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

The third line contains nn integers c1,c2,…,cnc1,c2,…,cn (1≤ci≤1081≤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

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

题目大意:

有n块大屏幕,每个屏幕上都有一个数字,下面n个数字分别代表这n个屏幕的价格。现在要找到三个连续的屏幕,这三个屏幕上的数字是递增的。问最低价格是多少?
代码如下:

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
struct display
{
    int size;//数字 
    int cost;//价格 
}d[3100];
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)cin>>d[i].size;
    for(int i=0;i<n;i++)cin>>d[i].cost;
    int mi=999999999;
    for(int i=0;i<n;i++)//枚举第二个数 
    {
        int left=999999999,right=999999999,sum=0;
        for(int j=0;j<i;j++)//从左边找数字比它小的 
        {
            if(d[j].size<d[i].size)
              left=min(d[j].cost,left);//找到后价格不断取最小的 
        }
        for(int j=i+1;j<n;j++)//右边同理 
        {
            if(d[j].size>d[i].size)
              right=min(d[j].cost,right);
        }
        sum=d[i].cost+right+left;
        if(sum<mi)mi=sum;//更新总花费 
    }
    if(mi==999999999)cout<<-1<<endl;
    else cout<<mi<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/A7_RIPPER/article/details/80573878