Codeforces Round #485 (Div. 2) C题求三元组(思维)

C. Three displays
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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<j<ki<j<k that the font size increases if you move along the road in a particular direction. Namely, the condition si<sj<sksi<sj<sk should be held.

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

Input

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

The second line contains nn integers s1,s2,,sns1,s2,…,sn (1si1091≤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 (1ci1081≤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<ki<j<k such that si<sj<sksi<sj<sk.

Examples
input
Copy
5
2 4 5 4 10
40 30 20 10 40
output
Copy
90
input
Copy
3
100 101 100
2 4 5
output
Copy
-1
input
Copy
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
output
Copy
33
Note

In the first example you can, for example, choose displays 11, 44 and 55, because s1<s4<s5s1<s4<s5 (2<4<102<4<10), and the rent cost is 40+10+40=9040+10+40=90.

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

题意:选三个数,要求:i<j<k 且a[i]<a[j]<a[k],要求选出来的三个数的权值最小

思路:开始总想的是贪心,二分啥啥啥的。。。结果仔细想了下,他的范围是3000, O(n^3)的时间复杂度肯定不行,O(n^2)就可以过

只要我预处理第三个数,在每个数这从后面找一个权值最小且大于它的数,以此来作为第三个数即可,后面只要枚举两个数即可

#include<cstdio>
#include<cmath>
#include<iostream>
#define ll long long
using namespace std;
ll a[5000],b[5000],dp[5000];
int main() {
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
    for(int i=0;i<n;i++)
        scanf("%d",&b[i]);
    for(int i=0;i<n;i++) 
    {
        ll mn=99999999999;
        for(int j=i+1;j<n;j++) {
            if(a[i]<a[j]) {
                mn=min(mn,b[j]);
            }
        }
        dp[i]=mn;
    }
    ll ans=999999999999;
    for(int i=0;i<n;i++)
    {
        for(int j=i+1;j<n;j++)
        {
            if(a[i]<a[j]) 
            {
                if(dp[j]!=99999999999)
                    ans=min(ans,b[i]+b[j]+dp[j]);
            }
        }
    }
    if(ans==999999999999) printf("-1\n");
    else cout<<ans<<endl;
}

总结:总的来说这次cf div2的题目不是很难,只是自己刷提还是刷的太少了,没想到思路就卡到了,训练太少,刷题太慢,需要好好反省

猜你喜欢

转载自www.cnblogs.com/Lis-/p/9112900.html
今日推荐