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<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 (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<j<ki<j<k such that si<sj<sksi<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 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.
题意:n个位置,每个位置有两个属性s,c,要求选择3个位置i,j,k(i < j < k),使得si<sj<sk,并使得 ci+cj+ck 最小,求最小的ci + cj + ck
思路:刚写的时候看了一下tag,说是dp,刚看完这个 题完全没dp的想法,甚至想着 n * n logn 的写法。后来才慢慢往dp方面去想,突然想到了LIS,就A了这个题。 dp[i] 表示到 i 位置的上升长度(严格控制为 1 或者 2),sum[i] 是跟随dp[i]的,表示到 i 这个位置上升长度为 2 的两个 c 的最小值。那么当有 s[j] > s[i] && dp[i] == 2 时,更新答案 ans = min(ans,sum[i] + c[j])
Code:
#include<bits/stdc++.h>
#define debug(x) cout << "[" << #x <<": " << (x) <<"]"<< endl
#define pii pair<int,int>
#define clr(a,b) memset((a),b,sizeof(a))
#define rep(i,a,b) for(int i = a;i < b;i ++)
#define pb push_back
#define MP make_pair
#define LL long long
#define ull unsigned LL
#define ls i << 1
#define rs (i << 1) + 1
#define fi first
#define se second
#define ptch putchar
#define CLR(a) while(!(a).empty()) a.pop()
using namespace std;
inline LL read() {
LL s = 0,w = 1;
char ch = getchar();
while(!isdigit(ch)) {
if(ch == '-') w = -1;
ch = getchar();
}
while(isdigit(ch))
s = s * 10 + ch - '0',ch = getchar();
return s * w;
}
inline void write(LL x) {
if(x < 0)
putchar('-'), x = -x;
if(x > 9)
write(x / 10);
putchar(x % 10 + '0');
}
const int maxn = 3010;
int s[maxn],c[maxn];
int dp[maxn],sum[maxn];
int main() {
//#ifndef ONLINE_JUDGE
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
//#endif
int n = read();
for(int i = 1;i <= n;++ i) s[i] = read();
for(int i = 1;i <= n;++ i) c[i] = read();
int ans = 0x3f3f3f3f;
for(int i = 1;i <= n;++ i) dp[i] = 1, sum[i] = 0x3f3f3f3f;
for(int i = 1;i <= n;++ i)
for(int j = i + 1;j <= n;++ j){
if(s[j] > s[i]){
// dp[j] = max(dp[j],dp[i] + 1);
if(dp[i] == 1) dp[j] = dp[i] + 1;
sum[j] = min(sum[j],c[i] + c[j]);
if(dp[i] == 2) ans = min(ans,sum[i] + c[j]);
}
}
write(ans == 0x3f3f3f3f ? -1 : ans); ptch('\n');
return 0;
}