1. 程式人生 > >Codeforces Round #485 (Div. 2) C題求三元組(思維)

Codeforces Round #485 (Div. 2) C題求三元組(思維)

rip ORC 它的 printf ble CA BE byte order

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的題目不是很難,只是自己刷提還是刷的太少了,沒想到思路就卡到了,訓練太少,刷題太慢,需要好好反省

Codeforces Round #485 (Div. 2) C題求三元組(思維)